]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/cmemory.c
2 ******************************************************************************
4 * Copyright (C) 2002-2003, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
9 * File cmemory.c ICU Heap allocation.
10 * All ICU heap allocation, both for C and C++ new of ICU
11 * class types, comes through these functions.
13 * If you have a need to replace ICU allocation, this is the
16 * Note that uprv_malloc(0) returns a non-NULL pointer, and
17 * that a subsequent free of that pointer value is a NOP.
19 ******************************************************************************
22 #include "unicode/uclean.h"
24 /* uprv_malloc(0) returns a pointer to this read-only data. */
25 static const int32_t zeroMem
[] = {0, 0, 0, 0, 0, 0};
27 /* Function Pointers for user-supplied heap functions */
28 static const void *pContext
;
29 static UMemAllocFn
*pAlloc
;
30 static UMemReallocFn
*pRealloc
;
31 static UMemFreeFn
*pFree
;
33 /* Flag indicating whether any heap allocations have happened.
34 * Used to prevent changing out the heap functions after allocations have been made */
35 static UBool gHeapInUse
;
37 U_CAPI
void * U_EXPORT2
38 uprv_malloc(size_t s
) {
42 return (*pAlloc
)(pContext
, s
);
47 return (void *)zeroMem
;
51 U_CAPI
void * U_EXPORT2
52 uprv_realloc(void * buffer
, size_t size
) {
53 if (buffer
== zeroMem
) {
54 return uprv_malloc(size
);
55 } else if (size
== 0) {
57 (*pFree
)(pContext
, buffer
);
61 return (void *)zeroMem
;
65 return (*pRealloc
)(pContext
, buffer
, size
);
67 return realloc(buffer
, size
);
73 uprv_free(void *buffer
) {
74 if (buffer
!= zeroMem
) {
76 (*pFree
)(pContext
, buffer
);
84 u_setMemoryFunctions(const void *context
, UMemAllocFn
*a
, UMemReallocFn
*r
, UMemFreeFn
*f
, UErrorCode
*status
)
86 if (U_FAILURE(*status
)) {
89 if (a
==NULL
|| r
==NULL
|| f
==NULL
) {
90 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
94 *status
= U_INVALID_STATE_ERROR
;
104 U_CFUNC UBool
cmemory_cleanup(void) {
116 * Return True if ICU has allocated any memory.
117 * Used by u_SetMutexFunctions() and similar to verify that ICU has not
118 * been used, that it is in a pristine initial state.
120 U_CFUNC UBool
cmemory_inUse() {