]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/cmemory.c
2 ******************************************************************************
4 * Copyright (C) 2002-2012, 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"
27 /* uprv_malloc(0) returns a pointer to this read-only data. */
28 static const int32_t zeroMem
[] = {0, 0, 0, 0, 0, 0};
30 /* Function Pointers for user-supplied heap functions */
31 static const void *pContext
;
32 static UMemAllocFn
*pAlloc
;
33 static UMemReallocFn
*pRealloc
;
34 static UMemFreeFn
*pFree
;
36 /* Flag indicating whether any heap allocations have happened.
37 * Used to prevent changing out the heap functions after allocations have been made */
38 static UBool gHeapInUse
;
40 #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
48 static char gValidMemorySink
= 0;
50 U_CAPI
void uprv_checkValidMemory(const void *p
, size_t n
) {
52 * Access the memory to ensure that it's all valid.
53 * Load and save a computed value to try to ensure that the compiler
54 * does not throw away the whole loop.
55 * A thread analyzer might complain about un-mutexed access to gValidMemorySink
56 * which is true but harmless because no one ever uses the value in gValidMemorySink.
58 const char *s
= (const char *)p
;
59 char c
= gValidMemorySink
;
62 for(i
= 0; i
< n
; ++i
) {
70 U_CAPI
void * U_EXPORT2
71 uprv_malloc(size_t s
) {
72 #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
77 fprintf(stderr
,"MALLOC\t#%d\t%ul bytes\t%ul total\n", ++n
,s
,(b
+=s
)); fflush(stderr
);
83 return (*pAlloc
)(pContext
, s
);
85 return uprv_default_malloc(s
);
88 return (void *)zeroMem
;
92 U_CAPI
void * U_EXPORT2
93 uprv_realloc(void * buffer
, size_t size
) {
94 #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
98 if (buffer
== zeroMem
) {
99 return uprv_malloc(size
);
100 } else if (size
== 0) {
102 (*pFree
)(pContext
, buffer
);
104 uprv_default_free(buffer
);
106 return (void *)zeroMem
;
110 return (*pRealloc
)(pContext
, buffer
, size
);
112 return uprv_default_realloc(buffer
, size
);
117 U_CAPI
void U_EXPORT2
118 uprv_free(void *buffer
) {
119 #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
123 if (buffer
!= zeroMem
) {
125 (*pFree
)(pContext
, buffer
);
127 uprv_default_free(buffer
);
132 U_CAPI
void * U_EXPORT2
133 uprv_calloc(size_t num
, size_t size
) {
136 mem
= uprv_malloc(size
);
138 uprv_memset(mem
, 0, size
);
143 U_CAPI
void U_EXPORT2
144 u_setMemoryFunctions(const void *context
, UMemAllocFn
*a
, UMemReallocFn
*r
, UMemFreeFn
*f
, UErrorCode
*status
)
146 if (U_FAILURE(*status
)) {
149 if (a
==NULL
|| r
==NULL
|| f
==NULL
) {
150 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
154 *status
= U_INVALID_STATE_ERROR
;
164 U_CFUNC UBool
cmemory_cleanup(void) {
176 * Return True if ICU has allocated any memory.
177 * Used by u_SetMutexFunctions() and similar to verify that ICU has not
178 * been used, that it is in a pristine initial state.
180 U_CFUNC UBool
cmemory_inUse() {