]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/cmemory.c
ICU-491.11.3.tar.gz
[apple/icu.git] / icuSources / common / cmemory.c
1 /*
2 ******************************************************************************
3 *
4 * Copyright (C) 2002-2011, 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 "unicode/uclean.h"
22 #include "cmemory.h"
23 #include "putilimp.h"
24 #include <stdlib.h>
25
26 /* uprv_malloc(0) returns a pointer to this read-only data. */
27 static const int32_t zeroMem[] = {0, 0, 0, 0, 0, 0};
28
29 /* Function Pointers for user-supplied heap functions */
30 static const void *pContext;
31 static UMemAllocFn *pAlloc;
32 static UMemReallocFn *pRealloc;
33 static UMemFreeFn *pFree;
34
35 /* Flag indicating whether any heap allocations have happened.
36 * Used to prevent changing out the heap functions after allocations have been made */
37 static UBool gHeapInUse;
38
39 U_CAPI void * U_EXPORT2
40 uprv_malloc(size_t s) {
41 if (s > 0) {
42 gHeapInUse = TRUE;
43 if (pAlloc) {
44 return (*pAlloc)(pContext, s);
45 } else {
46 return uprv_default_malloc(s);
47 }
48 } else {
49 return (void *)zeroMem;
50 }
51 }
52
53 U_CAPI void * U_EXPORT2
54 uprv_realloc(void * buffer, size_t size) {
55 if (buffer == zeroMem) {
56 return uprv_malloc(size);
57 } else if (size == 0) {
58 if (pFree) {
59 (*pFree)(pContext, buffer);
60 } else {
61 uprv_default_free(buffer);
62 }
63 return (void *)zeroMem;
64 } else {
65 gHeapInUse = TRUE;
66 if (pRealloc) {
67 return (*pRealloc)(pContext, buffer, size);
68 } else {
69 return uprv_default_realloc(buffer, size);
70 }
71 }
72 }
73
74 U_CAPI void U_EXPORT2
75 uprv_free(void *buffer) {
76 if (buffer != zeroMem) {
77 if (pFree) {
78 (*pFree)(pContext, buffer);
79 } else {
80 uprv_default_free(buffer);
81 }
82 }
83 }
84
85 U_CAPI void * U_EXPORT2
86 uprv_calloc(size_t num, size_t size) {
87 void *mem = NULL;
88 size *= num;
89 mem = uprv_malloc(size);
90 if (mem) {
91 uprv_memset(mem, 0, size);
92 }
93 return mem;
94 }
95
96 U_CAPI void U_EXPORT2
97 u_setMemoryFunctions(const void *context, UMemAllocFn *a, UMemReallocFn *r, UMemFreeFn *f, UErrorCode *status)
98 {
99 if (U_FAILURE(*status)) {
100 return;
101 }
102 if (a==NULL || r==NULL || f==NULL) {
103 *status = U_ILLEGAL_ARGUMENT_ERROR;
104 return;
105 }
106 if (gHeapInUse) {
107 *status = U_INVALID_STATE_ERROR;
108 return;
109 }
110 pContext = context;
111 pAlloc = a;
112 pRealloc = r;
113 pFree = f;
114 }
115
116
117 U_CFUNC UBool cmemory_cleanup(void) {
118 pContext = NULL;
119 pAlloc = NULL;
120 pRealloc = NULL;
121 pFree = NULL;
122 gHeapInUse = FALSE;
123 return TRUE;
124 }
125
126
127 /*
128 * gHeapInUse
129 * Return True if ICU has allocated any memory.
130 * Used by u_SetMutexFunctions() and similar to verify that ICU has not
131 * been used, that it is in a pristine initial state.
132 */
133 U_CFUNC UBool cmemory_inUse() {
134 return gHeapInUse;
135 }
136