c++-gtk-utils: shared_handle.h Source File

c++-gtk-utils

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

shared_handle.h
Go to the documentation of this file.
1 /* Copyright (C) 2004 to 2014 Chris Vine
2
3 The library comprised in this file or of which this file is part is
4 distributed by Chris Vine under the GNU Lesser General Public
5 License as follows:
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public License
9 as published by the Free Software Foundation; either version 2.1 of
10 the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License, version 2.1, for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License, version 2.1, along with this library (see the file LGPL.TXT
19 which came with this source code package in the c++-gtk-utils
20 sub-directory); if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
23 However, it is not intended that the object code of a program whose
24 source code instantiates a template from this file or uses macros or
25 inline functions (of any length) should by reason only of that
26 instantiation or use be subject to the restrictions of use in the GNU
27 Lesser General Public License. With that in mind, the words "and
28 macros, inline functions and instantiations of templates (of any
29 length)" shall be treated as substituted for the words "and small
30 macros and small inline functions (ten lines or less in length)" in
31 the fourth paragraph of section 5 of that licence. This does not
32 affect any other reason why object code may be subject to the
33 restrictions in that licence (nor for the avoidance of doubt does it
34 affect the application of section 2 of that licence to modifications
35 of the source code in this file).
36
37 */
38
39 #ifndef CGU_SHARED_HANDLE_H
40 #define CGU_SHARED_HANDLE_H
41
42 // define this if, instead of GLIB atomic funcions/memory barriers,
43 // you want to use a (slower) mutex to lock the reference count in the
44 // SharedLockHandle class
45 /* #define CGU_SHARED_LOCK_HANDLE_USE_MUTEX 1 */
46
47 #include <exception>
48 #include <new>
49 #include <functional> // for std::less and std::hash<T*>
50 #include <utility> // for std::swap
51 #include <cstddef> // for std::size_t
52 #include <cstdlib>
53
54 #include <glib.h>
55
56 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
57 #include <c++-gtk-utils/mutex.h>
58 #endif
59
60 #include <c++-gtk-utils/cgu_config.h>
61
62 /**
63 * @addtogroup handles handles and smart pointers
64 */
65
66 namespace Cgu {
67
68 /**
69 * @class SharedHandle shared_handle.h c++-gtk-utils/shared_handle.h
70 * @brief This is a generic class for managing the lifetime of objects
71 * allocated on freestore.
72 * @ingroup handles
73 * @sa SharedLockHandle
74 * @sa ScopedHandle
75 * @sa SharedHandleError
76 * @sa GcharSharedHandle
77 * @sa GerrorSharedHandle
78 * @sa StandardArrayDelete CFree GFree GerrorFree GSliceFree GSliceFreeSize GSliceDestroy
79 *
80 * The SharedHandle class is similar to the SharedPtr class (it keeps
81 * a reference count and deletes the handled object when the count
82 * reaches 0), but it does not have pointer semantics. Accordingly,
83 * it can be used to manage the memory of arrays and other objects
84 * allocated on the heap.
85 *
86 * Because it is useful with arrays, by default it deallocates memory
87 * using C++ delete[]. However, if a SharedHandle object is passed a
88 * function object type as a second template argument when
89 * instantiated, it will use that function object to delete memory.
90 * This enables it to handle the memory of any object, such as objects
91 * to be deleted using std::free() or Glib's g_free(), g_list_free()
92 * or g_slice_free(). Instances (such as @ref GcharScopedHandleAnchor
93 * "GcharScopedHandle", @ref GcharSharedHandleAnchor
94 * "GcharSharedHandle", @ref GerrorSharedHandleAnchor
95 * "GerrorSharedHandle" and @ref GerrorScopedHandleAnchor
96 * "GerrorScopedHandle") typdef'ed for particular deleters can
97 * conveniently manage objects of any kind.
98 *
99 * To reflect the fact that it is just a handle for a pointer, it has
100 * different instantiation semantics from a SharedPtr object. A
101 * SharedPtr object is instantiated using this syntax:
102 *
103 * @code SharedPtr<ObjType> sh_ptr(new ObjType); @endcode
104 *
105 * A SharedHandle is instantiated using this syntax (note that the
106 * instantiated handle is for type T* and not T):
107 *
108 * @code SharedHandle<ObjType*> sh_handle(new ObjType[n]); @endcode
109 *
110 *
111 * Apart from the operatorT() type conversion operator (which returns
112 * the underlying pointer), the only other method to obtain the
113 * underlying pointer is the get() method. If the object referenced
114 * is an array allocated on the heap, to use indexing you could either
115 * do this:
116 *
117 * @code
118 * using namespace Cgu;
119 * SharedHandle<char*> handle(new char[10]);
120 * handle.get()[0] = 'a';
121 * std::cout << handle.get()[0] << std::endl;
122 * @endcode
123 *
124 * or this:
125 *
126 * @code
127 * using namespace Cgu;
128 * SharedHandle<char*> handle(new char[10]);
129 * handle[0] = 'a';
130 * std::cout << handle[0] << std::endl;
131 * @endcode
132 *
133 * There is also a SharedLockHandle class, which has a thread-safe
134 * reference count, and a ScopedHandle class, which deletes its object
135 * as soon as it goes out of scope. A ScopedHandle class can be
136 * viewed as a SharedHandle which cannot be assigned to or used as the
137 * argument to a copy constructor and therefore which cannot have a
138 * reference count of more than 1. It is used where, if you wanted
139 * pointer semantics, you might use a const std::auto_ptr<>.
140 *
141 * SharedHandle objects can be instantiated for pointers to constant
142 * objects (such as SharedHandle<const char*>), provided the deleter
143 * functor will take such pointers.
144 *
145 * This library provides StandardArrayDelete, CFree, GFree,
146 * GerrorFree, GSliceFree, GSliceFreeSize and GSliceDestroy deleter
147 * functors, which can be used as the second template parameter of the
148 * SharedHandle class. As mentioned above, StandardArrayDelete is the
149 * default, and some typedef'ed instances of SharedHandle for gchar
150 * (with the GFree deleter) and for GError (with the GerrorFree
151 * deleter) are provided.
152 *
153 * @b Comparison @b with @b std::shared_ptr
154 *
155 * Although the semantics of std::shared_ptr in C++11/14 are not
156 * particularly suited to managing either arrays or C objects with
157 * accessor functions (such as in glib), most of the things that can
158 * be done by this class can be done by using std::shared_ptr with a
159 * specialised deleter. However, this class is retained in the
160 * c++-gtk-utils library not only to retain compatibility with series
161 * 1.2 of the library, but also to cater for some cases not met (or
162 * not so easily met) by std::shared_ptr:
163 *
164 * 1. The Cgu::SharedHandle class takes its deleter as a template
165 * parameter, which means that typedefs can be used to enable
166 * handles for particular deleters to be easily created (and as
167 * mentioned, this library provides a number of pre-formed deleter
168 * functors and typedefs for them). With std::shared_ptr, custom
169 * deleters must be passed to the shared_ptr constructor on every
170 * occasion a shared_ptr is constructed to manage a new object (and
171 * they cannot be templated as a typedef).
172 * 2. Glib memory slices provide an efficient small object allocator
173 * (they are likely to be significantly more efficient than global
174 * operator new()/new[](), which generally hand off to malloc(),
175 * and whilst malloc() is good for large block allocations it is
176 * generally poor as a small object allocator). Internal
177 * Cgu::SharedHandle allocation using glib memory slices can be
178 * achieved by compiling the library with the
179 * \--with-glib-memory-slices-no-compat configuration option.
180 * 3. If glib memory slices are not used (which do not throw),
181 * constructing a shared pointer for a new managed object (or
182 * calling reset() for a new managed object) might throw if
183 * internal allocation fails. Although by default the
184 * Cgu::SharedHandle implementation will delete the new managed
185 * object in such a case, it also provides an alternative
186 * constructor and reset() method which instead enable the new
187 * object to be accessed via the thrown exception object so that
188 * user code can decide what to do; std::shared_ptr deletes the new
189 * object in every case.
190 * 4. A user can explicitly state whether the shared handle object is
191 * to have atomic increment and decrement-and-test with respect to
192 * the reference count so that the reference count is thread safe
193 * ('no' in the case of Cgu::SharedHandle, and 'yes' in the case of
194 * Cgu::SharedLockHandle). Using atomic functions is unnecessary
195 * if the managed object concerned is only addressed in one thread
196 * (and might cause unwanted cache flushing in certain
197 * circumstances). std::shared_ptr will generally always use
198 * atomic functions with respect to its reference count in a
199 * multi-threaded program.
200 *
201 * In favour of std::shared_ptr, it has an associated std::weak_ptr
202 * class, which Cgu::SharedHandle does not (there is a
203 * Cgu::GobjWeakHandle class, but that is cognate with Cgu::GobjHandle
204 * and is only usable with GObjects).
205 *
206 * If the library is compiled with the
207 * \--with-glib-memory-slices-no-compat configuration option, as
208 * mentioned Cgu::SharedHandle constructs its reference counting
209 * internals using glib memory slices. Although it is safe in a
210 * multi-threaded program if glib < 2.32 is installed to construct a
211 * static SharedHandle object in global namespace (that is, prior to
212 * g_thread_init() being called) by means of the default constructor
213 * and/or a pointer argument of NULL, it is not safe if constructed
214 * with a non-NULL pointer value. If glib >= 2.32 is installed,
215 * global objects with memory slices are safe in all
216 * circumstances. (Having said that, it would be highly unusual to
217 * have global SharedHandle objects.)
218 */
219
220 /********************* here are some deleter classes *******************/
221
222 /**
223 * @class StandardArrayDelete shared_handle.h c++-gtk-utils/shared_handle.h
224 * @brief A deleter functor for use as the second (Dealloc) template
225 * parameter of the SharedHandle, SharedLockHandle or ScopedHandle
226 * template classes, which calls the C++ delete[] expression.
227 * @ingroup handles
228 * @details This functor enables those classes to manage arrays
229 * created with the new expression. It is the default type of the
230 * second template paramenter of those classes.
231 */
232 template <class T> class StandardArrayDelete {
233 public:
234 void operator()(T obj) {
235 delete[] obj;
236 }
237 };
238
239 /**
240 * @class CFree shared_handle.h c++-gtk-utils/shared_handle.h
241 * @brief A deleter functor for use as the second (Dealloc) template
242 * parameter of the SharedHandle, SharedLockHandle or ScopedHandle
243 * template classes, which calls std::free.
244 * @ingroup handles
245 * @details This functor enables those classes to manage memory
246 * allocated with std::malloc(), std::calloc() and std::realloc().
247 */
248 class CFree {
249 public:
250 void operator()(const void* obj) noexcept {
251 std::free(const_cast<void*>(obj));
252 }
253 };
254
255 /**
256 * @class GFree shared_handle.h c++-gtk-utils/shared_handle.h
257 * @brief A deleter functor for use as the second (Dealloc) template
258 * parameter of the SharedHandle, SharedLockHandle or ScopedHandle
259 * template classes, which calls glib's g_free().
260 * @ingroup handles
261 * @details This functor enables those classes to manage memory
262 * allocated by glib or gtk+ functions which requires to be freed with
263 * g_free(). It is used in the typedefs @ref GcharSharedHandleAnchor
264 * "GcharSharedHandle" and @ref GcharScopedHandleAnchor
265 * "GcharScopedHandle".
266 */
267 class GFree {
268 public:
269 void operator()(const void* obj) noexcept {
270 g_free(const_cast<void*>(obj));
271 }
272 };
273
274 /**
275 * @class GSliceFree shared_handle.h c++-gtk-utils/shared_handle.h
276 * @brief A deleter functor for use as the second (Dealloc) template
277 * parameter of the SharedHandle, SharedLockHandle or ScopedHandle
278 * template classes, which calls glib's g_slice_free1().
279 * @ingroup handles
280 *
281 * @details This functor enables those classes to manage a memory
282 * block allocated using glib memory slices. The managed memory block
283 * to be deleted by the GSliceFree functor must have the same size as
284 * the size of the object for which the functor is instantiated by
285 * pointer, as for example as allocated with the g_slice_new,
286 * g_slice_new0 or g_slice_dup macros (in other words, the GSliceFree
287 * template parameter must match the argument passed to those macros):
288 * see the example below. Use GSliceFreeSize where it is necessary or
289 * more convenient to have the size of the block to be freed as the
290 * template parameter. Use GSliceDestroy where the memory holds a C++
291 * object constructed in the memory by the global placement new
292 * expression.
293 *
294 * The type of the template argument for the functor is a pointer to
295 * the managed type: it is the same as the first template argument of
296 * the relevant SharedHandle, SharedLockHandle or ScopedHandle object.
297 * For example:
298 *
299 * @code
300 * using namespace Cgu;
301 * SharedHandle<MyStruct*, GSliceFree<MyStruct*> > h(g_slice_new(MyStruct));
302 * ...
303 * @endcode
304 *
305 * The availability of this functor is not dependent on the library
306 * having been installed with the \--with-glib-memory-slices-compat or
307 * \--with-glib-memory-slices-no-compat configuration option (see @ref
308 * Memory for further details of those options).
309 */
310 template <class T> class GSliceFree {
311 public:
312 void operator()(T obj) noexcept {
313 g_slice_free1(sizeof(*obj), (void*)obj);
314 }
315 };
316
317 /**
318 * @class GSliceDestroy shared_handle.h c++-gtk-utils/shared_handle.h
319 * @brief A deleter functor for use as the second (Dealloc) template
320 * parameter of the SharedHandle, SharedLockHandle or ScopedHandle
321 * template classes, which calls glib's g_slice_free1(), but before
322 * doing so also explicitly calls the destructor of a C++ object
323 * constructed in the memory.
324 * @ingroup handles
325 *
326 * @details The managed memory block to be deleted by the
327 * GSliceDestroy functor must have the same size as the size of the
328 * object for which the functor is instantiated by pointer, as for
329 * example as allocated with the g_slice_new or g_slice_new0 macros
330 * (in other words, the GSliceDestroy template parameter must match
331 * the argument passed to those macros), and the memory block must
332 * have had that object constructed in it with the global placement
333 * new expression: see the example below. Sometimes it is more
334 * convenient to implement C++ objects in glib memory slices that way,
335 * rather than to have custom new and delete member operators of the
336 * classes concerned which use glib's g_slice_*(). However, a
337 * SharedHandle class with a GSliceDestroy deleter is not as easy to
338 * use as the SharedPtr class, as SharedHandle has no operator*() nor
339 * operator->() method (the get() method would have to be used to
340 * obtain the underlying pointer).
341 *
342 * One consequence of the static sizing (and so typing) of memory
343 * slices is that a GSliceDestroy object instantiated for the
344 * management of a particular class must not be used by a
345 * SharedHandle, SharedLockHandle or ScopedHandle object which
346 * attempts to manage a class derived from it. This comes back to the
347 * point that the GSliceDestroy template parameter must match the
348 * argument passed to the g_slice_new or g_slice_new0 macros.
349 *
350 * The type of the template argument for the functor is a pointer to
351 * the managed type: it is the same as the first template argument of
352 * the relevant SharedHandle, SharedLockHandle or ScopedHandle object.
353 * For example, to construct a SharedHandle managing an object of type
354 * MyClass to be constructed in a glib memory slice in an exception
355 * safe way:
356 *
357 * @code
358 * using namespace Cgu;
359 * SharedHandle<MyClass*, GSliceDestroy<MyClass*> > h; // won't throw
360 * { // scope block for p variable
361 * MyClass* p = g_slice_new(MyClass);
362 * try {new(p) MyClass;} // MyClass constructor might throw
363 * catch(...) {
364 * g_slice_free(MyClass, p);
365 * throw;
366 * }
367 * h.reset(p); // might throw but if so cleans up
368 * }
369 * ...
370 * @endcode
371 *
372 * The availability of this functor is not dependent on the library
373 * having been installed with the \--with-glib-memory-slices-compat or
374 * \--with-glib-memory-slices-no-compat configuration option (see @ref
375 * Memory for further details of those options).
376 */
377 template <class T> class GSliceDestroy {
378 template <class U> void destroy(U& obj) {obj.~U();}
379 public:
380 void operator()(T obj) {
381 destroy(*obj);
382 g_slice_free1(sizeof(*obj), (void*)obj);
383 }
384 };
385
386 /**
387 * @class GSliceFreeSize shared_handle.h c++-gtk-utils/shared_handle.h
388 * @brief A deleter functor for use as the second (Dealloc) template
389 * parameter of the SharedHandle, SharedLockHandle or ScopedHandle
390 * template classes, which calls glib's g_slice_free1().
391 * @ingroup handles
392 *
393 * @details This functor enables those classes to manage memory
394 * allocated with g_slice_alloc(), g_slice_alloc0() or g_slice_copy().
395 * It is an alternative to using GSliceFree where, instead of the
396 * template parameter being a pointer to a particular managed type,
397 * the size of the memory block to be freed is passed, so enabling it
398 * to be more conveniently used to free memory containing arrays of
399 * built-in types or of PODSs. Use GSliceDestroy where the memory
400 * holds a C++ object constructed in the memory by the global
401 * placement new expression.
402 *
403 * The type of the template argument for the functor is an integer
404 * type (gsize) and is the size of the block to be managed. For
405 * example:
406 *
407 * @code
408 * using namespace Cgu;
409 * SharedHandle<char*, GSliceFreeSize<10> > h(static_cast<char*>(g_slice_alloc(10)));
410 * ...
411 * @endcode
412 *
413 * The availability of this functor is not dependent on the library
414 * having been installed with the \--with-glib-memory-slices-compat or
415 * \--with-glib-memory-slices-no-compat configuration option (see @ref
416 * Memory for further details of those options).
417 */
418 template <gsize block_size> class GSliceFreeSize {
419 public:
420 void operator()(const void* obj) noexcept {
421 g_slice_free1(block_size, const_cast<void*>(obj));
422 }
423 };
424
425 /*
426 * we could provide a functor class for
427 * g_slice_free_chain_with_offset() such as:
428 *
429 * template <class T, gsize offset> class GSliceFreeChain {
430 * public:
431 * void operator()(T obj) {
432 * g_slice_free_chain_with_offset(sizeof(*obj), (void*)obj, offset);
433 * }
434 * };
435 *
436 * However, this is not going to be particularly useful because the
437 * G_STRUCT_OFFSET macro and/or C's offsetof macro, needed to provide
438 * the value for the offset parameter, do not work for other than
439 * PODSs. g_slice_free_chain_with_offset() is intended for internal
440 * implementations and in the event of a user wanting such memory
441 * management it is best achieved by having custom new[] and delete[]
442 * member operators of the class concerned which use glib's
443 * g_slice_*() directly.
444 */
445
446 /********************* define some typedefs for Glib ******************/
447
448 template <class T, class Dealloc> class SharedHandle;
449 template <class T, class Dealloc> class ScopedHandle;
450
451 /**
452 * @typedef GcharSharedHandle.
453 * @brief A handle comprising a typed instance of the SharedHandle
454 * class for gchar* arrays and strings
455 * @anchor GcharSharedHandleAnchor
456 * @ingroup handles
457 * \#include <c++-gtk-utils/shared_handle.h>
458 */
459 typedef SharedHandle<gchar*, GFree> GcharSharedHandle;
460
461 /**
462 * @typedef GcharScopedHandle.
463 * @brief A handle comprising a typed instance of the ScopedHandle
464 * class for gchar* arrays and strings
465 * @anchor GcharScopedHandleAnchor
466 * @ingroup handles
467 * \#include <c++-gtk-utils/shared_handle.h>
468 */
469 typedef ScopedHandle<gchar*, GFree> GcharScopedHandle;
470
471
472 /******************* now the handle class definitions *****************/
473
474 /**
475 * @class SharedHandleError shared_handle.h c++-gtk-utils/shared_handle.h
476 * @brief This is an exception struct thrown as an alternative to
477 * deleting a managed object when internal memory allocation for
478 * SharedHandle or SharedLockHandle fails in their reset() method or
479 * in their constructor which takes a pointer.
480 * @sa SharedHandle SharedLockHandle SharedHandleAllocFail
481 * @ingroup handles
482 *
483 * This is an exception struct thrown as an alternative to deleting a
484 * managed object when SharedHandle<T>::SharedHandle(T),
485 * SharedLockHandle<T>::SharedLockHandle(T), SharedHandle<T>::reset(T)
486 * or SharedLockHandle<T>::reset(T) would otherwise throw
487 * std::bad_alloc. To make those methods do that,
488 * Cgu::SharedHandleAllocFail::leave is passed as their second
489 * argument.
490 *
491 * If the exception is thrown, the struct has a member 'obj' of type
492 * T, which is a pointer to the object or array originally passed to
493 * those methods, so the user can deal with it appropriately. This
494 * enables the result of the new expression to be passed directly as
495 * the argument to those methods without giving rise to a resource
496 * leak, as in:
497 *
498 * @code
499 * using namespace Cgu;
500 * SharedHandle<T*> s; // doesn't throw
501 * try {
502 * s.reset(new T[2], SharedHandleAllocFail::leave); // both T allocation and reset() might throw
503 * }
504 * catch (std::bad_alloc&) {
505 * ...
506 * }
507 * catch (SharedHandleError<T*>& e) {
508 * e.obj[0].do_something();
509 * e.obj[1].do_something();
510 * ...
511 * }
512 * ...
513 * @endcode
514 *
515 * As above, a catch block will need to deal with std::bad_alloc (if
516 * the call to the new expression when creating the T object fails)
517 * as well as SharedHandleError (if the call to the new expression in
518 * the reset() method fails after a valid T object has been
519 * constructed).
520 */
521
522 template <class T> struct SharedHandleError: public std::exception {
523 T obj;
524 virtual const char* what() const throw() {return "SharedHandleError\n";}
525 SharedHandleError(T p): obj(p) {}
526 };
527
528 /**
529 * enum Cgu::SharedHandleAllocFail::Leave
530 * The enumerator Cgu::SharedHandleAllocFail::leave is passed as the
531 * second argument of the reset() method of SharedHandle or
532 * SharedLockHandle in order to prevent the method deleting the object
533 * passed to it if reset() fails internally because of memory
534 * exhaustion.
535 * @ingroup handles
536 */
537 namespace SharedHandleAllocFail {
538 enum Leave {leave};
539 }
540
541 template <class T, class Dealloc = StandardArrayDelete<T>> class SharedHandle {
542
543 Dealloc deleter;
544
545 #ifndef DOXYGEN_PARSING
546 struct RefItems {
547 unsigned int* ref_count_p;
548 T obj;
549 } ref_items;
550 #endif
551
552 void unreference() {
553 if (!ref_items.ref_count_p) return;
554 --(*ref_items.ref_count_p);
555 if (*ref_items.ref_count_p == 0) {
556 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
557 g_slice_free(unsigned int, ref_items.ref_count_p);
558 #else
559 delete ref_items.ref_count_p;
560 #endif
561 deleter(ref_items.obj);
562 }
563 }
564
565 void reference() noexcept {
566 if (!ref_items.ref_count_p) return;
567 ++(*ref_items.ref_count_p);
568 }
569
570 public:
571 /**
572 * Constructor taking an unmanaged object.
573 * @param ptr The object which the SharedHandle is to manage (if
574 * any).
575 * @exception std::bad_alloc This constructor will not throw if the
576 * 'ptr' argument has a NULL value (the default), otherwise it might
577 * throw std::bad_alloc if memory is exhausted and the system throws
578 * in that case. If such an exception is thrown, this constructor is
579 * exception safe (it does not leak resources), but as well as
580 * cleaning itself up this constructor will also delete the managed
581 * object passed to it to avoid a memory leak. If such automatic
582 * deletion is not wanted in that case, use the version of this
583 * constructor taking a Cgu::SharedHandleAllocFail::Leave tag argument.
584 * @note std::bad_alloc will not be thrown if the library has been
585 * installed using the \--with-glib-memory-slices-no-compat
586 * configuration option: instead glib will terminate the program if it
587 * is unable to obtain memory from the operating system.
588 */
589 explicit SharedHandle(T ptr = 0) {
590
591 if ((ref_items.obj = ptr)) { // not NULL
592 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
593 ref_items.ref_count_p = g_slice_new(unsigned int);
594 *ref_items.ref_count_p = 1;
595 #else
596 try {
597 ref_items.ref_count_p = new unsigned int(1);
598 }
599 catch (...) {
600 deleter(ptr); // if allocating the int referenced by ref_items.ref_count_p
601 // has failed then delete the object to be referenced to
602 // avoid a memory leak
603 throw;
604 }
605 #endif
606 }
607 else ref_items.ref_count_p = 0;
608 }
609
610 /**
611 * Constructor taking an unmanaged object.
612 * @param ptr The object which the SharedHandle is to manage
613 * @param tag Passing the tag emumerator
614 * Cgu::SharedHandleAllocFail::leave causes this constructor not to
615 * delete the new managed object passed as the 'ptr' argument in the
616 * event of internal allocation in this method failing because of
617 * memory exhaustion (in that event, Cgu::SharedHandleError will be
618 * thrown).
619 * @exception Cgu::SharedHandleError This constructor might throw
620 * Cgu::SharedHandleError if memory is exhausted and the system would
621 * otherwise throw std::bad_alloc in that case. This constructor is
622 * exception safe (it does not leak resources), and if such an
623 * exception is thrown it will clean itself up, but it will not
624 * attempt to delete the new managed object passed to it. Access to
625 * the object passed to the 'ptr' argument can be obtained via the
626 * thrown Cgu::SharedHandleError object.
627 * @note 1. On systems with over-commit/lazy-commit combined with
628 * virtual memory (swap), it is rarely useful to check for memory
629 * exhaustion, so in those cases this version of the constructor will
630 * not be useful.
631 * @note 2. If the library has been installed using the
632 * \--with-glib-memory-slices-no-compat configuration option this
633 * version of the constructor will also not be useful: instead glib
634 * will terminate the program if it is unable to obtain memory from
635 * the operating system.
636 */
637 SharedHandle(T ptr, Cgu::SharedHandleAllocFail::Leave tag) {
638
639 if ((ref_items.obj = ptr)) { // not NULL
640 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
641 ref_items.ref_count_p = g_slice_new(unsigned int);
642 *ref_items.ref_count_p = 1;
643 #else
644 try {
645 ref_items.ref_count_p = new unsigned int(1);
646 }
647 catch (std::bad_alloc&) { // as we are not rethrowing, make NPTL friendly
648 throw SharedHandleError<T>(ptr);
649 }
650 #endif
651 }
652 else ref_items.ref_count_p = 0;
653 }
654
655 /**
656 * Causes the SharedHandle to cease to manage its managed object (if
657 * any), deleting it if this is the last SharedHandle object managing
658 * it. If the argument passed is not NULL, the SharedHandle object
659 * will manage the new object passed (which must not be managed by any
660 * other SharedHandle object). This method is exception safe, but see
661 * the comments below on std::bad_alloc.
662 * @param ptr NULL (the default), or a new unmanaged object to manage.
663 * @exception std::bad_alloc This method will not throw if the 'ptr'
664 * argument has a NULL value (the default) and the destructor of a
665 * managed object does not throw, otherwise it might throw
666 * std::bad_alloc if memory is exhausted and the system throws in that
667 * case. Note that if such an exception is thrown then this method
668 * will do nothing (it is strongly exception safe and will continue to
669 * manage the object it was managing prior to the call), except that
670 * it will delete the new managed object passed to it to avoid a
671 * memory leak. If such automatic deletion in the event of such an
672 * exception is not wanted, use the reset() method taking a
673 * Cgu::SharedHandleAllocFail::Leave tag type as its second argument.
674 * @note std::bad_alloc will not be thrown if the library has been
675 * installed using the \--with-glib-memory-slices-no-compat
676 * configuration option: instead glib will terminate the program if it
677 * is unable to obtain memory from the operating system.
678 */
679 void reset(T ptr = 0) {
680 SharedHandle tmp(ptr);
681 std::swap(ref_items, tmp.ref_items);
682 }
683
684 /**
685 * Causes the SharedHandle to cease to manage its managed object (if
686 * any), deleting it if this is the last SharedHandle object managing
687 * it. The SharedHandle object will manage the new object passed
688 * (which must not be managed by any other SharedHandle object). This
689 * method is exception safe, but see the comments below on
690 * Cgu::SharedHandleError.
691 * @param ptr A new unmanaged object to manage (if no new object is to
692 * be managed, use the version of reset() taking a default value of
693 * NULL).
694 * @param tag Passing the tag emumerator
695 * Cgu::SharedHandleAllocFail::leave causes this method not to delete
696 * the new managed object passed as the 'ptr' argument in the event of
697 * internal allocation in this method failing because of memory
698 * exhaustion (in that event, Cgu::SharedHandleError will be thrown).
699 * @exception Cgu::SharedHandleError This method might throw
700 * Cgu::SharedHandleError if memory is exhausted and the system would
701 * otherwise throw std::bad_alloc in that case. Note that if such an
702 * exception is thrown then this method will do nothing (it is
703 * strongly exception safe and will continue to manage the object it
704 * was managing prior to the call), and it will not attempt to delete
705 * the new managed object passed to it. Access to the object passed
706 * to the 'ptr' argument can be obtained via the thrown
707 * Cgu::SharedHandleError object.
708 * @note 1. On systems with over-commit/lazy-commit combined with
709 * virtual memory (swap), it is rarely useful to check for memory
710 * exhaustion, so in those cases this version of the reset() method
711 * will not be useful.
712 * @note 2. If the library has been installed using the
713 * \--with-glib-memory-slices-no-compat configuration option this
714 * version of the reset() method will also not be useful: instead glib
715 * will terminate the program if it is unable to obtain memory from
716 * the operating system.
717 */
718 void reset(T ptr, Cgu::SharedHandleAllocFail::Leave tag) {
719 SharedHandle tmp(ptr, tag);
720 std::swap(ref_items, tmp.ref_items);
721 }
722
723 /**
724 * The copy constructor does not throw.
725 * @param sh_hand The handle to be copied.
726 */
727 SharedHandle(const SharedHandle& sh_hand) noexcept {
728 ref_items = sh_hand.ref_items;
729 reference();
730 }
731
732 /**
733 * The move constructor does not throw. It has move semantics.
734 * @param sh_hand The handle to be moved.
735 */
736 SharedHandle(SharedHandle&& sh_hand) noexcept {
737 ref_items = sh_hand.ref_items;
738 sh_hand.ref_items.ref_count_p = 0;
739 sh_hand.ref_items.obj = 0;
740 }
741
742 /**
743 * This method (and so copy or move assignment) does not throw unless
744 * the destructor of a managed object throws.
745 * @param sh_hand the assignor.
746 * @return The SharedHandle object after assignment.
747 */
748 // having a value type as the argument, rather than reference to const
749 // and then initialising a tmp object, gives the compiler more scope
750 // for optimisation, and also caters for r-values without a separate
751 // overload
752 SharedHandle& operator=(SharedHandle sh_hand) {
753 std::swap(ref_items, sh_hand.ref_items);
754 return *this;
755 }
756
757 /**
758 * This method does not throw.
759 * @return A pointer to the handled object (or NULL if none is
760 * handled).
761 */
762 T get() const noexcept {return ref_items.obj;}
763
764 /**
765 * This method does not throw.
766 * @return A pointer to the handled object (or NULL if none is
767 * handled).
768 */
769 operator T() const noexcept {return ref_items.obj;}
770
771 /**
772 * This method does not throw.
773 * @return The number of SharedHandle objects referencing the managed
774 * object (or 0 if none is managed by this SharedHandle).
775 */
776 unsigned int get_refcount() const noexcept {return (ref_items.ref_count_p) ? *ref_items.ref_count_p : 0;}
777
778 /**
779 * The destructor does not throw unless the destructor of a handled
780 * object throws - that should never happen.
781 */
782 ~SharedHandle() {unreference();}
783 };
784
785 /**
786 * @class ScopedHandle shared_handle.h c++-gtk-utils/shared_handle.h
787 * @brief This is a generic scoped class for managing the lifetime of objects
788 * allocated on freestore.
789 * @ingroup handles
790 * @sa SharedHandle SharedLockHandle SharedHandleError
791 * @sa StandardArrayDelete CFree GFree GerrorFree GSliceFree GSliceFreeSize GSliceDestroy
792 *
793 * This class deletes its object as soon as it goes out of scope. It
794 * can be viewed as a SharedHandle which cannot be copy assigned to or
795 * used as the argument to a copy constructor and therefore which
796 * cannot have a reference count of more than 1.
797 *
798 * ScopedHandle objects can be instantiated for pointers to constant
799 * objects (such as ScopedHandle<const char*>), provided the deleter
800 * functor will take such pointers.
801 *
802 * This library provides StandardArrayDelete, CFree, GFree,
803 * GerrorFree, GSliceFree, GSliceFreeSize and GSliceDestroy deleter
804 * functors, which can be used as the second template parameter of the
805 * ScopedHandle class. StandardArrayDelete is the default, and some
806 * typedef'ed instances of ScopedHandle for gchar (with the GFree
807 * deleter) and for GError (with the GerrorFree deleter) are provided:
808 * @ref GcharScopedHandleAnchor "GcharScopedHandle" and @ref
809 * GerrorScopedHandleAnchor "GerrorScopedHandle")
810 *
811 * @b Comparison @b with @b std::unique_ptr
812 *
813 * This class is mainly retained to retain compatibility with series
814 * 1.2 of the library, since most of the things that can be done with
815 * it can also be done using std::unique_ptr. However, this class is
816 * a little easier to use when managing objects with associated C
817 * functions (such as in glib), because it provides a type conversion
818 * operator.
819 *
820 * From version 2.2.2, this class has a move constructor and move
821 * assignment operator. Prior to that, it could not be moved from or
822 * to.
823 */
824
825 template <class T, class Dealloc = StandardArrayDelete<T>> class ScopedHandle {
826 Dealloc deleter;
827 T obj;
828 public:
829 /**
830 * This class cannot be copied. The copy constructor is deleted.
831 */
832 ScopedHandle(const ScopedHandle&) = delete;
833
834 /**
835 * This class cannot be copied. The copy assignment operator is
836 * deleted.
837 */
838 ScopedHandle& operator=(const ScopedHandle&) = delete;
839
840 /**
841 * The move constructor does not throw.
842 * @param sc_hand The handle to be moved.
843 *
844 * Since 2.0.19/2.2.2
845 */
846 ScopedHandle(ScopedHandle&& sc_hand) noexcept {
847 obj = sc_hand.obj;
848 sc_hand.obj = 0;
849 }
850
851 /**
852 * The move assignment operator. It will delete the object managed
853 * prior to the move, if any. It does not throw unless the destructor
854 * of that object throws.
855 * @param sc_hand The handle to be moved.
856 * @return The ScopedHandle object after move assignment.
857 *
858 * Since 2.0.19/2.2.2
859 */
860 ScopedHandle& operator=(ScopedHandle&& sc_hand) {
861 reset(sc_hand.release());
862 return *this;
863 }
864
865 /**
866 * This constructor does not throw.
867 * @param ptr The object which the ScopedHandle is to manage (if
868 * any).
869 *
870 * ScopedHandle objects can be instantiated for pointers to constant
871 * objects (such as SharedHandle<const char*>), provided the deleter
872 * functor will take such pointers.
873 */
874 explicit ScopedHandle(T ptr = 0) noexcept: obj(ptr) {}
875
876 /**
877 * Causes the ScopedHandle to delete its managed object (if any), and
878 * if the argument passed is not NULL, the ScopedHandle object will
879 * manage the new object passed (which must not be managed by any
880 * other ScopedHandle object). This method does not throw (assuming
881 * the destructor of a managed object does not throw).
882 * @param ptr NULL (the default), or a new unmanaged object to manage.
883 */
884 void reset(T ptr = 0) {
885 std::swap(obj, ptr);
886 if (ptr) deleter(ptr); // ptr now points to the original managed object
887 }
888
889 /**
890 * Causes the ScopedHandle to cease to manage the handled object, but
891 * does not delete that object. This method does not throw.
892 * @return A pointer to the previously handled object (or NULL if none
893 * was handled).
894 */
895 T release() noexcept {T tmp = obj; obj = 0; return tmp;}
896
897 /**
898 * This method does not throw.
899 * @return A pointer to the handled object (or NULL if none is
900 * handled).
901 */
902 T get() const noexcept {return obj;}
903
904 /**
905 * This method does not throw.
906 * @return A pointer to the handled object (or NULL if none is
907 * handled).
908 */
909 operator T() const noexcept {return obj;}
910
911 /**
912 * The destructor does not throw unless the destructor of a handled
913 * object throws - that should never happen.
914 */
915 ~ScopedHandle() {if (obj) deleter(obj);}
916 };
917
918
919 /**
920 * @class SharedLockHandle shared_handle.h c++-gtk-utils/shared_handle.h
921 * @brief This is a generic class for managing the lifetime of objects
922 * allocated on freestore, with a thread safe reference count..
923 * @ingroup handles
924 * @sa SharedHandle ScopedHandle SharedHandleError
925 * @sa StandardArrayDelete CFree GFree GerrorFree GSliceFree GSliceFreeSize GSliceDestroy
926 *
927 * Class SharedLockHandle is a version of the SharedHandle class which
928 * includes synchronization so that it can handle objects accessed in
929 * multiple threads (although the word Lock is in the title, by
930 * default it uses glib atomic functions to access the reference count
931 * rather than a mutex, so the overhead should be very small). Note
932 * that only the reference count is protected, so this is thread safe
933 * in the sense in which a raw pointer is thread safe. A shared
934 * handle accessed in one thread referencing a particular object is
935 * thread safe as against another shared handle accessing the same
936 * object in a different thread. It is thus suitable for use in
937 * different standard C++ containers which exist in different threads
938 * but which contain shared objects by reference. But:
939 *
940 * 1. If the referenced object is to be modified in one thread and
941 * read or modified in another thread an appropriate mutex for the
942 * referenced object is required (unless that referenced object
943 * does its own locking).
944 * 2. If the same instance of shared handle is to be modified in one
945 * thread (by assigning to the handle so that it references a
946 * different object, or by moving from it), and copied (assigned
947 * from or used as the argument of a copy constructor), accessed,
948 * destroyed or modified in another thread, a mutex for that
949 * instance of shared handle is required.
950 * 3. Objects referenced by shared handles which are objects for
951 * which POSIX provides no guarantees (in the main, those which
952 * are not built-in types), such as strings and similar
953 * containers, may not support concurrent reads in different
954 * threads. That depends on the library implementation concerned.
955 * If that is the case, a mutex for the referenced object will
956 * also be required when reading any given instance of such an
957 * object in more than one thread by dereferencing any shared
958 * handles referencing it (and indeed, when not using shared
959 * handles at all).
960 *
961 * SharedLockHandle objects can be instantiated for pointers to
962 * constant objects (such as SharedLockHandle<const char*>), provided
963 * the deleter functor will take such pointers.
964 *
965 * This library provides StandardArrayDelete, CFree, GFree,
966 * GerrorFree, GSliceFree, GSliceFreeSize and GSliceDestroy deleter
967 * functors, which can be used as the second template parameter of the
968 * SharedLockHandle class. StandardArrayDelete is the default.
969 *
970 * As mentioned, by default glib atomic functions are used to provide
971 * thread-safe manipulation of the reference count. However, the
972 * symbol CGU_SHARED_LOCK_HANDLE_USE_MUTEX can be defined so that the
973 * library uses mutexes instead, which might be useful for some
974 * debugging purposes. Note that if CGU_SHARED_LOCK_HANDLE_USE_MUTEX
975 * is to be defined, this is best done by textually amending the
976 * shared_handle.h header file before the library is compiled. This
977 * will ensure that everything in the program and the library which
978 * includes the shared_handle.h header is guaranteed to see the same
979 * definitions so that the C++ standard's one-definition-rule is
980 * complied with.
981 *
982 * @b Comparison @b with @b std::shared_ptr
983 *
984 * Although the semantics of std::shared_ptr in C++11/14 are not
985 * particularly suited to managing either arrays or C objects with
986 * accessor functions (such as in glib), most of the things that can
987 * be done by this class can be done by using std::shared_ptr with a
988 * specialised deleter. However, this class is retained in the
989 * c++-gtk-utils library not only to retain compatibility with series
990 * 1.2 of the library, but also to cater for some cases not met (or
991 * not so easily met) by std::shared_ptr:
992 *
993 * 1. The Cgu::SharedLockHandle class takes its deleter as a template
994 * parameter, which means that typedefs can be used to enable
995 * handles for particular deleters to be easily created (and as
996 * mentioned, this library provides a number of pre-formed deleter
997 * functors and typedefs for them). With std::shared_ptr, custom
998 * deleters must be passed to the shared_ptr constructor on every
999 * occasion a shared_ptr is constructed to manage a new object (and
1000 * they cannot be templated as a typedef).
1001 * 2. Glib memory slices provide an efficient small object allocator
1002 * (they are likely to be significantly more efficient than global
1003 * operator new()/new[](), which generally hand off to malloc(),
1004 * and whilst malloc() is good for large block allocations it is
1005 * generally poor as a small object allocator). Internal
1006 * Cgu::SharedLockHandle allocation using glib memory slices can be
1007 * achieved by compiling the library with the
1008 * \--with-glib-memory-slices-no-compat configuration option.
1009 * 3. If glib memory slices are not used (which do not throw),
1010 * constructing a shared pointer for a new managed object (or
1011 * calling reset() for a new managed object) might throw if
1012 * internal allocation fails. Although by default the
1013 * Cgu::SharedLockHandle implementation will delete the new managed
1014 * object in such a case, it also provides an alternative
1015 * constructor and reset() method which instead enable the new
1016 * object to be accessed via the thrown exception object so that
1017 * user code can decide what to do; std::shared_ptr deletes the new
1018 * object in every case.
1019 * 4. A user can explicitly state whether the shared handle object is
1020 * to have atomic increment and decrement-and-test with respect to
1021 * the reference count so that the reference count is thread safe
1022 * ('no' in the case of Cgu::SharedHandle, and 'yes' in the case of
1023 * Cgu::SharedLockHandle). Using atomic functions is unnecessary
1024 * if the managed object concerned is only addressed in one thread
1025 * (and might cause unwanted cache flushing in certain
1026 * circumstances). std::shared_ptr will generally always use
1027 * atomic functions with respect to its reference count in a
1028 * multi-threaded program.
1029 *
1030 * In favour of std::shared_ptr, it has an associated std::weak_ptr
1031 * class, which Cgu::SharedLockHandle does not (there is a
1032 * Cgu::GobjWeakHandle class, but that is cognate with Cgu::GobjHandle
1033 * and is only usable with GObjects). In addition shared_ptr objects
1034 * have some atomic store, load and exchange functions provided for
1035 * them which enable concurrent modifications of the same instance of
1036 * shared_ptr in different threads to have defined results.
1037 *
1038 * If the library is compiled with the
1039 * \--with-glib-memory-slices-no-compat configuration option, as
1040 * mentioned Cgu::SharedLockHandle constructs its reference counting
1041 * internals using glib memory slices. Although it is safe in a
1042 * multi-threaded program if glib < 2.32 is installed to construct a
1043 * static SharedLockHandle object in global namespace (that is, prior
1044 * to g_thread_init() being called) by means of the default
1045 * constructor and/or a pointer argument of NULL, it is not safe if
1046 * constructed with a non-NULL pointer value. If glib >= 2.32 is
1047 * installed, global objects with memory slices are safe in all
1048 * circumstances. (Having said that, it would be highly unusual to
1049 * have global SharedLockHandle objects.)
1050 */
1051
1052 template <class T, class Dealloc = StandardArrayDelete<T>> class SharedLockHandle {
1053
1054 Dealloc deleter;
1055
1056 #ifndef DOXYGEN_PARSING
1057 struct RefItems {
1058 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
1059 Thread::Mutex* mutex_p;
1060 unsigned int* ref_count_p;
1061 #else
1062 gint* ref_count_p;
1063 #endif
1064 T obj;
1065 } ref_items;
1066 #endif
1067
1068 // SharedLockHandle<T, Dealloc>::unreference() does not throw exceptions
1069 // because Thread::Mutex::~Mutex(), Thread::Mutex::lock() and Thread::Mutex::unlock()
1070 // do not throw
1071 void unreference() {
1072 // we can (and should) check whether ref_items.ref_count_p is NULL without
1073 // a lock, because that member is specific to this SharedLockHandle object.
1074 // Only the integer pointed to by it is shared amongst SharedLockHandle
1075 // objects and requires locking
1076 if (!ref_items.ref_count_p) return;
1077 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
1078 ref_items.mutex_p->lock();
1079 --(*ref_items.ref_count_p);
1080 if (*ref_items.ref_count_p == 0) {
1081 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
1082 g_slice_free(unsigned int, ref_items.ref_count_p);
1083 # else
1084 delete ref_items.ref_count_p;
1085 # endif
1086 ref_items.mutex_p->unlock();
1087 delete ref_items.mutex_p;
1088 deleter(ref_items.obj);
1089 }
1090 else ref_items.mutex_p->unlock();
1091 #else
1092 if (g_atomic_int_dec_and_test(ref_items.ref_count_p)) {
1093 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
1094 g_slice_free(gint, ref_items.ref_count_p);
1095 # else
1096 delete ref_items.ref_count_p;
1097 # endif
1098 deleter(ref_items.obj);
1099 }
1100 #endif
1101 }
1102
1103 // SharedLockHandle<T, Dealloc>::reference() does not throw exceptions because
1104 // Thread::Mutex::Lock::Lock() and Thread::Mutex::Lock::~Lock() do not throw
1105 void reference() noexcept {
1106 // we can (and should) check whether ref_items.ref_count_p is NULL without
1107 // a lock, because that member is specific to this SharedLockHandle object.
1108 // Only the integer pointed to by it is shared amongst SharedLockHandle
1109 // objects and requires locking
1110 if (!ref_items.ref_count_p) return;
1111 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
1112 Thread::Mutex::Lock lock(*ref_items.mutex_p);
1113 ++(*ref_items.ref_count_p);
1114 #else
1115 g_atomic_int_inc(ref_items.ref_count_p);
1116 #endif
1117 }
1118
1119 public:
1120 /**
1121 * Constructor taking an unmanaged object.
1122 * @param ptr The object which the SharedLockHandle is to manage (if
1123 * any).
1124 * @exception std::bad_alloc This constructor will not throw if the
1125 * 'ptr' argument has a NULL value (the default), otherwise it might
1126 * throw std::bad_alloc if memory is exhausted and the system throws
1127 * in that case. If such an exception is thrown, this constructor is
1128 * exception safe (it does not leak resources), but as well as
1129 * cleaning itself up this constructor will also delete the managed
1130 * object passed to it to avoid a memory leak. If such automatic
1131 * deletion is not wanted in that case, use the version of this
1132 * constructor taking a Cgu::SharedHandleAllocFail::Leave tag
1133 * argument.
1134 * @note 1. std::bad_alloc will not be thrown if the library has been
1135 * installed using the \--with-glib-memory-slices-no-compat
1136 * configuration option: instead glib will terminate the program if it
1137 * is unable to obtain memory from the operating system.
1138 * @note 2. By default, glib atomic functions are used to provide
1139 * thread-safe manipulation of the reference count. However, the
1140 * header file shared_handle.h can be textually amended before the
1141 * library is compiled to define the symbol
1142 * CGU_SHARED_LOCK_HANDLE_USE_MUTEX so as to use mutexes instead,
1143 * which might be useful for some debugging purposes. Were that to be
1144 * done, Cgu::Thread::MutexError might be thrown by this constructor
1145 * if initialization of the mutex fails.
1146 */
1147 explicit SharedLockHandle(T ptr = 0) {
1148
1149 if ((ref_items.obj = ptr)) { // not NULL
1150 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
1151 try {
1152 ref_items.mutex_p = new Thread::Mutex;
1153 }
1154 catch (...) {
1155 deleter(ptr); // if allocating the object referenced by ref_items.mutex_p
1156 // has failed then delete the object to be referenced to
1157 // avoid a memory leak
1158 throw;
1159 }
1160 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
1161 ref_items.ref_count_p = g_slice_new(unsigned int);
1162 *ref_items.ref_count_p = 1;
1163 # else
1164 try {
1165 ref_items.ref_count_p = new unsigned int(1);
1166 }
1167 catch (...) {
1168 delete ref_items.mutex_p;
1169 deleter(ptr);
1170 throw;
1171 }
1172 # endif
1173 #else
1174 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
1175 ref_items.ref_count_p = g_slice_new(gint);
1176 *ref_items.ref_count_p = 1;
1177 # else
1178 try {
1179 ref_items.ref_count_p = new gint(1);
1180 }
1181 catch (...) {
1182 deleter(ptr); // if allocating the int referenced by ref_items.ref_count_p
1183 // has failed then delete the object to be referenced to
1184 // avoid a memory leak
1185 throw;
1186 }
1187 # endif
1188 #endif
1189 }
1190 else {
1191 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
1192 ref_items.mutex_p = 0; // make sure the value is valid as we may assign it
1193 #endif
1194 ref_items.ref_count_p = 0;
1195 }
1196 }
1197
1198 /**
1199 * Constructor taking an unmanaged object.
1200 * @param ptr The object which the SharedLockHandle is to manage.
1201 * @param tag Passing the tag emumerator
1202 * Cgu::SharedHandleAllocFail::leave causes this constructor not to
1203 * delete the new managed object passed as the 'ptr' argument in the
1204 * event of internal allocation in this method failing because of
1205 * memory exhaustion (in that event, Cgu::SharedHandleError will be
1206 * thrown).
1207 * @exception Cgu::SharedHandleError This constructor might throw
1208 * Cgu::SharedHandleError if memory is exhausted and the system would
1209 * otherwise throw std::bad_alloc in that case. This constructor is
1210 * exception safe (it does not leak resources), and if such an
1211 * exception is thrown it will clean itself up, but it will not
1212 * attempt to delete the new managed object passed to it. Access to
1213 * the object passed to the 'ptr' argument can be obtained via the
1214 * thrown Cgu::SharedHandleError object.
1215 * @note 1. On systems with over-commit/lazy-commit combined with
1216 * virtual memory (swap), it is rarely useful to check for memory
1217 * exhaustion, so in those cases this version of the constructor will
1218 * not be useful.
1219 * @note 2. If the library has been installed using the
1220 * \--with-glib-memory-slices-no-compat configuration option this
1221 * version of the constructor will also not be useful: instead glib
1222 * will terminate the program if it is unable to obtain memory from
1223 * the operating system.
1224 * @note 3. By default, glib atomic functions are used to provide
1225 * thread-safe manipulation of the reference count. However, the
1226 * header file shared_handle.h can be textually amended before the
1227 * library is compiled to define the symbol
1228 * CGU_SHARED_LOCK_HANDLE_USE_MUTEX so as to use mutexes instead,
1229 * which might be useful for some debugging purposes. Were that to be
1230 * done, Cgu::SharedHandleError might be thrown by this constructor if
1231 * initialization of the mutex fails (even if the
1232 * \--with-glib-memory-slices-no-compat configuration option is
1233 * chosen).
1234 */
1235 SharedLockHandle(T ptr, Cgu::SharedHandleAllocFail::Leave tag) {
1236
1237 if ((ref_items.obj = ptr)) { // not NULL
1238 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
1239 try {
1240 ref_items.mutex_p = new Thread::Mutex;
1241 }
1242 catch (std::bad_alloc&) { // as we are not rethrowing, make NPTL friendly
1243 throw SharedHandleError<T>(ptr);
1244 }
1245 catch (Thread::MutexError&) { // as we are not rethrowing, make NPTL friendly
1246 throw SharedHandleError<T>(ptr);
1247 }
1248 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
1249 ref_items.ref_count_p = g_slice_new(unsigned int);
1250 *ref_items.ref_count_p = 1;
1251 # else
1252 try {
1253 ref_items.ref_count_p = new unsigned int(1);
1254 }
1255 catch (std::bad_alloc&) {
1256 delete ref_items.mutex_p;
1257 throw SharedHandleError<T>(ptr);
1258 }
1259 # endif
1260 #else
1261 # ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
1262 ref_items.ref_count_p = g_slice_new(gint);
1263 *ref_items.ref_count_p = 1;
1264 # else
1265 try {
1266 ref_items.ref_count_p = new gint(1);
1267 }
1268 catch (std::bad_alloc&) { // as we are not rethrowing, make NPTL friendly
1269 throw SharedHandleError<T>(ptr);
1270 }
1271 # endif
1272 #endif
1273 }
1274 else {
1275 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
1276 ref_items.mutex_p = 0; // make sure the value is valid as we may assign it
1277 #endif
1278 ref_items.ref_count_p = 0;
1279 }
1280 }
1281
1282 /**
1283 * Causes the SharedLockHandle to cease to manage its managed object
1284 * (if any), deleting it if this is the last ShareLockHandle object
1285 * managing it. If the argument passed is not NULL, the
1286 * SharedLockHandle object will manage the new object passed (which
1287 * must not be managed by any other SharedLockHandle object).
1288 * @param ptr NULL (the default), or a new unmanaged object to manage.
1289 * @exception std::bad_alloc This method will not throw if the 'ptr'
1290 * argument has a NULL value (the default) and the destructor of a
1291 * managed object does not throw, otherwise it might throw
1292 * std::bad_alloc if memory is exhausted and the system throws in that
1293 * case. Note that if such an exception is thrown then this method
1294 * will do nothing (it is strongly exception safe and will continue to
1295 * manage the object it was managing prior to the call), except that
1296 * it will delete the new managed object passed to it to avoid a
1297 * memory leak. If such automatic deletion in the event of such an
1298 * exception is not wanted, use the reset() method taking a
1299 * Cgu::SharedHandleAllocFail::Leave tag type as its second argument.
1300 * @note 1. std::bad_alloc will not be thrown if the library has been
1301 * installed using the \--with-glib-memory-slices-no-compat
1302 * configuration option: instead glib will terminate the program if it
1303 * is unable to obtain memory from the operating system.
1304 * @note 2. By default, glib atomic functions are used to provide
1305 * thread-safe manipulation of the reference count. However, the
1306 * header file shared_handle.h can be textually amended before the
1307 * library is compiled to define the symbol
1308 * CGU_SHARED_LOCK_HANDLE_USE_MUTEX so as to use mutexes instead,
1309 * which might be useful for some debugging purposes. Were that to be
1310 * done, Cgu::Thread::MutexError might be thrown by this method if
1311 * initialization of the mutex fails.
1312 * @note 3. A SharedLockHandle object protects its reference count but
1313 * not the managed object or its other internals. The reset() method
1314 * should not be called by one thread in respect of a particular
1315 * SharedLockHandle object while another thread may be operating on,
1316 * copying or dereferencing the same instance of SharedLockHandle. It
1317 * is thread-safe as against another instance of SharedLockHandle
1318 * managing the same object.
1319 */
1320 void reset(T ptr = 0) {
1321 SharedLockHandle tmp(ptr);
1322 std::swap(ref_items, tmp.ref_items);
1323 }
1324
1325 /**
1326 * Causes the SharedLockHandle to cease to manage its managed object
1327 * (if any), deleting it if this is the last ShareLockHandle object
1328 * managing it. The SharedLockHandle object will manage the new
1329 * object passed (which must not be managed by any other
1330 * SharedLockHandle object). This method is exception safe, but see
1331 * the comments below on Cgu::SharedHandleError.
1332 * @param ptr A new unmanaged object to manage (if no new object is to
1333 * be managed, use the version of reset() taking a default value of
1334 * NULL).
1335 * @param tag Passing the tag emumerator
1336 * Cgu::SharedHandleAllocFail::leave causes this method not to delete
1337 * the new managed object passed as the 'ptr' argument in the event of
1338 * internal allocation in this method failing because of memory
1339 * exhaustion (in that event, Cgu::SharedHandleError will be thrown).
1340 * @exception Cgu::SharedHandleError This method might throw
1341 * Cgu::SharedHandleError if memory is exhausted and the system would
1342 * otherwise throw std::bad_alloc in that case. Note that if such an
1343 * exception is thrown then this method will do nothing (it is
1344 * strongly exception safe and will continue to manage the object it
1345 * was managing prior to the call), and it will not attempt to delete
1346 * the new managed object passed to it (if any). Access to the object
1347 * passed to the 'ptr' argument can be obtained via the thrown
1348 * Cgu::SharedHandleError object.
1349 * @note 1. A SharedLockHandle object protects its reference count but
1350 * not the managed object or its other internals. The reset() method
1351 * should not be called by one thread in respect of a particular
1352 * SharedLockHandle object while another thread may be operating on,
1353 * copying or dereferencing the same instance of SharedLockHandle. It
1354 * is thread-safe as against another instance of SharedLockHandle
1355 * managing the same object.
1356 * @note 2. On systems with over-commit/lazy-commit combined with
1357 * virtual memory (swap), it is rarely useful to check for memory
1358 * exhaustion, so in those cases this version of the reset() method
1359 * will not be useful.
1360 * @note 3. If the library has been installed using the
1361 * \--with-glib-memory-slices-no-compat configuration option this
1362 * version of the reset() method will also not be useful: instead glib
1363 * will terminate the program if it is unable to obtain memory from
1364 * the operating system.
1365 * @note 4. By default, glib atomic functions are used to provide
1366 * thread-safe manipulation of the reference count. However, the
1367 * header file shared_handle.h can be textually amended before the
1368 * library is compiled to define the symbol
1369 * CGU_SHARED_LOCK_HANDLE_USE_MUTEX so as to use mutexes instead,
1370 * which might be useful for some debugging purposes. Were that to be
1371 * done, Cgu::SharedHandleError might be thrown by this method if
1372 * initialization of the mutex fails (even if the
1373 * \--with-glib-memory-slices-no-compat configuration option is
1374 * chosen).
1375 */
1376 void reset(T ptr, Cgu::SharedHandleAllocFail::Leave tag) {
1377 SharedLockHandle tmp(ptr, tag);
1378 std::swap(ref_items, tmp.ref_items);
1379 }
1380
1381 /**
1382 * The copy constructor does not throw.
1383 * @param sh_hand The handle to be copied.
1384 */
1385 SharedLockHandle(const SharedLockHandle& sh_hand) noexcept {
1386 ref_items = sh_hand.ref_items;
1387 reference();
1388 }
1389
1390 /**
1391 * The move constructor does not throw. It has move semantics.
1392 * @param sh_hand The handle to be moved.
1393 */
1394 SharedLockHandle(SharedLockHandle&& sh_hand) noexcept {
1395 ref_items = sh_hand.ref_items;
1396 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
1397 sh_hand.ref_items.mutex_p = 0; // make sure the value is valid as we may assign it
1398 #endif
1399 sh_hand.ref_items.ref_count_p = 0;
1400 sh_hand.ref_items.obj = 0;
1401 }
1402
1403 /**
1404 * This method (and so copy or move assignment) does not throw unless
1405 * the destructor of a managed object throws.
1406 * @param sh_hand the assignor.
1407 * @return The SharedLockHandle object after assignment.
1408 */
1409 // having a value type as the argument, rather than reference to const
1410 // and then initialising a tmp object, gives the compiler more scope
1411 // for optimisation
1412 SharedLockHandle& operator=(SharedLockHandle sh_hand) {
1413 std::swap(ref_items, sh_hand.ref_items);
1414 return *this;
1415 }
1416
1417 /**
1418 * This method does not throw.
1419 * @return A pointer to the handled object (or NULL if none is
1420 * handled).
1421 */
1422 T get() const noexcept {return ref_items.obj;}
1423
1424 /**
1425 * This method does not throw.
1426 * @return A pointer to the handled object (or NULL if none is
1427 * handled).
1428 */
1429 operator T() const noexcept {return ref_items.obj;}
1430
1431 /**
1432 * This method does not throw.
1433 * @return The number of SharedLockHandle objects referencing the
1434 * managed object (or 0 if none is managed by this SharedLockHandle).
1435 * @note The return value may not be valid if another thread has
1436 * changed the reference count before the value returned by this
1437 * method is acted on. It is provided as a utility, but may not be
1438 * meaningful, depending on the intended usage.
1439 */
1440 unsigned int get_refcount() const noexcept {
1441 if (!ref_items.ref_count_p) return 0;
1442 #ifdef CGU_SHARED_LOCK_HANDLE_USE_MUTEX
1443 Thread::Mutex::Lock lock(*ref_items.mutex_p);
1444 return *ref_items.ref_count_p;
1445 #else
1446 return g_atomic_int_get(ref_items.ref_count_p);
1447 #endif
1448 }
1449
1450 /**
1451 * The destructor does not throw unless the destructor of a handled
1452 * object throws - that should never happen.
1453 */
1454 ~SharedLockHandle() {unreference();}
1455 };
1456
1457 #if defined(CGU_USE_SMART_PTR_COMPARISON) || defined(DOXYGEN_PARSING)
1458
1459 // we can use built-in operator == when comparing pointers referencing
1460 // different objects of the same type
1461 /**
1462 * @ingroup handles
1463 *
1464 * This comparison operator does not throw. It compares the addresses
1465 * of the managed objects.
1466 *
1467 * Since 2.0.0-rc2
1468 */
1469 template <class T, class Dealloc>
1470 bool operator==(const SharedHandle<T, Dealloc>& s1, const SharedHandle<T, Dealloc>& s2) noexcept {
1471 return (s1.get() == s2.get());
1472 }
1473
1474 /**
1475 * @ingroup handles
1476 *
1477 * This comparison operator does not throw. It compares the addresses
1478 * of the managed objects.
1479 *
1480 * Since 2.0.0-rc2
1481 */
1482 template <class T, class Dealloc>
1483 bool operator!=(const SharedHandle<T, Dealloc>& s1, const SharedHandle<T, Dealloc>& s2) noexcept {
1484 return !(s1 == s2);
1485 }
1486
1487 // we must use std::less rather than the < built-in operator for
1488 // pointers to objects not within the same array or object: "For
1489 // templates greater, less, greater_equal, and less_equal, the
1490 // specializations for any pointer type yield a total order, even if
1491 // the built-in operators <, >, <=, >= do not." (para 20.3.3/8).
1492 /**
1493 * @ingroup handles
1494 *
1495 * This comparison operator does not throw unless std::less applied to
1496 * pointer types throws (which it would not do with any sane
1497 * implementation). It compares the addresses of the managed objects.
1498 *
1499 * Since 2.0.0-rc2
1500 */
1501 template <class T, class Dealloc>
1502 bool operator<(const SharedHandle<T, Dealloc>& s1, const SharedHandle<T, Dealloc>& s2) {
1503 return std::less<T>()(s1.get(), s2.get());
1504 }
1505
1506 /**
1507 * @ingroup handles
1508 *
1509 * This comparison operator does not throw. It compares the addresses
1510 * of the managed objects.
1511 *
1512 * Since 2.0.0-rc2
1513 */
1514 template <class T, class Dealloc>
1515 bool operator==(const SharedLockHandle<T, Dealloc>& s1, const SharedLockHandle<T, Dealloc>& s2) noexcept {
1516 return (s1.get() == s2.get());
1517 }
1518
1519 /**
1520 * @ingroup handles
1521 *
1522 * This comparison operator does not throw. It compares the addresses
1523 * of the managed objects.
1524 *
1525 * Since 2.0.0-rc2
1526 */
1527 template <class T, class Dealloc>
1528 bool operator!=(const SharedLockHandle<T, Dealloc>& s1, const SharedLockHandle<T, Dealloc>& s2) noexcept {
1529 return !(s1 == s2);
1530 }
1531
1532 /**
1533 * @ingroup handles
1534 *
1535 * This comparison operator does not throw unless std::less applied to
1536 * pointer types throws (which it would not do with any sane
1537 * implementation). It compares the addresses of the managed objects.
1538 *
1539 * Since 2.0.0-rc2
1540 */
1541 template <class T, class Dealloc>
1542 bool operator<(const SharedLockHandle<T, Dealloc>& s1, const SharedLockHandle<T, Dealloc>& s2) {
1543 return std::less<T>()(s1.get(), s2.get());
1544 }
1545
1546 #endif // CGU_USE_SMART_PTR_COMPARISON
1547
1548 } // namespace Cgu
1549
1550 // doxygen produces long filenames that tar can't handle:
1551 // we have generic documentation for std::hash specialisations
1552 // in doxygen.main.in
1553 #if defined(CGU_USE_SMART_PTR_COMPARISON) && !defined(DOXYGEN_PARSING)
1554 /* These structs allow SharedHandle and SharedLockHandle objects to be
1555 keys in unordered associative containers */
1556 namespace std {
1557 template <class T, class Dealloc>
1558 struct hash<Cgu::SharedHandle<T, Dealloc>> {
1559 typedef std::size_t result_type;
1560 typedef Cgu::SharedHandle<T, Dealloc> argument_type;
1561 result_type operator()(const argument_type& s) const {
1562 // this is fine: std::hash structs do not normally contain data and
1563 // std::hash<T*> certainly won't, so we don't have overhead constructing
1564 // std::hash<T*> on the fly
1565 return std::hash<T>()(s.get());
1566 }
1567 };
1568 template <class T, class Dealloc>
1569 struct hash<Cgu::SharedLockHandle<T, Dealloc>> {
1570 typedef std::size_t result_type;
1571 typedef Cgu::SharedLockHandle<T, Dealloc> argument_type;
1572 result_type operator()(const argument_type& s) const {
1573 // this is fine: std::hash structs do not normally contain data and
1574 // std::hash<T*> certainly won't, so we don't have overhead constructing
1575 // std::hash<T*> on the fly
1576 return std::hash<T>()(s.get());
1577 }
1578 };
1579 } // namespace std
1580 #endif // CGU_USE_SMART_PTR_COMPARISON
1581
1582 #endif