]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/cmemory.c
ICU-6.2.8.tar.gz
[apple/icu.git] / icuSources / common / cmemory.c
1 /*
2 ******************************************************************************
3 *
4 * Copyright (C) 2002-2003, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 ******************************************************************************
8 *
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.
12 *
13 * If you have a need to replace ICU allocation, this is the
14 * place to do it.
15 *
16 * Note that uprv_malloc(0) returns a non-NULL pointer, and
17 * that a subsequent free of that pointer value is a NOP.
18 *
19 ******************************************************************************
20 */
21 #include "cmemory.h"
22 #include "unicode/uclean.h"
23
24 /* uprv_malloc(0) returns a pointer to this read-only data. */
25 static const int32_t zeroMem[] = {0, 0, 0, 0, 0, 0};
26
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;
32
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;
36
37 U_CAPI void * U_EXPORT2
38 uprv_malloc(size_t s) {
39 if (s > 0) {
40 gHeapInUse = TRUE;
41 if (pAlloc) {
42 return (*pAlloc)(pContext, s);
43 } else {
44 return malloc(s);
45 }
46 } else {
47 return (void *)zeroMem;
48 }
49 }
50
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) {
56 if (pFree) {
57 (*pFree)(pContext, buffer);
58 } else {
59 free(buffer);
60 }
61 return (void *)zeroMem;
62 } else {
63 gHeapInUse = TRUE;
64 if (pRealloc) {
65 return (*pRealloc)(pContext, buffer, size);
66 } else {
67 return realloc(buffer, size);
68 }
69 }
70 }
71
72 U_CAPI void U_EXPORT2
73 uprv_free(void *buffer) {
74 if (buffer != zeroMem) {
75 if (pFree) {
76 (*pFree)(pContext, buffer);
77 } else {
78 free(buffer);
79 }
80 }
81 }
82
83 U_CAPI void U_EXPORT2
84 u_setMemoryFunctions(const void *context, UMemAllocFn *a, UMemReallocFn *r, UMemFreeFn *f, UErrorCode *status)
85 {
86 if (U_FAILURE(*status)) {
87 return;
88 }
89 if (a==NULL || r==NULL || f==NULL) {
90 *status = U_ILLEGAL_ARGUMENT_ERROR;
91 return;
92 }
93 if (gHeapInUse) {
94 *status = U_INVALID_STATE_ERROR;
95 return;
96 }
97 pContext = context;
98 pAlloc = a;
99 pRealloc = r;
100 pFree = f;
101 }
102
103
104 U_CFUNC UBool cmemory_cleanup(void) {
105 pContext = NULL;
106 pAlloc = NULL;
107 pRealloc = NULL;
108 pFree = NULL;
109 gHeapInUse = FALSE;
110 return TRUE;
111 }
112
113
114 /*
115 * gHeapInUse
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.
119 */
120 U_CFUNC UBool cmemory_inUse() {
121 return gHeapInUse;
122 }
123