]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/cmemory.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
6 * Copyright (C) 2002-2015, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 ******************************************************************************
11 * File cmemory.c ICU Heap allocation.
12 * All ICU heap allocation, both for C and C++ new of ICU
13 * class types, comes through these functions.
15 * If you have a need to replace ICU allocation, this is the
18 * Note that uprv_malloc(0) returns a non-NULL pointer, and
19 * that a subsequent free of that pointer value is a NOP.
21 ******************************************************************************
23 #include "unicode/uclean.h"
29 /* uprv_malloc(0) returns a pointer to this read-only data. */
30 static const int32_t zeroMem
[] = {0, 0, 0, 0, 0, 0};
32 /* Function Pointers for user-supplied heap functions */
33 static const void *pContext
;
34 static UMemAllocFn
*pAlloc
;
35 static UMemReallocFn
*pRealloc
;
36 static UMemFreeFn
*pFree
;
38 #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
44 U_CAPI
void * U_EXPORT2
45 uprv_malloc(size_t s
) {
46 #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
51 fprintf(stderr
,"MALLOC\t#%d\t%ul bytes\t%ul total\n", ++n
,s
,(b
+=s
)); fflush(stderr
);
56 return (*pAlloc
)(pContext
, s
);
58 return uprv_default_malloc(s
);
61 return (void *)zeroMem
;
65 U_CAPI
void * U_EXPORT2
66 uprv_realloc(void * buffer
, size_t size
) {
67 #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
71 if (buffer
== zeroMem
) {
72 return uprv_malloc(size
);
73 } else if (size
== 0) {
75 (*pFree
)(pContext
, buffer
);
77 uprv_default_free(buffer
);
79 return (void *)zeroMem
;
82 return (*pRealloc
)(pContext
, buffer
, size
);
84 return uprv_default_realloc(buffer
, size
);
90 uprv_free(void *buffer
) {
91 #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
95 if (buffer
!= zeroMem
) {
97 (*pFree
)(pContext
, buffer
);
99 uprv_default_free(buffer
);
104 U_CAPI
void * U_EXPORT2
105 uprv_calloc(size_t num
, size_t size
) {
108 mem
= uprv_malloc(size
);
110 uprv_memset(mem
, 0, size
);
115 U_CAPI
void U_EXPORT2
116 u_setMemoryFunctions(const void *context
, UMemAllocFn
*a
, UMemReallocFn
*r
, UMemFreeFn
*f
, UErrorCode
*status
)
118 if (U_FAILURE(*status
)) {
121 if (a
==NULL
|| r
==NULL
|| f
==NULL
) {
122 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
132 U_CFUNC UBool
cmemory_cleanup(void) {