]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/cmemory.c
2 ******************************************************************************
4 * Copyright (C) 2002-2011, 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"
26 /* uprv_malloc(0) returns a pointer to this read-only data. */
27 static const int32_t zeroMem
[] = {0, 0, 0, 0, 0, 0};
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
;
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
;
39 U_CAPI
void * U_EXPORT2
40 uprv_malloc(size_t s
) {
44 return (*pAlloc
)(pContext
, s
);
46 return uprv_default_malloc(s
);
49 return (void *)zeroMem
;
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) {
59 (*pFree
)(pContext
, buffer
);
61 uprv_default_free(buffer
);
63 return (void *)zeroMem
;
67 return (*pRealloc
)(pContext
, buffer
, size
);
69 return uprv_default_realloc(buffer
, size
);
75 uprv_free(void *buffer
) {
76 if (buffer
!= zeroMem
) {
78 (*pFree
)(pContext
, buffer
);
80 uprv_default_free(buffer
);
85 U_CAPI
void * U_EXPORT2
86 uprv_calloc(size_t num
, size_t size
) {
89 mem
= uprv_malloc(size
);
91 uprv_memset(mem
, 0, size
);
97 u_setMemoryFunctions(const void *context
, UMemAllocFn
*a
, UMemReallocFn
*r
, UMemFreeFn
*f
, UErrorCode
*status
)
99 if (U_FAILURE(*status
)) {
102 if (a
==NULL
|| r
==NULL
|| f
==NULL
) {
103 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
107 *status
= U_INVALID_STATE_ERROR
;
117 U_CFUNC UBool
cmemory_cleanup(void) {
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.
133 U_CFUNC UBool
cmemory_inUse() {