]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/cmemory.cpp
ICU-62108.0.1.tar.gz
[apple/icu.git] / icuSources / common / cmemory.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/*
4******************************************************************************
5*
b331163b 6* Copyright (C) 2002-2015, International Business Machines
b75a7d8f
A
7* Corporation and others. All Rights Reserved.
8*
9******************************************************************************
10*
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.
14*
15* If you have a need to replace ICU allocation, this is the
16* place to do it.
17*
18* Note that uprv_malloc(0) returns a non-NULL pointer, and
19* that a subsequent free of that pointer value is a NOP.
20*
21******************************************************************************
22*/
374ca955 23#include "unicode/uclean.h"
73c04bcf 24#include "cmemory.h"
4388f060 25#include "putilimp.h"
51004dcb 26#include "uassert.h"
73c04bcf 27#include <stdlib.h>
b75a7d8f 28
51004dcb 29/* uprv_malloc(0) returns a pointer to this read-only data. */
b75a7d8f
A
30static const int32_t zeroMem[] = {0, 0, 0, 0, 0, 0};
31
374ca955
A
32/* Function Pointers for user-supplied heap functions */
33static const void *pContext;
34static UMemAllocFn *pAlloc;
35static UMemReallocFn *pRealloc;
36static UMemFreeFn *pFree;
37
51004dcb
A
38#if U_DEBUG && defined(UPRV_MALLOC_COUNT)
39#include <stdio.h>
40static int n=0;
41static long b=0;
42#endif
43
b75a7d8f
A
44U_CAPI void * U_EXPORT2
45uprv_malloc(size_t s) {
51004dcb
A
46#if U_DEBUG && defined(UPRV_MALLOC_COUNT)
47#if 1
48 putchar('>');
49 fflush(stdout);
50#else
51 fprintf(stderr,"MALLOC\t#%d\t%ul bytes\t%ul total\n", ++n,s,(b+=s)); fflush(stderr);
52#endif
53#endif
b75a7d8f 54 if (s > 0) {
374ca955
A
55 if (pAlloc) {
56 return (*pAlloc)(pContext, s);
57 } else {
4388f060 58 return uprv_default_malloc(s);
374ca955 59 }
b75a7d8f
A
60 } else {
61 return (void *)zeroMem;
62 }
63}
64
65U_CAPI void * U_EXPORT2
66uprv_realloc(void * buffer, size_t size) {
51004dcb
A
67#if U_DEBUG && defined(UPRV_MALLOC_COUNT)
68 putchar('~');
69 fflush(stdout);
70#endif
b75a7d8f
A
71 if (buffer == zeroMem) {
72 return uprv_malloc(size);
73 } else if (size == 0) {
374ca955
A
74 if (pFree) {
75 (*pFree)(pContext, buffer);
76 } else {
4388f060 77 uprv_default_free(buffer);
374ca955 78 }
b75a7d8f
A
79 return (void *)zeroMem;
80 } else {
374ca955
A
81 if (pRealloc) {
82 return (*pRealloc)(pContext, buffer, size);
83 } else {
4388f060 84 return uprv_default_realloc(buffer, size);
374ca955 85 }
b75a7d8f
A
86 }
87}
88
89U_CAPI void U_EXPORT2
90uprv_free(void *buffer) {
51004dcb
A
91#if U_DEBUG && defined(UPRV_MALLOC_COUNT)
92 putchar('<');
93 fflush(stdout);
94#endif
b75a7d8f 95 if (buffer != zeroMem) {
374ca955
A
96 if (pFree) {
97 (*pFree)(pContext, buffer);
98 } else {
4388f060 99 uprv_default_free(buffer);
374ca955
A
100 }
101 }
102}
103
4388f060
A
104U_CAPI void * U_EXPORT2
105uprv_calloc(size_t num, size_t size) {
106 void *mem = NULL;
107 size *= num;
108 mem = uprv_malloc(size);
109 if (mem) {
110 uprv_memset(mem, 0, size);
111 }
112 return mem;
113}
114
374ca955
A
115U_CAPI void U_EXPORT2
116u_setMemoryFunctions(const void *context, UMemAllocFn *a, UMemReallocFn *r, UMemFreeFn *f, UErrorCode *status)
117{
118 if (U_FAILURE(*status)) {
119 return;
120 }
121 if (a==NULL || r==NULL || f==NULL) {
122 *status = U_ILLEGAL_ARGUMENT_ERROR;
123 return;
124 }
374ca955
A
125 pContext = context;
126 pAlloc = a;
127 pRealloc = r;
128 pFree = f;
129}
130
131
132U_CFUNC UBool cmemory_cleanup(void) {
133 pContext = NULL;
134 pAlloc = NULL;
135 pRealloc = NULL;
136 pFree = NULL;
374ca955
A
137 return TRUE;
138}