]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/cmemory.c
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / common / cmemory.c
CommitLineData
b75a7d8f
A
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. */
24static const int32_t zeroMem[] = {0, 0, 0, 0, 0, 0};
25
26U_CAPI void * U_EXPORT2
27uprv_malloc(size_t s) {
28 if (s > 0) {
29 return malloc(s);
30 } else {
31 return (void *)zeroMem;
32 }
33}
34
35U_CAPI void * U_EXPORT2
36uprv_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
47U_CAPI void U_EXPORT2
48uprv_free(void *buffer) {
49 if (buffer != zeroMem) {
50 free(buffer);
51 }
52}
53