aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/swig/Source/DOH/memory.c
blob: f8081d3aeef7529e48f8b9c2f690db34632cf831 (plain) (blame)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* ----------------------------------------------------------------------------- 
 * This file is part of SWIG, which is licensed as a whole under version 3 
 * (or any later version) of the GNU General Public License. Some additional
 * terms also apply to certain portions of SWIG. The full details of the SWIG
 * license and copyrights can be found in the LICENSE and COPYRIGHT files
 * included with the SWIG source code as distributed by the SWIG developers
 * and at https://www.swig.org/legal.html.
 *
 * memory.c
 *
 *     This file implements all of DOH's memory management including allocation
 *     of objects and checking of objects.
 * ----------------------------------------------------------------------------- */

#include "dohint.h"

#include <stdio.h>
#include <stdlib.h>

#ifndef DOH_POOL_SIZE
#define DOH_POOL_SIZE         4194304
#endif

/* Checks stale DOH object use - will use a lot more memory as pool memory is not re-used. */
/*
#define DOH_DEBUG_MEMORY_POOLS
*/

static int PoolSize = DOH_POOL_SIZE;

DOH *DohNone = 0;		/* The DOH None object */

typedef struct pool {
  DohBase *ptr;			/* Start of pool */
  int len;			/* Length of pool */
  int blen;			/* Byte length of pool */
  int current;			/* Current position for next allocation */
  char *pbeg;			/* Beg of pool */
  char *pend;			/* End of pool */
  struct pool *next;		/* Next pool */
} Pool;

static DohBase *FreeList = 0;	/* List of free objects */
static Pool *Pools = 0;
static int pools_initialized = 0;

/* ----------------------------------------------------------------------
 * CreatePool() - Create a new memory pool 
 * ---------------------------------------------------------------------- */

static void CreatePool(void) {
  Pool *p = 0;
  p = (Pool *) DohMalloc(sizeof(Pool));
  p->ptr = (DohBase *) DohCalloc(PoolSize, sizeof(DohBase));
  p->len = PoolSize;
  p->blen = PoolSize * sizeof(DohBase);
  p->current = 0;
  p->pbeg = ((char *) p->ptr);
  p->pend = p->pbeg + p->blen;
  p->next = Pools;
  Pools = p;
}

/* ----------------------------------------------------------------------
 * InitPools() - Initialize the memory allocator
 * ---------------------------------------------------------------------- */

static void InitPools(void) {
  if (pools_initialized)
    return;
  CreatePool();			/* Create initial pool */
  pools_initialized = 1;
  DohNone = NewVoid(0, 0);	/* Create the None object */
  DohIntern(DohNone);
}

/* ----------------------------------------------------------------------
 * DohCheck()
 *
 * Returns 1 if an arbitrary pointer is a DOH object.
 * ---------------------------------------------------------------------- */

int DohCheck(const DOH *ptr) {
  Pool *p = Pools;
  char *cptr = (char *) ptr;
  while (p) {
    if ((cptr >= p->pbeg) && (cptr < p->pend)) {
#ifdef DOH_DEBUG_MEMORY_POOLS
      DohBase *b = (DohBase *) ptr;
      int DOH_object_already_deleted = b->type == 0;
      assert(!DOH_object_already_deleted);
#endif
      return 1;
    }
    /*
       pptr = (char *) p->ptr;
       if ((cptr >= pptr) && (cptr < (pptr+(p->current*sizeof(DohBase))))) return 1; */
    p = p->next;
  }
  return 0;
}

/* -----------------------------------------------------------------------------
 * DohIntern()
 * ----------------------------------------------------------------------------- */

void DohIntern(DOH *obj) {
  DohBase *b = (DohBase *) obj;
  b->flag_intern = 1;
}

/* ----------------------------------------------------------------------
 * DohObjMalloc()
 *
 * Allocate memory for a new object.
 * ---------------------------------------------------------------------- */

DOH *DohObjMalloc(DohObjInfo *type, void *data) {
  DohBase *obj;
  if (!pools_initialized)
    InitPools();
#ifndef DOH_DEBUG_MEMORY_POOLS
  if (FreeList) {
    obj = FreeList;
    FreeList = (DohBase *) obj->data;
  } else {
#endif
    while (Pools->current == Pools->len) {
      CreatePool();
    }
    obj = Pools->ptr + Pools->current;
    ++Pools->current;
#ifndef DOH_DEBUG_MEMORY_POOLS
  }
#endif
  obj->type = type;
  obj->data = data;
  obj->meta = 0;
  obj->refcount = 1;
  obj->flag_intern = 0;
  obj->flag_marked = 0;
  obj->flag_user = 0;
  obj->flag_usermark = 0;
  return (DOH *) obj;
}

