]>
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 ******************************************************************************
21 #include "unicode/uclean.h"
25 /* uprv_malloc(0) returns a pointer to this read-only data. */
26 static const int32_t zeroMem
[] = {0, 0, 0, 0, 0, 0};
28 /* Function Pointers for user-supplied heap functions */
29 static const void *pContext
;
30 static UMemAllocFn
*pAlloc
;
31 static UMemReallocFn
*pRealloc
;
32 static UMemFreeFn
*pFree
;
34 /* Flag indicating whether any heap allocations have happened.
35 * Used to prevent changing out the heap functions after allocations have been made */
36 static UBool gHeapInUse
;
38 U_CAPI
void * U_EXPORT2
39 uprv_malloc(size_t s
) {
43 return (*pAlloc
)(pContext
, s
);
48 return (void *)zeroMem
;
52 U_CAPI
void * U_EXPORT2
53 uprv_realloc(void * buffer
, size_t size
) {
54 if (buffer
== zeroMem
) {
55 return uprv_malloc(size
);
56 } else if (size
== 0) {
58 (*pFree
)(pContext
, buffer
);
62 return (void *)zeroMem
;
66 return (*pRealloc
)(pContext
, buffer
, size
);
68 return realloc(buffer
, size
);
74 uprv_free(void *buffer
) {
75 if (buffer
!= zeroMem
) {
77 (*pFree
)(pContext
, buffer
);
85 u_setMemoryFunctions(const void *context
, UMemAllocFn
*a
, UMemReallocFn
*r
, UMemFreeFn
*f
, UErrorCode
*status
)
87 if (U_FAILURE(*status
)) {
90 if (a
==NULL
|| r
==NULL
|| f
==NULL
) {
91 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
95 *status
= U_INVALID_STATE_ERROR
;
105 U_CFUNC UBool
cmemory_cleanup(void) {
117 * Return True if ICU has allocated any memory.
118 * Used by u_SetMutexFunctions() and similar to verify that ICU has not
119 * been used, that it is in a pristine initial state.
121 U_CFUNC UBool
cmemory_inUse() {