2013-11-22 18:26:22 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2014-01-11 00:30:39 +00:00
|
|
|
* Copyright (C) 2014, International Business Machines Corporation and
|
2013-11-22 18:26:22 +00:00
|
|
|
* others. All Rights Reserved.
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* File SHAREDPTR.H
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __SHARED_PTR_H__
|
|
|
|
#define __SHARED_PTR_H__
|
|
|
|
|
|
|
|
#include "unicode/uobject.h"
|
|
|
|
#include "umutex.h"
|
|
|
|
#include "uassert.h"
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
// Wrap u_atomic_int32_t in a UMemory so that we allocate them in the same
|
|
|
|
// way we allocate all other ICU objects.
|
2014-01-14 22:39:13 +00:00
|
|
|
struct AtomicInt : public UMemory {
|
2013-11-22 18:26:22 +00:00
|
|
|
u_atomic_int32_t value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SharedPtr are shared pointers that support copy-on-write sematics.
|
|
|
|
* SharedPtr makes the act of copying large objects cheap by deferring the
|
|
|
|
* cost of the copy to the first write operation after the copy.
|
|
|
|
*
|
2014-01-11 00:30:39 +00:00
|
|
|
* A SharedPtr<T> instance can refer to no object or an object of type T.
|
|
|
|
* T must have a clone() method that copies
|
2013-11-22 18:26:22 +00:00
|
|
|
* the object and returns a pointer to the copy. Copy and assignment of
|
|
|
|
* SharedPtr instances are cheap because they only involve copying or
|
|
|
|
* assigning the SharedPtr instance, not the T object which could be large.
|
|
|
|
* Although many SharedPtr<T> instances may refer to the same T object,
|
|
|
|
* clients can still assume that each SharedPtr<T> instance has its own
|
|
|
|
* private instance of T because each SharedPtr<T> instance offers only a
|
|
|
|
* const view of its T object through normal pointer operations. If a caller
|
|
|
|
* must change a T object through its SharedPtr<T>, it can do so by calling
|
|
|
|
* readWrite() on the SharedPtr instance. readWrite() ensures that the
|
|
|
|
* SharedPtr<T> really does have its own private T object by cloning it if
|
|
|
|
* it is shared by using its clone() method. SharedPtr<T> instances handle
|
|
|
|
* management by reference counting their T objects. T objects that are
|
|
|
|
* referenced by no SharedPtr<T> instances get deleted automatically.
|
|
|
|
*/
|
2014-01-11 00:30:39 +00:00
|
|
|
|
|
|
|
// TODO (Travis Keep): Leave interface the same, but find a more efficient
|
|
|
|
// implementation that is easier to understand.
|
2013-11-22 18:26:22 +00:00
|
|
|
template<typename T>
|
|
|
|
class SharedPtr {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor. If there is a memory allocation error creating
|
|
|
|
* reference counter then this object will contain NULL, and adopted
|
|
|
|
* pointer will be freed. Note that when passing NULL or no argument to
|
|
|
|
* constructor, no memory allocation error can happen as NULL pointers
|
|
|
|
* are never reference counted.
|
|
|
|
*/
|
|
|
|
explicit SharedPtr(T *adopted=NULL) : ptr(adopted), refPtr(NULL) {
|
|
|
|
if (ptr != NULL) {
|
2014-01-14 22:39:13 +00:00
|
|
|
refPtr = new AtomicInt();
|
2013-11-22 18:26:22 +00:00
|
|
|
if (refPtr == NULL) {
|
|
|
|
delete ptr;
|
|
|
|
ptr = NULL;
|
|
|
|
} else {
|
2014-01-11 00:30:39 +00:00
|
|
|
refPtr->value = 1;
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-01-14 22:39:13 +00:00
|
|
|
* Copy constructor.
|
2013-11-22 18:26:22 +00:00
|
|
|
*/
|
|
|
|
SharedPtr(const SharedPtr<T> &other) :
|
|
|
|
ptr(other.ptr), refPtr(other.refPtr) {
|
|
|
|
if (refPtr != NULL) {
|
|
|
|
umtx_atomic_inc(&refPtr->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-01-14 22:39:13 +00:00
|
|
|
* assignment operator.
|
2013-11-22 18:26:22 +00:00
|
|
|
*/
|
|
|
|
SharedPtr<T> &operator=(const SharedPtr<T> &other) {
|
|
|
|
if (ptr != other.ptr) {
|
|
|
|
SharedPtr<T> newValue(other);
|
|
|
|
swap(newValue);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
~SharedPtr() {
|
|
|
|
if (refPtr != NULL) {
|
|
|
|
if (umtx_atomic_dec(&refPtr->value) == 0) {
|
2014-01-11 00:30:39 +00:00
|
|
|
delete ptr;
|
2013-11-22 18:26:22 +00:00
|
|
|
delete refPtr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-01-14 22:39:13 +00:00
|
|
|
* reset adopts a new pointer. On success, returns TRUE.
|
2013-11-22 18:26:22 +00:00
|
|
|
* On memory allocation error creating reference counter for adopted
|
|
|
|
* pointer, returns FALSE while leaving this instance unchanged.
|
|
|
|
*/
|
2014-01-14 22:39:13 +00:00
|
|
|
bool reset(T *adopted) {
|
2013-11-22 18:26:22 +00:00
|
|
|
SharedPtr<T> newValue(adopted);
|
|
|
|
if (adopted != NULL && newValue.ptr == NULL) {
|
|
|
|
// We couldn't allocate ref counter.
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
swap(newValue);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-01-14 22:39:13 +00:00
|
|
|
* reset makes this instance refer to no object.
|
2013-11-22 18:26:22 +00:00
|
|
|
*/
|
2014-01-14 22:39:13 +00:00
|
|
|
void reset() {
|
|
|
|
reset(NULL);
|
2013-11-22 18:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* count returns how many SharedPtr instances, including this one,
|
|
|
|
* refer to the T object. Used for testing. Clients need not use in
|
|
|
|
* practice.
|
|
|
|
*/
|
|
|
|
int32_t count() const {
|
|
|
|
if (refPtr == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return umtx_loadAcquire(refPtr->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-01-14 22:39:13 +00:00
|
|
|
* Swaps this instance with other.
|
2013-11-22 18:26:22 +00:00
|
|
|
*/
|
|
|
|
void swap(SharedPtr<T> &other) {
|
|
|
|
T *tempPtr = other.ptr;
|
2014-01-14 22:39:13 +00:00
|
|
|
AtomicInt *tempRefPtr = other.refPtr;
|
2013-11-22 18:26:22 +00:00
|
|
|
other.ptr = ptr;
|
|
|
|
other.refPtr = refPtr;
|
|
|
|
ptr = tempPtr;
|
|
|
|
refPtr = tempRefPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T *operator->() const {
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T &operator*() const {
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const T *other) const {
|
|
|
|
return ptr == other;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const T *other) const {
|
|
|
|
return ptr != other;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* readOnly gives const access to this instance's T object. If this
|
|
|
|
* instance refers to no object, returns NULL.
|
|
|
|
*/
|
|
|
|
const T *readOnly() const {
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* readWrite returns a writable pointer to its T object copying it first
|
|
|
|
* using its clone() method if it is shared.
|
|
|
|
* On memory allocation error or if this instance refers to no object,
|
2014-01-14 22:39:13 +00:00
|
|
|
* this method returns NULL leaving this instance unchanged.
|
|
|
|
* <p>
|
|
|
|
* If readWrite() returns a non NULL pointer, it guarantees that this
|
|
|
|
* object holds the only reference to its T object enabling the caller to
|
|
|
|
* perform mutations using the returned pointer without affecting other
|
|
|
|
* SharedPtr objects. However, the non-constness of readWrite continues as
|
|
|
|
* long as the returned pointer is in scope. Therefore it is an API
|
|
|
|
* violation to call readWrite() on A; perform B = A; and then proceed to
|
|
|
|
* mutate A via its writeable pointer as that would be the same as setting
|
|
|
|
* B = A while A is changing. The returned pointer is guaranteed to be
|
|
|
|
* valid only while this object is in scope because this object maintains
|
|
|
|
* ownership of its T object. Therefore, callers must never attempt to
|
|
|
|
* delete the returned writeable pointer. The best practice with readWrite
|
|
|
|
* is this: callers should use the returned pointer from readWrite() only
|
|
|
|
* within the same scope as that call to readWrite, and that scope should
|
|
|
|
* be made as small as possible avoiding overlap with other operatios on
|
|
|
|
* this object.
|
2013-11-22 18:26:22 +00:00
|
|
|
*/
|
|
|
|
T *readWrite() {
|
|
|
|
int32_t refCount = count();
|
2014-01-11 00:30:39 +00:00
|
|
|
if (refCount <= 1) {
|
2013-11-22 18:26:22 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
T *result = (T *) ptr->clone();
|
|
|
|
if (result == NULL) {
|
|
|
|
// Memory allocation error
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-01-14 22:39:13 +00:00
|
|
|
if (!reset(result)) {
|
2013-11-22 18:26:22 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
T *ptr;
|
2014-01-14 22:39:13 +00:00
|
|
|
AtomicInt *refPtr;
|
2013-11-22 18:26:22 +00:00
|
|
|
// No heap allocation. Use only stack.
|
|
|
|
static void * U_EXPORT2 operator new(size_t size);
|
|
|
|
static void * U_EXPORT2 operator new[](size_t size);
|
|
|
|
#if U_HAVE_PLACEMENT_NEW
|
|
|
|
static void * U_EXPORT2 operator new(size_t, void *ptr);
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif
|