/* ----------------------------------------------------------------------
 * DohObjFree() - Free a DOH object
 * ---------------------------------------------------------------------- */

void DohObjFree(DOH *ptr) {
  DohBase *b, *meta;
  b = (DohBase *) ptr;
  if (b->flag_intern)
    return;
  meta = (DohBase *) b->meta;
  b->data = (void *) FreeList;
  b->meta = 0;
  b->type = 0;
  b->refcount = 0;
  FreeList = b;
  if (meta) {
    Delete(meta);
  }
}

/* ----------------------------------------------------------------------
 * DohMemoryDebug()
 *
 * Display memory usage statistics
 * ---------------------------------------------------------------------- */

void DohMemoryDebug(void) {
  extern DohObjInfo DohStringType;
  extern DohObjInfo DohListType;
  extern DohObjInfo DohHashType;

  Pool *p;
  int totsize = 0;
  int totused = 0;
  int totfree = 0;

  int numstring = 0;
  int numlist = 0;
  int numhash = 0;

  printf("Memory statistics:\n\n");
  printf("Pools:\n");

  p = Pools;
  while (p) {
    /* Calculate number of used, free items */
    int i;
    int nused = 0, nfree = 0;
    for (i = 0; i < p->len; i++) {
      if (p->ptr[i].refcount <= 0)
	nfree++;
      else {
	nused++;
	if (p->ptr[i].type == &DohStringType)
	  numstring++;
	else if (p->ptr[i].type == &DohListType)
	  numlist++;
	else if (p->ptr[i].type == &DohHashType)
	  numhash++;
      }
    }
    printf("    Pool %8p: size = %10d. used = %10d. free = %10d\n", (void *) p, p->len, nused, nfree);
    totsize += p->len;
    totused += nused;
    totfree += nfree;
    p = p->next;
  }
  printf("\n    Total:          size = %10d, used = %10d, free = %10d\n", totsize, totused, totfree);

  printf("\nObject types\n");
  printf("    Strings   : %d\n", numstring);
  printf("    Lists     : %d\n", numlist);
  printf("    Hashes    : %d\n", numhash);

#if 0
  p = Pools;
  while (p) {
    int i;
    for (i = 0; i < p->len; i++) {
      if (p->ptr[i].refcount > 0) {
	if (p->ptr[i].type == &DohStringType) {
	  Printf(stdout, "%s\n", p->ptr + i);
	}
      }
    }
    p = p->next;
  }
#endif

}

/* Function to call instead of exit(). */
static void (*doh_exit_handler)(int) = NULL;

void DohSetExitHandler(void (*new_handler)(int)) {
  doh_exit_handler = new_handler;
}

void DohExit(int status) {
  if (doh_exit_handler) {
    void (*handler)(int) = doh_exit_handler;
    /* Unset the handler to avoid infinite loops if it tries to do something
     * which calls DohExit() (e.g. calling Malloc() and that failing).
     */
    doh_exit_handler = NULL;
    handler(status);
  }
  doh_internal_exit(status);
}

static void allocation_failed(size_t n, size_t size) {
  /* Report and exit as directly as possible to try to avoid further issues due
   * to lack of memory. */
  if (n == 1) {
#if defined __STDC_VERSION__ && __STDC_VERSION__-0 >= 19901L
    fprintf(stderr, "Failed to allocate %zu bytes\n", size);
#else
    fprintf(stderr, "Failed to allocate %lu bytes\n", (unsigned long)size);
#endif
  } else {
#if defined __STDC_VERSION__ && __STDC_VERSION__-0 >= 19901L
    fprintf(stderr, "Failed to allocate %zu*%zu bytes\n", n, size);
#else
    fprintf(stderr, "Failed to allocate %lu*%lu bytes\n", (unsigned long)n, (unsigned long)size);
#endif
  }
  DohExit(EXIT_FAILURE);
}

void *DohMalloc(size_t size) {
  void *p = doh_internal_malloc(size);
  if (!p) allocation_failed(1, size);
  return p;
}

void *DohRealloc(void *ptr, size_t size) {
  void *p = doh_internal_realloc(ptr, size);
  if (!p) allocation_failed(1, size);
  return p;
}

void *DohCalloc(size_t n, size_t size) {
  void *p = doh_internal_calloc(n, size);
  if (!p) allocation_failed(n, size);
  return p;
}