]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/cmemory.c
ICU-6.2.22.tar.gz
[apple/icu.git] / icuSources / common / cmemory.c
CommitLineData
b75a7d8f
A
1/*
2******************************************************************************
3*
374ca955 4* Copyright (C) 2002-2003, International Business Machines
b75a7d8f
A
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"
374ca955 22#include "unicode/uclean.h"
b75a7d8f
A
23
24/* uprv_malloc(0) returns a pointer to this read-only data. */
25static const int32_t zeroMem[] = {0, 0, 0, 0, 0, 0};
26
374ca955
A
27/* Function Pointers for user-supplied heap functions */
28static const void *pContext;
29static UMemAllocFn *pAlloc;
30static UMemReallocFn *pRealloc;
31static 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 */
35static UBool gHeapInUse;
36
b75a7d8f
A
37U_CAPI void * U_EXPORT2
38uprv_malloc(size_t s) {
39 if (s > 0) {
374ca955
A
40 gHeapInUse = TRUE;
41 if (pAlloc) {
42 return (*pAlloc)(pContext, s);
43 } else {
44 return malloc(s);
45 }
b75a7d8f
A
46 } else {
47 return (void *)zeroMem;
48 }
49}
50
51U_CAPI void * U_EXPORT2
52uprv_realloc(void * buffer, size_t size) {
53 if (buffer == zeroMem) {
54 return uprv_malloc(size);
55 } else if (size == 0) {
374ca955
A
56 if (pFree) {
57 (*pFree)(pContext, buffer);
58 } else {
59 free(buffer);
60 }
b75a7d8f
A
61 return (void *)zeroMem;
62 } else {
374ca955
A
63 gHeapInUse = TRUE;
64 if (pRealloc) {
65 return (*pRealloc)(pContext, buffer, size);
66 } else {
67 return realloc(buffer, size);
68 }
b75a7d8f
A
69 }
70}
71
72U_CAPI void U_EXPORT2
73uprv_free(void *buffer) {
74 if (buffer != zeroMem) {
374ca955
A
75 if (pFree) {
76 (*pFree)(pContext, buffer);
77 } else {
78 free(buffer);
79 }
80 }
81}
82
83U_CAPI void U_EXPORT2
84u_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;
b75a7d8f 96 }
374ca955
A
97 pContext = context;
98 pAlloc = a;
99 pRealloc = r;
100 pFree = f;
101}
102
103
104U_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 */
120U_CFUNC UBool cmemory_inUse() {
121 return gHeapInUse;
b75a7d8f
A
122}
123