ICU-2244 split UMemory off of UObject

X-SVN-Rev: 9951
This commit is contained in:
Markus Scherer 2002-10-03 01:13:01 +00:00
parent 097be2462d
commit 7b22f218e3
2 changed files with 68 additions and 44 deletions

View file

@ -27,20 +27,20 @@ U_NAMESPACE_BEGIN
*/
/** U_OVERRIDE_CXX_ALLOCATION - Define this to override operator new and
* delete in UObject. Enabled by default for ICU.
* delete in UMemory. Enabled by default for ICU.
*
* Enabling forces all allocation of ICU object types to use ICU's
* memory allocation. On Windows, this allows the ICU DLL to be used by
* memory allocation. On Windows, this allows the ICU DLL to be used by
* applications that statically link the C Runtime library, meaning that
* the app and ICU sill be using different heaps.
* the app and ICU will be using different heaps.
*/
#ifndef U_OVERRIDE_CXX_ALLOCATION
#define U_OVERRIDE_CXX_ALLOCATION 1
#endif
/**
* UObject is the common ICU base class.
* All other ICU C++ classes are derived from UObject (starting with ICU 2.2).
* UMemory is the common ICU base class.
* All other ICU C++ classes are derived from UMemory (starting with ICU 2.4).
*
* This is primarily to make it possible and simple to override the
* C++ memory management by adding new/delete operators to this base class.
@ -48,16 +48,61 @@ U_NAMESPACE_BEGIN
* To override ALL ICU memory management, including that from plain C code,
* replace the allocation functions declared in cmemory.h
*
* UObject does not contain default implementations of virtual methods
* UMemory does not contain any virtual functions.
* Common "boilerplate" functions are defined in UObject.
*
* @draft ICU 2.4
*/
class U_COMMON_API UMemory {
public:
#if U_OVERRIDE_CXX_ALLOCATION
/**
* Override for ICU4C C++ memory management.
* simple, non-class types are allocated using the macros in common/cmemory.h
* (uprv_malloc(), uprv_free(), uprv_realloc());
* they or something else could be used here to implement C++ new/delete
* for ICU4C C++ classes
* @draft ICU 2.4
*/
void *operator new(size_t size);
/**
* Override for ICU4C C++ memory management.
* See new().
* @draft ICU 2.4
*/
void *operator new[](size_t size);
/**
* Override for ICU4C C++ memory management.
* simple, non-class types are allocated using the macros in common/cmemory.h
* (uprv_malloc(), uprv_free(), uprv_realloc());
* they or something else could be used here to implement C++ new/delete
* for ICU4C C++ classes
* @draft ICU 2.4
*/
void operator delete(void *p);
/**
* Override for ICU4C C++ memory management.
* See delete().
* @draft ICU 2.4
*/
void operator delete[](void *p);
#endif
};
/**
* UObject is the common ICU "boilerplate" class.
* UObject inherits UMemory, and all other public ICU C++ classes
* are derived from UObject (starting with ICU 2.2).
*
* UObject contains common virtual functions like for ICU's "poor man's RTTI".
* It does not contain default implementations of virtual methods
* like getDynamicClassID to allow derived classes such as Format
* to declare these as pure virtual.
*
* It is likely that a future ICU release will split UObject to
* separate out a new "more base" class for only the
* memory management customization, with UObject subclassing that new
* class and adding virtual methods for "boilerplate" functions.
* This will simplify the maintenance of ICU.
*
* @draft ICU 2.2
*/
class U_COMMON_API UObject {
@ -69,31 +114,6 @@ public:
*/
virtual inline ~UObject() {}
#if U_OVERRIDE_CXX_ALLOCATION
/**
* Overrides for ICU4C C++ memory management.
* simple, non-class types are allocated using the macros in common/cmemory.h
* (uprv_malloc(), uprv_free(), uprv_realloc());
* they or something else could be used here to implement C++ new/delete
* for ICU4C C++ classes
* @draft ICU 2.2
*/
void *operator new(size_t size);
void *operator new[](size_t size);
/**
* Overrides for ICU4C C++ memory management.
* simple, non-class types are allocated using the macros in common/cmemory.h
* (uprv_malloc(), uprv_free(), uprv_realloc());
* they or something else could be used here to implement C++ new/delete
* for ICU4C C++ classes
* @draft ICU 2.2
*/
void operator delete(void *p);
void operator delete[](void *p);
#endif
/**
* ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
*

View file

@ -23,9 +23,10 @@
U_NAMESPACE_BEGIN
/*
* Test implementation of UObject::new/delete
* Default implementation of UMemory::new/delete
* using uprv_malloc() and uprv_free().
* This is used together with a list of imported symbols to verify
*
* For testing, this is used together with a list of imported symbols to verify
* that ICU is not using the global ::new and ::delete operators.
*
* These operators can be implemented like this or any other appropriate way
@ -34,6 +35,9 @@ U_NAMESPACE_BEGIN
* to use library name suffixes to distinguish such libraries from
* the standard build.
*
* Instead of just modifying these C++ new/delete operators, it is usually best
* to modify the uprv_malloc()/uprv_free()/uprv_realloc() functions in cmemory.c.
*
* Memory test on Windows/MSVC 6:
* The global operators new and delete look as follows:
* 04F 00000000 UNDEF notype () External | ??2@YAPAXI@Z (void * __cdecl operator new(unsigned int))
@ -45,7 +49,7 @@ U_NAMESPACE_BEGIN
* ??2@YAPAXI@Z and ??3@YAXPAX@Z are the linker symbols in the .obj
* files and are imported from msvcrtd.dll (in a debug build).
*
* Make sure that with the UObject operators new and delete defined these two symbols
* Make sure that with the UMemory operators new and delete defined these two symbols
* do not appear in the dumpbin /symbols output for the ICU libraries!
*
* If such a symbol appears in the output then look in the preceding lines in the output
@ -53,21 +57,21 @@ U_NAMESPACE_BEGIN
* and replace with uprv_malloc/uprv_free.
*/
void *UObject::operator new(size_t size) {
void *UMemory::operator new(size_t size) {
return uprv_malloc(size);
}
void UObject::operator delete(void *p) {
void UMemory::operator delete(void *p) {
if(p!=NULL) {
uprv_free(p);
}
}
void *UObject::operator new[](size_t size) {
void *UMemory::operator new[](size_t size) {
return uprv_malloc(size);
}
void UObject::operator delete[](void *p) {
void UMemory::operator delete[](void *p) {
if(p!=NULL) {
uprv_free(p);
}