summaryrefslogtreecommitdiff
path: root/support/util/NewAlloc.c
blob: ef34c4d04348ba31c2d6e5792e890bea953710ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*-------------------------------------------------------------------------
   newalloc.c - SDCC Memory allocation functions

   These functions are wrappers for the standard malloc, realloc and free
   functions.

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-------------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <assert.h>
#include "newalloc.h"

#if OPT_ENABLE_LIBGC
#include <gc/gc.h>

#define MALLOC	GC_malloc
#define REALLOC GC_realloc
/* PENDING: This is a mild hack.  If we try to GC_free something
   allocated with malloc() then the program will segfault.  Might as
   well drop it and let the garbase collector take care of things.
*/
#define FREE(_a)

#else

#define MALLOC	malloc
#define REALLOC realloc
#define FREE	free

#endif

#define TRACEMALLOC	0

#if TRACEMALLOC
enum
{
  TRACESIZE = 4096
};

static int _allocs[TRACESIZE];
static int _above;

static void
_dumpTrace (int code, void *parg)
{
  int i;
  for (i = 0; i < TRACESIZE; i++)
    {
      if (_allocs[i])
        {
          printf ("%u %u\n", _allocs[i], i);
        }
    }
  printf ("%u above\n", _above);
}

static void
_log (int size)
{
  static int registered;

  if (registered == 0)
    {
      on_exit (_dumpTrace, NULL);
      registered = 1;
    }
  if (size == 12)
    {
      _above++;
    }

  if (size >= TRACESIZE)
    {
      _above++;
    }
  else
    {
      _allocs[size]++;
    }
}
#endif

/*
-------------------------------------------------------------------------------
Clear_realloc - Reallocate a memory block and clear any memory added with
out of memory error detection

-------------------------------------------------------------------------------
*/

void *
Clear_realloc (void *OldPtr, size_t OldSize, size_t NewSize)
{
  void *NewPtr;

  NewPtr = REALLOC (OldPtr, NewSize);

  if (!NewPtr)
    {
      printf ("ERROR - No more memory\n");
/*  werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
      exit (1);
    }

  if (NewPtr)
    if (NewSize > OldSize)
      memset ((char *) NewPtr + OldSize, 0x00, NewSize - OldSize);

  return NewPtr;
}

/*
-------------------------------------------------------------------------------
Safe_realloc - Reallocate a memory block with out of memory error detection

-------------------------------------------------------------------------------
*/

void *
Safe_realloc (void *OldPtr, size_t NewSize)
{
  void *NewPtr;

  NewPtr = REALLOC (OldPtr, NewSize);

  if (!NewPtr)
    {
      printf ("ERROR - No more memory\n");
/*  werror(E_OUT_OF_MEM,__FILE__,NewSize);*/
      exit (1);
    }

  return NewPtr;
}

/*
-------------------------------------------------------------------------------
Safe_calloc - Allocate a block of memory from the application heap, clearing
all data to zero and checking for out of memory errors.

-------------------------------------------------------------------------------
*/

void *
Safe_calloc (size_t Elements, size_t Size)
{
  void *NewPtr;

  NewPtr = MALLOC (Elements * Size);
#if TRACEMALLOC
  _log (Elements * Size);
#endif

  if (!NewPtr)
    {
      printf ("ERROR - No more memory\n");
/*  werror(E_OUT_OF_MEM,__FILE__,Size);*/
      exit (1);
    }

  memset (NewPtr, 0, Elements * Size);

  return NewPtr;
}

/*
-------------------------------------------------------------------------------
Safe_malloc - Allocate a block of memory from the application heap
and checking for out of memory errors.

-------------------------------------------------------------------------------
*/

void *
Safe_malloc (size_t Size)
{
  void *NewPtr;

  NewPtr = MALLOC (Size);

#if TRACEMALLOC
  _log (Size);
#endif

  if (!NewPtr)
    {
      printf ("ERROR - No more memory\n");
/*  werror(E_OUT_OF_MEM,__FILE__,Size);*/
      exit (1);
    }

  return NewPtr;
}

void *
Safe_alloc (size_t Size)
{
  return Safe_calloc (1, Size);
}

void
Safe_free (void *p)
{
  FREE (p);
}

char *
Safe_strndup (const char *sz, size_t size)
{
  char *pret;
  assert (sz);

  pret = Safe_alloc (size + 1);
  strncpy (pret, sz, size);
  pret[size] = '\0';

  return pret;
}

char *
Safe_strdup (const char *sz)
{
  assert (sz);

  return Safe_strndup (sz, strlen (sz));
}

void *
traceAlloc (allocTrace * ptrace, void *p)
{
  assert (ptrace);
  assert (p);

  /* Also handles where max == 0 */
  if (ptrace->num == ptrace->max)
    {
      /* Add an offset to handle max == 0 */
      ptrace->max = (ptrace->max + 2) * 2;
      ptrace->palloced = Safe_realloc (ptrace->palloced, ptrace->max * sizeof (*ptrace->palloced));
    }
  ptrace->palloced[ptrace->num++] = p;

  return p;
}

void
freeTrace (allocTrace * ptrace)
{
  int i;
  assert (ptrace);

  for (i = 0; i < ptrace->num; i++)
    {
      Safe_free (ptrace->palloced[i]);
    }
  ptrace->num = 0;

  Safe_free (ptrace->palloced);
  ptrace->palloced = NULL;
  ptrace->max = 0;
}