Commit Graph

32 Commits

Author SHA1 Message Date
Gleb Natapov
7d33cf62a5 Add is_smart_ptr metafunction to detect know smart pointers objects 2015-07-02 16:46:23 +03:00
Nadav Har'El
20d2154c4d shared_ptr: add use_count()
Add a use_count() method for shared_ptr, like std::shared_ptr has.

We already had such a method for lw_shared_ptr, but not for shared_ptr.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
2015-06-28 15:14:56 +03:00
Tomasz Grabiec
cf42e4c1a4 core: Fix enable_lw_shared_from_this<T>::shared_from_this() const
Compilation failed when shared_from_this() was called on a const object.

Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
2015-06-16 20:32:08 +03:00
Tomasz Grabiec
8efcec1949 core: enable_lw_shared_from_this: Add missing move and copy constructors
Signed-off-by: Avi Kivity <avi@cloudius-systems.com>
2015-06-16 20:32:07 +03:00
Avi Kivity
606368f6f6 shared_ptr: add debug option for detecting copies on wrong cpus
Track the cpu on which the reference count referred to by shared_ptr was
created, and prevent modifying it on the wrong cpu.
2015-06-01 18:18:52 +03:00
Avi Kivity
fe9c5a2750 shared_ptr: const correctness for lw_shared_ptr::use_count() 2015-05-06 11:41:51 +03:00
Avi Kivity
211c77bf52 shared_ptr: add const support for lw_shared_ptr 2015-05-06 11:41:33 +03:00
Avi Kivity
c952248dc5 shared_ptr: improve const support
Allow enable_shared_from_this<T>::shared_from_this() to return
a shared_ptr<const T> when called on a const object.

Reviewed-by: Pekka Enberg <penberg@cloudius-systems.com>
2015-04-29 12:32:43 +03:00
Pekka Enberg
066a37ad21 shared_ptr: Make shared_ptr work with foreign_ptr
The foreign_ptr wrapper needs 'element_type' to be present in
shared_ptr to be able to access the data.

Fixes the following compilation failure when trying to use shared_ptr
with foreign_ptr:

  In file included from tests/foreign_ptr_test.cc:24:0:
  ./core/distributed.hh: In instantiation of ‘class foreign_ptr<shared_ptr<basic_sstring<char, unsigned int, 15u> > >’:
  tests/foreign_ptr_test.cc:28:54:   required from here
  ./core/distributed.hh:272:56: error: no type named ‘element_type’ in ‘class shared_ptr<basic_sstring<char, unsigned int, 15u> >’
       using element_type = typename PtrType::element_type;

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
2015-04-27 14:39:12 +03:00
Avi Kivity
558af3d9a5 shared_ptr: add nullptr_t overloads 2015-04-07 21:04:57 +03:00
Tomasz Grabiec
6692435757 core: Implement operator<< for smart pointers
It will allow to implement generic join(), which works with
sequences of things, and use it on std::vector<shared_ptr<printable>>
2015-03-23 11:49:18 +02:00
Avi Kivity
8628d98542 shared_ptr: fix reference count loss when creating a derived type with e_s_f_t
make_shared() has a special case for detecting a created class deriving
from enable_shared_from_this<>, so it can point the refcount pointer into
the object's data area instead of creating a shared_ptr_count_base for it.

The code, however, fails to detect a creating class deriving indirectly
from enable_shared_from_this:

   struct base : enable_shared_from_this<base> {};

   struct derived : base {};

   make_shared<derived>();  // <- allocates independent refcount

The result is that the object reference counter lives in two locations.

Fix by detecting the derived class case as well.
2015-03-10 10:56:10 +02:00
Tomasz Grabiec
d674cd7deb core: add make_shared(T&&) overload
Allows for code like this:

  auto p = make_shared(object());
2015-03-04 18:49:26 +02:00
Avi Kivity
1e5df06f9a shared_ptr: provide default std::hash<> specializations
Folloing std::shared_ptr.
2015-03-01 13:36:17 +02:00
Avi Kivity
3d81e06353 shared_ptr: fix dynamic_pointer_cast() dropping a ref after failure
If the input to dynamic_pointer_cast() faild the conversion, we leaked
a reference to the input object.
2015-02-25 11:05:45 +02:00
Avi Kivity
7f8d88371a Add LICENSE, NOTICE, and copyright headers to all source files.
The two files imported from the OSv project retain their original licenses.
2015-02-19 16:52:34 +02:00
Pekka Enberg
fc7cb5ab5e shared_ptr: Fix assignment of polymorphic types
Fix the assignment operator to work with polymorphic types.

Suggested by Nadav.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
2015-02-08 10:24:21 +02:00
Tomasz Grabiec
24227fd933 shared_ptr: Add helpers for hashing shared_ptr<> by value 2015-01-25 13:12:04 +02:00
Gleb Natapov
20d2725ed9 core: shared_ptr add noexcept
Add missing noexcepts. Those on constructors are needed to be able to
store shared pointers in growable containers efficiently.
2015-01-12 17:29:02 +02:00
Pekka Enberg
fa8d120d70 core: Fix shared_ptr pointer casts
Fix compilation errors in the shared_ptr pointer cast functions and
update shared_ptr constructor to take shared_ptr_count_base.

Suggested by Avi.

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
2015-01-07 13:50:59 +02:00
Avi Kivity
66eea763bf shared_ptr: add comparison operators 2015-01-05 13:24:07 +02:00
Avi Kivity
7a317f78a2 shared_ptr: be friend to self
Needed for converting constructor.
2015-01-05 13:24:06 +02:00
Avi Kivity
c7d7494a65 shared_ptr: add some documentation 2015-01-05 10:39:35 +02:00
Avi Kivity
b6d76ed650 shared_ptr: add polymorphic-capable shared pointer
At the cost of doubling the pointer size, this shared_ptr is polymorphic
and can be upcast to a base class.
2015-01-04 23:15:58 +02:00
Avi Kivity
87f63f7b90 shared_ptr: rename to lw_shared_ptr (for light-weight)
The current shared_ptr implementation is efficient, but does not support
polymorphic types.

Rename it in order to make room for a polymorphic shared_ptr.
2015-01-04 22:38:49 +02:00
Avi Kivity
33518ea9d0 shared_ptr: fix uninitialized enable_shared_from_this ref count
Caused immediate leak.
2015-01-01 14:29:27 +02:00
Avi Kivity
3cecef365f shared_ptr: implement enable_shared_from_this<>
Mirrors std::enable_shared_from_this<>.
2014-12-29 11:02:31 +02:00
Tomasz Grabiec
0b4ee2ff60 core: advertise element type in shared_ptr<>
Other smart pointers also do that. Will help foreign_ptr<>.
2014-11-11 13:52:23 +02:00
Asias He
8b737c95e5 core: Add use_count for shared_ptr
This is useful when debugging shared_ptr.
2014-10-22 11:40:18 +03:00
Tomasz Grabiec
319ae0b530 core: add shared_ptr::operator=(T&&)
Allows to move an item directly into existing shread_ptr.
2014-10-15 15:59:42 +02:00
Avi Kivity
e659c7fafc Add specializations to make_shared 2014-09-03 16:22:27 +03:00
Avi Kivity
48f570f57c core: add shared_ptr<> smart pointer
Just like std::shared_ptr<>, except no atomics.
2014-09-02 19:08:52 +03:00