]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/cmemory.c
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / common / cmemory.c
1 /*
2 ******************************************************************************
3 *
4 * Copyright (C) 2002, 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 "cmemory.h"
22
23 /* uprv_malloc(0) returns a pointer to this read-only data. */
24 static const int32_t zeroMem[] = {0, 0, 0, 0, 0, 0};
25
26 U_CAPI void * U_EXPORT2
27 uprv_malloc(size_t s) {
28 if (s > 0) {
29 return malloc(s);
30 } else {
31 return (void *)zeroMem;
32 }
33 }
34
35 U_CAPI void * U_EXPORT2
36 uprv_realloc(void * buffer, size_t size) {
37 if (buffer == zeroMem) {
38 return uprv_malloc(size);
39 } else if (size == 0) {
40 free(buffer);
41 return (void *)zeroMem;
42 } else {
43 return realloc(buffer, size);
44 }
45 }
46
47 U_CAPI void U_EXPORT2
48 uprv_free(void *buffer) {
49 if (buffer != zeroMem) {
50 free(buffer);
51 }
52 }
53