1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
1999-12-13 22:28:37 +00:00
|
|
|
*
|
2011-05-16 20:46:27 +00:00
|
|
|
* Copyright (C) 1997-2011, International Business Machines
|
1999-12-13 22:28:37 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* File CMEMORY.H
|
|
|
|
*
|
|
|
|
* Contains stdlib.h/string.h memory functions
|
|
|
|
*
|
|
|
|
* @author Bertrand A. Damiba
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 6/20/98 Bertrand Created.
|
|
|
|
* 05/03/99 stephen Changed from functions to macros.
|
|
|
|
*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CMEMORY_H
|
|
|
|
#define CMEMORY_H
|
|
|
|
|
2008-10-30 05:20:32 +00:00
|
|
|
#include <stddef.h>
|
1999-08-16 21:50:52 +00:00
|
|
|
#include <string.h>
|
2010-05-18 21:53:21 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/localpointer.h"
|
2000-11-30 20:48:59 +00:00
|
|
|
|
|
|
|
#define uprv_memcpy(dst, src, size) U_STANDARD_CPP_NAMESPACE memcpy(dst, src, size)
|
|
|
|
#define uprv_memmove(dst, src, size) U_STANDARD_CPP_NAMESPACE memmove(dst, src, size)
|
|
|
|
#define uprv_memset(buffer, mark, size) U_STANDARD_CPP_NAMESPACE memset(buffer, mark, size)
|
|
|
|
#define uprv_memcmp(buffer1, buffer2, size) U_STANDARD_CPP_NAMESPACE memcmp(buffer1, buffer2,size)
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-07-29 21:04:18 +00:00
|
|
|
U_CAPI void * U_EXPORT2
|
|
|
|
uprv_malloc(size_t s);
|
|
|
|
|
|
|
|
U_CAPI void * U_EXPORT2
|
|
|
|
uprv_realloc(void *mem, size_t size);
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
uprv_free(void *mem);
|
|
|
|
|
2001-09-26 21:09:18 +00:00
|
|
|
/**
|
|
|
|
* This should align the memory properly on any machine.
|
|
|
|
* This is very useful for the safeClone functions.
|
|
|
|
*/
|
|
|
|
typedef union {
|
|
|
|
long t1;
|
|
|
|
double t2;
|
|
|
|
void *t3;
|
|
|
|
} UAlignedMemory;
|
|
|
|
|
2008-10-30 05:20:32 +00:00
|
|
|
/**
|
|
|
|
* Get the least significant bits of a pointer (a memory address).
|
|
|
|
* For example, with a mask of 3, the macro gets the 2 least significant bits,
|
|
|
|
* which will be 0 if the pointer is 32-bit (4-byte) aligned.
|
|
|
|
*
|
|
|
|
* ptrdiff_t is the most appropriate integer type to cast to.
|
|
|
|
* size_t should work too, since on most (or all?) platforms it has the same
|
|
|
|
* width as ptrdiff_t.
|
|
|
|
*/
|
|
|
|
#define U_POINTER_MASK_LSB(ptr, mask) (((ptrdiff_t)(char *)(ptr)) & (mask))
|
|
|
|
|
2001-09-26 21:09:18 +00:00
|
|
|
/**
|
|
|
|
* Get the amount of bytes that a pointer is off by from
|
2008-10-30 05:20:32 +00:00
|
|
|
* the previous UAlignedMemory-aligned pointer.
|
2001-09-26 21:09:18 +00:00
|
|
|
*/
|
2008-10-30 05:20:32 +00:00
|
|
|
#define U_ALIGNMENT_OFFSET(ptr) U_POINTER_MASK_LSB(ptr, sizeof(UAlignedMemory) - 1)
|
2001-09-26 21:09:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the amount of bytes to add to a pointer
|
2008-10-30 05:20:32 +00:00
|
|
|
* in order to get the next UAlignedMemory-aligned address.
|
2001-09-26 21:09:18 +00:00
|
|
|
*/
|
|
|
|
#define U_ALIGNMENT_OFFSET_UP(ptr) (sizeof(UAlignedMemory) - U_ALIGNMENT_OFFSET(ptr))
|
|
|
|
|
2003-08-05 01:25:54 +00:00
|
|
|
/**
|
|
|
|
* Indicate whether the ICU allocation functions have been used.
|
|
|
|
* This is used to determine whether ICU is in an initial, unused state.
|
|
|
|
*/
|
|
|
|
U_CFUNC UBool
|
2003-11-27 01:14:37 +00:00
|
|
|
cmemory_inUse(void);
|
2003-08-05 01:25:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Heap clean up function, called from u_cleanup()
|
|
|
|
* Clears any user heap functions from u_setMemoryFunctions()
|
|
|
|
* Does NOT deallocate any remaining allocated memory.
|
|
|
|
*/
|
|
|
|
U_CFUNC UBool
|
|
|
|
cmemory_cleanup(void);
|
|
|
|
|
2011-06-03 05:23:57 +00:00
|
|
|
/**
|
|
|
|
* A function called by <TT>uhash_remove</TT>,
|
|
|
|
* <TT>uhash_close</TT>, or <TT>uhash_put</TT> to delete
|
|
|
|
* an existing key or value.
|
|
|
|
* @param obj A key or value stored in a hashtable
|
|
|
|
* @see uprv_deleteUObject
|
|
|
|
*/
|
|
|
|
typedef void U_CALLCONV UObjectDeleter(void* obj);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deleter for UObject instances.
|
|
|
|
* Works for all subclasses of UObject because it has a virtual destructor.
|
|
|
|
*/
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
uprv_deleteUObject(void *obj);
|
|
|
|
|
2009-11-18 23:35:52 +00:00
|
|
|
#ifdef XP_CPLUSPLUS
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/**
|
|
|
|
* "Smart pointer" class, deletes memory via uprv_free().
|
|
|
|
* For most methods see the LocalPointerBase base class.
|
|
|
|
* Adds operator[] for array item access.
|
|
|
|
*
|
|
|
|
* @see LocalPointerBase
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
class LocalMemory : public LocalPointerBase<T> {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor takes ownership.
|
|
|
|
* @param p simple pointer to an array of T items that is adopted
|
|
|
|
*/
|
|
|
|
explicit LocalMemory(T *p=NULL) : LocalPointerBase<T>(p) {}
|
|
|
|
/**
|
|
|
|
* Destructor deletes the memory it owns.
|
|
|
|
*/
|
|
|
|
~LocalMemory() {
|
|
|
|
uprv_free(LocalPointerBase<T>::ptr);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Deletes the array it owns,
|
|
|
|
* and adopts (takes ownership of) the one passed in.
|
|
|
|
* @param p simple pointer to an array of T items that is adopted
|
|
|
|
*/
|
|
|
|
void adoptInstead(T *p) {
|
|
|
|
uprv_free(LocalPointerBase<T>::ptr);
|
|
|
|
LocalPointerBase<T>::ptr=p;
|
|
|
|
}
|
2009-11-20 06:28:25 +00:00
|
|
|
/**
|
|
|
|
* Deletes the array it owns, allocates a new one and reset its bytes to 0.
|
|
|
|
* Returns the new array pointer.
|
|
|
|
* If the allocation fails, then the current array is unchanged and
|
|
|
|
* this method returns NULL.
|
|
|
|
* @param newCapacity must be >0
|
|
|
|
* @return the allocated array pointer, or NULL if the allocation failed
|
|
|
|
*/
|
|
|
|
inline T *allocateInsteadAndReset(int32_t newCapacity=1);
|
|
|
|
/**
|
|
|
|
* Deletes the array it owns and allocates a new one, copying length T items.
|
|
|
|
* Returns the new array pointer.
|
|
|
|
* If the allocation fails, then the current array is unchanged and
|
|
|
|
* this method returns NULL.
|
|
|
|
* @param newCapacity must be >0
|
|
|
|
* @param length number of T items to be copied from the old array to the new one;
|
|
|
|
* must be no more than the capacity of the old array,
|
|
|
|
* which the caller must track because the LocalMemory does not track it
|
|
|
|
* @return the allocated array pointer, or NULL if the allocation failed
|
|
|
|
*/
|
|
|
|
inline T *allocateInsteadAndCopy(int32_t newCapacity=1, int32_t length=0);
|
2009-11-18 23:35:52 +00:00
|
|
|
/**
|
|
|
|
* Array item access (writable).
|
|
|
|
* No index bounds check.
|
|
|
|
* @param i array index
|
|
|
|
* @return reference to the array item
|
|
|
|
*/
|
|
|
|
T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
|
|
|
|
};
|
|
|
|
|
2010-05-18 21:53:21 +00:00
|
|
|
template<typename T>
|
|
|
|
inline T *LocalMemory<T>::allocateInsteadAndReset(int32_t newCapacity) {
|
|
|
|
if(newCapacity>0) {
|
|
|
|
T *p=(T *)uprv_malloc(newCapacity*sizeof(T));
|
|
|
|
if(p!=NULL) {
|
|
|
|
uprv_memset(p, 0, newCapacity*sizeof(T));
|
|
|
|
uprv_free(LocalPointerBase<T>::ptr);
|
|
|
|
LocalPointerBase<T>::ptr=p;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T *LocalMemory<T>::allocateInsteadAndCopy(int32_t newCapacity, int32_t length) {
|
|
|
|
if(newCapacity>0) {
|
|
|
|
T *p=(T *)uprv_malloc(newCapacity*sizeof(T));
|
|
|
|
if(p!=NULL) {
|
|
|
|
if(length>0) {
|
|
|
|
if(length>newCapacity) {
|
|
|
|
length=newCapacity;
|
|
|
|
}
|
|
|
|
uprv_memcpy(p, LocalPointerBase<T>::ptr, length*sizeof(T));
|
|
|
|
}
|
|
|
|
uprv_free(LocalPointerBase<T>::ptr);
|
|
|
|
LocalPointerBase<T>::ptr=p;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-18 23:35:52 +00:00
|
|
|
/**
|
|
|
|
* Simple array/buffer management class using uprv_malloc() and uprv_free().
|
|
|
|
* Provides an internal array with fixed capacity. Can alias another array
|
|
|
|
* or allocate one.
|
2010-05-18 21:53:21 +00:00
|
|
|
*
|
|
|
|
* The array address is properly aligned for type T. It might not be properly
|
|
|
|
* aligned for types larger than T (or larger than the largest subtype of T).
|
|
|
|
*
|
2009-11-18 23:35:52 +00:00
|
|
|
* Unlike LocalMemory and LocalArray, this class never adopts
|
|
|
|
* (takes ownership of) another array.
|
|
|
|
*/
|
|
|
|
template<typename T, int32_t stackCapacity>
|
|
|
|
class MaybeStackArray {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Default constructor initializes with internal T[stackCapacity] buffer.
|
|
|
|
*/
|
2010-05-18 21:53:21 +00:00
|
|
|
MaybeStackArray() : ptr(stackArray), capacity(stackCapacity), needToRelease(FALSE) {}
|
2009-11-18 23:35:52 +00:00
|
|
|
/**
|
|
|
|
* Destructor deletes the array (if owned).
|
|
|
|
*/
|
|
|
|
~MaybeStackArray() { releaseArray(); }
|
|
|
|
/**
|
|
|
|
* Returns the array capacity (number of T items).
|
|
|
|
* @return array capacity
|
|
|
|
*/
|
|
|
|
int32_t getCapacity() const { return capacity; }
|
|
|
|
/**
|
|
|
|
* Access without ownership change.
|
|
|
|
* @return the array pointer
|
|
|
|
*/
|
|
|
|
T *getAlias() const { return ptr; }
|
|
|
|
/**
|
|
|
|
* Returns the array limit. Simple convenience method.
|
|
|
|
* @return getAlias()+getCapacity()
|
|
|
|
*/
|
|
|
|
T *getArrayLimit() const { return getAlias()+capacity; }
|
|
|
|
/**
|
|
|
|
* Access without ownership change. Same as getAlias().
|
|
|
|
* A class instance can be used directly in expressions that take a T *.
|
|
|
|
* @return the array pointer
|
|
|
|
*/
|
|
|
|
operator T *() const { return ptr; }
|
|
|
|
/**
|
|
|
|
* Array item access (writable).
|
|
|
|
* No index bounds check.
|
|
|
|
* @param i array index
|
|
|
|
* @return reference to the array item
|
|
|
|
*/
|
|
|
|
T &operator[](ptrdiff_t i) { return ptr[i]; }
|
|
|
|
/**
|
|
|
|
* Deletes the array (if owned) and aliases another one, no transfer of ownership.
|
|
|
|
* If the arguments are illegal, then the current array is unchanged.
|
|
|
|
* @param otherArray must not be NULL
|
|
|
|
* @param otherCapacity must be >0
|
|
|
|
*/
|
|
|
|
void aliasInstead(T *otherArray, int32_t otherCapacity) {
|
|
|
|
if(otherArray!=NULL && otherCapacity>0) {
|
|
|
|
releaseArray();
|
|
|
|
ptr=otherArray;
|
|
|
|
capacity=otherCapacity;
|
|
|
|
needToRelease=FALSE;
|
|
|
|
}
|
2011-07-06 20:05:38 +00:00
|
|
|
}
|
2009-11-18 23:35:52 +00:00
|
|
|
/**
|
|
|
|
* Deletes the array (if owned) and allocates a new one, copying length T items.
|
|
|
|
* Returns the new array pointer.
|
|
|
|
* If the allocation fails, then the current array is unchanged and
|
|
|
|
* this method returns NULL.
|
|
|
|
* @param newCapacity can be less than or greater than the current capacity;
|
|
|
|
* must be >0
|
|
|
|
* @param length number of T items to be copied from the old array to the new one
|
|
|
|
* @return the allocated array pointer, or NULL if the allocation failed
|
|
|
|
*/
|
2009-11-20 06:28:25 +00:00
|
|
|
inline T *resize(int32_t newCapacity, int32_t length=0);
|
2009-11-18 23:35:52 +00:00
|
|
|
/**
|
|
|
|
* Gives up ownership of the array if owned, or else clones it,
|
|
|
|
* copying length T items; resets itself to the internal stack array.
|
|
|
|
* Returns NULL if the allocation failed.
|
|
|
|
* @param length number of T items to copy when cloning,
|
|
|
|
* and capacity of the clone when cloning
|
|
|
|
* @param resultCapacity will be set to the returned array's capacity (output-only)
|
|
|
|
* @return the array pointer;
|
|
|
|
* caller becomes responsible for deleting the array
|
|
|
|
*/
|
2009-11-20 06:28:25 +00:00
|
|
|
inline T *orphanOrClone(int32_t length, int32_t &resultCapacity);
|
2009-11-18 23:35:52 +00:00
|
|
|
private:
|
2010-02-27 18:10:23 +00:00
|
|
|
T *ptr;
|
2010-05-18 21:53:21 +00:00
|
|
|
int32_t capacity;
|
|
|
|
UBool needToRelease;
|
2009-11-18 23:35:52 +00:00
|
|
|
T stackArray[stackCapacity];
|
|
|
|
void releaseArray() {
|
|
|
|
if(needToRelease) {
|
|
|
|
uprv_free(ptr);
|
|
|
|
}
|
|
|
|
}
|
2009-12-03 22:41:48 +00:00
|
|
|
/* No comparison operators with other MaybeStackArray's. */
|
2011-07-06 20:05:38 +00:00
|
|
|
bool operator==(const MaybeStackArray & /*other*/) {return FALSE;}
|
|
|
|
bool operator!=(const MaybeStackArray & /*other*/) {return TRUE;}
|
2009-12-03 22:41:48 +00:00
|
|
|
/* No ownership transfer: No copy constructor, no assignment operator. */
|
2011-07-06 20:05:38 +00:00
|
|
|
MaybeStackArray(const MaybeStackArray & /*other*/) {}
|
|
|
|
void operator=(const MaybeStackArray & /*other*/) {}
|
2010-02-26 02:29:00 +00:00
|
|
|
|
|
|
|
// No heap allocation. Use only on the stack.
|
|
|
|
// (Declaring these functions private triggers a cascade of problems:
|
|
|
|
// MSVC insists on exporting an instantiation of MaybeStackArray, which
|
|
|
|
// requires that all functions be defined.
|
|
|
|
// An empty implementation of new() is rejected, it must return a value.
|
|
|
|
// Returning NULL is rejected by gcc for operator new.
|
|
|
|
// The expedient thing is just not to override operator new.
|
|
|
|
// While relatively pointless, heap allocated instances will function.
|
|
|
|
// static void * U_EXPORT2 operator new(size_t size);
|
|
|
|
// static void * U_EXPORT2 operator new[](size_t size);
|
2009-11-18 23:35:52 +00:00
|
|
|
#if U_HAVE_PLACEMENT_NEW
|
2010-02-26 02:29:00 +00:00
|
|
|
// static void * U_EXPORT2 operator new(size_t, void *ptr);
|
1999-08-16 21:50:52 +00:00
|
|
|
#endif
|
2009-11-18 23:35:52 +00:00
|
|
|
};
|
|
|
|
|
2009-11-20 06:28:25 +00:00
|
|
|
template<typename T, int32_t stackCapacity>
|
|
|
|
inline T *MaybeStackArray<T, stackCapacity>::resize(int32_t newCapacity, int32_t length) {
|
|
|
|
if(newCapacity>0) {
|
|
|
|
T *p=(T *)uprv_malloc(newCapacity*sizeof(T));
|
|
|
|
if(p!=NULL) {
|
|
|
|
if(length>0) {
|
|
|
|
if(length>capacity) {
|
|
|
|
length=capacity;
|
|
|
|
}
|
|
|
|
if(length>newCapacity) {
|
|
|
|
length=newCapacity;
|
|
|
|
}
|
|
|
|
uprv_memcpy(p, ptr, length*sizeof(T));
|
|
|
|
}
|
|
|
|
releaseArray();
|
|
|
|
ptr=p;
|
|
|
|
capacity=newCapacity;
|
|
|
|
needToRelease=TRUE;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, int32_t stackCapacity>
|
|
|
|
inline T *MaybeStackArray<T, stackCapacity>::orphanOrClone(int32_t length, int32_t &resultCapacity) {
|
|
|
|
T *p;
|
|
|
|
if(needToRelease) {
|
|
|
|
p=ptr;
|
|
|
|
} else if(length<=0) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
if(length>capacity) {
|
|
|
|
length=capacity;
|
|
|
|
}
|
|
|
|
p=(T *)uprv_malloc(length*sizeof(T));
|
|
|
|
if(p==NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
uprv_memcpy(p, ptr, length*sizeof(T));
|
|
|
|
}
|
|
|
|
resultCapacity=length;
|
|
|
|
ptr=stackArray;
|
|
|
|
capacity=stackCapacity;
|
|
|
|
needToRelease=FALSE;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2010-05-18 21:53:21 +00:00
|
|
|
/**
|
|
|
|
* Variant of MaybeStackArray that allocates a header struct and an array
|
|
|
|
* in one contiguous memory block, using uprv_malloc() and uprv_free().
|
|
|
|
* Provides internal memory with fixed array capacity. Can alias another memory
|
|
|
|
* block or allocate one.
|
|
|
|
* The stackCapacity is the number of T items in the internal memory,
|
|
|
|
* not counting the H header.
|
|
|
|
* Unlike LocalMemory and LocalArray, this class never adopts
|
|
|
|
* (takes ownership of) another memory block.
|
|
|
|
*/
|
|
|
|
template<typename H, typename T, int32_t stackCapacity>
|
|
|
|
class MaybeStackHeaderAndArray {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Default constructor initializes with internal H+T[stackCapacity] buffer.
|
|
|
|
*/
|
|
|
|
MaybeStackHeaderAndArray() : ptr(&stackHeader), capacity(stackCapacity), needToRelease(FALSE) {}
|
|
|
|
/**
|
|
|
|
* Destructor deletes the memory (if owned).
|
|
|
|
*/
|
|
|
|
~MaybeStackHeaderAndArray() { releaseMemory(); }
|
|
|
|
/**
|
|
|
|
* Returns the array capacity (number of T items).
|
|
|
|
* @return array capacity
|
|
|
|
*/
|
|
|
|
int32_t getCapacity() const { return capacity; }
|
|
|
|
/**
|
|
|
|
* Access without ownership change.
|
|
|
|
* @return the header pointer
|
|
|
|
*/
|
|
|
|
H *getAlias() const { return ptr; }
|
|
|
|
/**
|
|
|
|
* Returns the array start.
|
|
|
|
* @return array start, same address as getAlias()+1
|
|
|
|
*/
|
|
|
|
T *getArrayStart() const { return reinterpret_cast<T *>(getAlias()+1); }
|
|
|
|
/**
|
|
|
|
* Returns the array limit.
|
|
|
|
* @return array limit
|
|
|
|
*/
|
|
|
|
T *getArrayLimit() const { return getArrayStart()+capacity; }
|
|
|
|
/**
|
|
|
|
* Access without ownership change. Same as getAlias().
|
|
|
|
* A class instance can be used directly in expressions that take a T *.
|
|
|
|
* @return the header pointer
|
|
|
|
*/
|
|
|
|
operator H *() const { return ptr; }
|
|
|
|
/**
|
|
|
|
* Array item access (writable).
|
|
|
|
* No index bounds check.
|
|
|
|
* @param i array index
|
|
|
|
* @return reference to the array item
|
|
|
|
*/
|
|
|
|
T &operator[](ptrdiff_t i) { return getArrayStart()[i]; }
|
|
|
|
/**
|
|
|
|
* Deletes the memory block (if owned) and aliases another one, no transfer of ownership.
|
|
|
|
* If the arguments are illegal, then the current memory is unchanged.
|
|
|
|
* @param otherArray must not be NULL
|
|
|
|
* @param otherCapacity must be >0
|
|
|
|
*/
|
|
|
|
void aliasInstead(H *otherMemory, int32_t otherCapacity) {
|
|
|
|
if(otherMemory!=NULL && otherCapacity>0) {
|
|
|
|
releaseMemory();
|
|
|
|
ptr=otherMemory;
|
|
|
|
capacity=otherCapacity;
|
|
|
|
needToRelease=FALSE;
|
|
|
|
}
|
2011-07-06 20:05:38 +00:00
|
|
|
}
|
2010-05-18 21:53:21 +00:00
|
|
|
/**
|
|
|
|
* Deletes the memory block (if owned) and allocates a new one,
|
|
|
|
* copying the header and length T array items.
|
|
|
|
* Returns the new header pointer.
|
|
|
|
* If the allocation fails, then the current memory is unchanged and
|
|
|
|
* this method returns NULL.
|
|
|
|
* @param newCapacity can be less than or greater than the current capacity;
|
|
|
|
* must be >0
|
|
|
|
* @param length number of T items to be copied from the old array to the new one
|
|
|
|
* @return the allocated pointer, or NULL if the allocation failed
|
|
|
|
*/
|
|
|
|
inline H *resize(int32_t newCapacity, int32_t length=0);
|
|
|
|
/**
|
|
|
|
* Gives up ownership of the memory if owned, or else clones it,
|
|
|
|
* copying the header and length T array items; resets itself to the internal memory.
|
|
|
|
* Returns NULL if the allocation failed.
|
|
|
|
* @param length number of T items to copy when cloning,
|
|
|
|
* and array capacity of the clone when cloning
|
|
|
|
* @param resultCapacity will be set to the returned array's capacity (output-only)
|
|
|
|
* @return the header pointer;
|
|
|
|
* caller becomes responsible for deleting the array
|
|
|
|
*/
|
|
|
|
inline H *orphanOrClone(int32_t length, int32_t &resultCapacity);
|
|
|
|
private:
|
|
|
|
H *ptr;
|
|
|
|
int32_t capacity;
|
|
|
|
UBool needToRelease;
|
|
|
|
// stackHeader must precede stackArray immediately.
|
|
|
|
H stackHeader;
|
|
|
|
T stackArray[stackCapacity];
|
|
|
|
void releaseMemory() {
|
|
|
|
if(needToRelease) {
|
|
|
|
uprv_free(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* No comparison operators with other MaybeStackHeaderAndArray's. */
|
2011-07-06 20:05:38 +00:00
|
|
|
bool operator==(const MaybeStackHeaderAndArray & /*other*/) {return FALSE;}
|
|
|
|
bool operator!=(const MaybeStackHeaderAndArray & /*other*/) {return TRUE;}
|
2010-05-18 21:53:21 +00:00
|
|
|
/* No ownership transfer: No copy constructor, no assignment operator. */
|
2011-07-06 20:05:38 +00:00
|
|
|
MaybeStackHeaderAndArray(const MaybeStackHeaderAndArray & /*other*/) {}
|
|
|
|
void operator=(const MaybeStackHeaderAndArray & /*other*/) {}
|
2010-05-18 21:53:21 +00:00
|
|
|
|
|
|
|
// No heap allocation. Use only on the stack.
|
|
|
|
// (Declaring these functions private triggers a cascade of problems;
|
|
|
|
// see the MaybeStackArray class for details.)
|
|
|
|
// 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
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename H, typename T, int32_t stackCapacity>
|
|
|
|
inline H *MaybeStackHeaderAndArray<H, T, stackCapacity>::resize(int32_t newCapacity,
|
|
|
|
int32_t length) {
|
|
|
|
if(newCapacity>=0) {
|
|
|
|
H *p=(H *)uprv_malloc(sizeof(H)+newCapacity*sizeof(T));
|
|
|
|
if(p!=NULL) {
|
|
|
|
if(length<0) {
|
|
|
|
length=0;
|
|
|
|
} else if(length>0) {
|
|
|
|
if(length>capacity) {
|
|
|
|
length=capacity;
|
|
|
|
}
|
|
|
|
if(length>newCapacity) {
|
|
|
|
length=newCapacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uprv_memcpy(p, ptr, sizeof(H)+length*sizeof(T));
|
|
|
|
releaseMemory();
|
|
|
|
ptr=p;
|
|
|
|
capacity=newCapacity;
|
|
|
|
needToRelease=TRUE;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename H, typename T, int32_t stackCapacity>
|
|
|
|
inline H *MaybeStackHeaderAndArray<H, T, stackCapacity>::orphanOrClone(int32_t length,
|
|
|
|
int32_t &resultCapacity) {
|
|
|
|
H *p;
|
|
|
|
if(needToRelease) {
|
|
|
|
p=ptr;
|
|
|
|
} else {
|
|
|
|
if(length<0) {
|
|
|
|
length=0;
|
|
|
|
} else if(length>capacity) {
|
|
|
|
length=capacity;
|
|
|
|
}
|
|
|
|
p=(H *)uprv_malloc(sizeof(H)+length*sizeof(T));
|
|
|
|
if(p==NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
uprv_memcpy(p, ptr, sizeof(H)+length*sizeof(T));
|
|
|
|
}
|
|
|
|
resultCapacity=length;
|
|
|
|
ptr=&stackHeader;
|
|
|
|
capacity=stackCapacity;
|
|
|
|
needToRelease=FALSE;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2009-11-18 23:35:52 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* XP_CPLUSPLUS */
|
|
|
|
#endif /* CMEMORY_H */
|