dmemory.c 14 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
8
9
10
11
#if  defined  (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include <errno.h>

12
#include "error.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
13

Uwe Schulzweida's avatar
Uwe Schulzweida committed
14
#if ! defined (HAVE_CONFIG_H)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
15
#if ! defined (HAVE_MALLOC_H)
16
#  if defined (SX)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
17
18
19
#    define  HAVE_MALLOC_H
#  endif
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
20
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
21
22

#if  defined  (HAVE_MALLOC_H)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
23
#    include <malloc.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
#endif


#define  MALLOC_FUNC   0
#define  CALLOC_FUNC   1
#define  REALLOC_FUNC  2
#define  FREE_FUNC     3

#undef   UNDEFID
#define  UNDEFID  -1

#define  MAXNAME  32   /* Min = 8, for  "unknown" ! */

int dmemory_ExitOnError = 0;

typedef struct
{
  void     *ptr;
  int       item;
  size_t    size;
  size_t    nobj;
  int       mtype;
  int       line;
  char      file[MAXNAME];
  char      caller[MAXNAME];
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
50
MemTable_t;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51

52
53
static MemTable_t *memTable;
static size_t  memTableSize  = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
54
55
static long    memAccess     = 0;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
56
57
58
59
static size_t  MemObjs       = 0;
static size_t  MaxMemObjs    = 0;
static size_t  MemUsed       = 0;
static size_t  MaxMemUsed    = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
60

Uwe Schulzweida's avatar
Uwe Schulzweida committed
61
static int     MEM_Debug     = 0;   /* If set to 1, debugging */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
62
63
64
65
66
67

void memDebug(int debug)
{
  MEM_Debug = debug;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
68
69
static
void memInternalProblem(const char *caller, const char *fmt, ...)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
70
71
{
  va_list args;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
72

Uwe Schulzweida's avatar
Uwe Schulzweida committed
73
74
75
76
77
78
79
80
81
82
83
84
  va_start(args, fmt);

  printf("\n");
   fprintf(stderr, "Internal problem (%s) : ", caller);
  vfprintf(stderr, fmt, args);
   fprintf(stderr, "\n");

  va_end(args);

  exit(EXIT_FAILURE);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
85
86
static
void memError(const char *caller, const char *file, int line, size_t size)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
87
88
{
  printf("\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
89
90
  fprintf(stderr, "Error (%s) : Allocation of %zu bytes failed. [ line %d file %s ]\n",
	  caller, size, line, file);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
91

Uwe Schulzweida's avatar
Uwe Schulzweida committed
92
93
  if ( errno ) perror("System error message ");

Uwe Schulzweida's avatar
Uwe Schulzweida committed
94
95
96
  exit(EXIT_FAILURE);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
97
98
99
static
void memListPrintEntry(int mtype, int item, size_t size, void *ptr,
		       const char *caller, const char *file, int line)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
{
  switch (mtype)
    {
    case MALLOC_FUNC:
      fprintf(stderr, "[%-7s ", "Malloc");
      break;
    case CALLOC_FUNC:
      fprintf(stderr, "[%-7s ", "Calloc");
      break;
    case REALLOC_FUNC:
      fprintf(stderr, "[%-7s ", "Realloc");
      break;
    case FREE_FUNC:
      fprintf(stderr, "[%-7s ", "Free");
      break;
    }

   fprintf(stderr, "memory item %3d ", item);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
   fprintf(stderr, "(%6zu byte) ", size);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
119
120
121
122
123
   fprintf(stderr, "at %p", ptr);
   if ( file != NULL )
     {
       fprintf(stderr, " line %4d", line);
       fprintf(stderr, " file %s", file);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
124
     }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
125
   if ( caller != NULL )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
     fprintf(stderr, " (%s)", caller);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
127
128
129
   fprintf(stderr, "]\n");
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
130
131
static
void memListPrintTable(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
132
{
133
  int item, item1, item2 = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
135
136
137

  if ( MemObjs ) fprintf(stderr, "\nMemory table:\n");

  /* find maximum item */
138
  for (size_t memID = 0; memID < memTableSize; memID++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
139
140
141
142
143
    if ( memTable[memID].item != UNDEFID )
      if ( memTable[memID].item > item2 ) item2 = memTable[memID].item;

  /* find minimum item */
  item1 = item2;
144
  for (size_t memID = 0; memID < memTableSize; memID++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
145
146
147
148
    if ( memTable[memID].item != UNDEFID )
      if ( memTable[memID].item < item1 ) item1 = memTable[memID].item;

  for ( item = item1; item <= item2; item++ )
149
    for (size_t memID = 0; memID < memTableSize; memID++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
151
152
153
154
155
156
157
158
159
160
      {
	if ( memTable[memID].item == item )
	  memListPrintEntry(memTable[memID].mtype, memTable[memID].item,
			    memTable[memID].size*memTable[memID].nobj,
			    memTable[memID].ptr, memTable[memID].caller,
			    memTable[memID].file, memTable[memID].line);
      }

  if ( MemObjs )
    {
      fprintf(stderr, "  Memory access             : %6u\n", (unsigned) memAccess);
161
      fprintf(stderr, "  Maximum objects           : %6zu\n", memTableSize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
      fprintf(stderr, "  Objects used              : %6u\n", (unsigned) MaxMemObjs);
      fprintf(stderr, "  Objects in use            : %6u\n", (unsigned) MemObjs);
      fprintf(stderr, "  Memory allocated          : ");
      if (MemUsed > 1024*1024*1024)
	fprintf(stderr, " %5d GB\n",   (int) (MemUsed/(1024*1024*1024)));
      else if (MemUsed > 1024*1024)
	fprintf(stderr, " %5d MB\n",   (int) (MemUsed/(1024*1024)));
      else if (MemUsed > 1024)
	fprintf(stderr, " %5d KB\n",   (int) (MemUsed/(1024)));
      else
	fprintf(stderr, " %5d Byte\n", (int)  MemUsed);
    }

  if ( MaxMemUsed )
    {
      fprintf(stderr, "  Maximum memory allocated  : ");
      if (MaxMemUsed > 1024*1024*1024)
	fprintf(stderr, " %5d GB\n",   (int) (MaxMemUsed/(1024*1024*1024)));
      else if (MaxMemUsed > 1024*1024)
	fprintf(stderr, " %5d MB\n",   (int) (MaxMemUsed/(1024*1024)));
      else if (MaxMemUsed > 1024)
	fprintf(stderr, " %5d KB\n",   (int) (MaxMemUsed/(1024)));
      else
	fprintf(stderr, " %5d Byte\n", (int)  MaxMemUsed);
    }
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
189
190
static
void memGetDebugLevel(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
{
  char *debugLevel;

  debugLevel = getenv("MEMORY_DEBUG");

  if ( debugLevel )
    {
      if ( isdigit((int) debugLevel[0]) )
	MEM_Debug = atoi(debugLevel);

      if ( MEM_Debug )
	atexit(memListPrintTable);
    }
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
206
207
static
void memInit(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
208
209
210
211
212
213
214
{
  static int initDebugLevel = 0;

  if ( ! initDebugLevel )
    {
      memGetDebugLevel();
      initDebugLevel = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
216
217
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
218
219
static
int memListDeleteEntry(void *ptr, size_t *size)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
220
221
{
  int item = UNDEFID;
222
  size_t memID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
223

224
  for (memID = 0; memID < memTableSize; memID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
    {
      if ( memTable[memID].item == UNDEFID ) continue;
      if ( memTable[memID].ptr == ptr ) break;
    }

  if ( memID != memTableSize )
    {
      MemObjs--;
      MemUsed -= memTable[memID].size * memTable[memID].nobj;
      *size = memTable[memID].size * memTable[memID].nobj;
       item = memTable[memID].item;
       memTable[memID].item   = UNDEFID;
    }

  return (item);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
242
static
243
void memTableInitEntry(size_t memID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
244
{
245
  if ( memID >= memTableSize )
246
    memInternalProblem(__func__, "memID %d undefined!", memID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
247
248
249
250
251
252
253
254
255

  memTable[memID].ptr    = NULL;
  memTable[memID].item   = UNDEFID;
  memTable[memID].size   = 0;
  memTable[memID].nobj   = 0;
  memTable[memID].mtype  = UNDEFID;
  memTable[memID].line   = UNDEFID;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
256
257
258
static
int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj,
		    const char *caller, const char *file, int line)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
259
260
261
{
  static int item = 0;
  size_t memSize = 0;
262
  size_t memID = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
263
264
265
266
267
268
269
270

  /*
    Look for a free slot in memTable.
    (Create the table the first time through).
  */
  if ( memTableSize == 0 )
    {
      memTableSize = 8;
271
272
      memSize  = memTableSize * sizeof(MemTable_t);
      memTable = (MemTable_t *) malloc(memSize);
273
      if( memTable == NULL ) memError(__func__, __FILE__, __LINE__, memSize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
274

275
      for(size_t i = 0; i < memTableSize; i++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
	memTableInitEntry(i);
    }
  else
    {
      while( memID < memTableSize )
	{
	  if ( memTable[memID].item == UNDEFID ) break;
	  memID++;
	}
    }
  /*
    If the table overflows, double its size.
  */
  if ( memID == memTableSize )
    {
      memTableSize = 2*memTableSize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
292
293
      memSize  = memTableSize*sizeof(MemTable_t);
      memTable = (MemTable_t*) realloc(memTable, memSize);
294
      if( memTable == NULL ) memError(__func__, __FILE__, __LINE__, memSize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
295

296
      for (size_t i = memID; i < memTableSize; i++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
297
298
299
300
301
302
303
304
305
306
307
308
	memTableInitEntry(i);
    }

  memTable[memID].item  = item;
  memTable[memID].ptr   = ptr;
  memTable[memID].size  = size;
  memTable[memID].nobj  = nobj;
  memTable[memID].mtype = mtype;
  memTable[memID].line  = line;

  if ( file )
    {
309
      size_t len = strlen(file);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
310
      if ( len > MAXNAME-1 ) len = MAXNAME-1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
311

312
      (void) memcpy(memTable[memID].file, file, len);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
313
314
315
316
317
318
319
320
321
      memTable[memID].file[len] = '\0';
    }
  else
    {
      (void) strcpy(memTable[memID].file, "unknown");
    }

  if ( caller )
    {
322
      size_t len = strlen(caller);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
323
324
      if ( len > MAXNAME-1 ) len = MAXNAME-1;

325
      (void) memcpy(memTable[memID].caller, caller, len);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
      memTable[memID].caller[len] = '\0';
    }
  else
    {
      (void) strcpy(memTable[memID].caller, "unknown");
    }

  MaxMemObjs++;
  MemObjs++;
  MemUsed += size*nobj;
  if ( MemUsed > MaxMemUsed ) MaxMemUsed = MemUsed;

  return (item++);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
341
342
343
static
int memListChangeEntry(void *ptrold, void *ptr, size_t size,
		       const char *caller, const char *file, int line)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
344
345
{
  int item = UNDEFID;
346
  size_t memID = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
347
348
349
350
351
352
353
354
355
356
357
358
  size_t sizeold;

  while( memID < memTableSize )
    {
      if ( memTable[memID].item != UNDEFID )
	if ( memTable[memID].ptr == ptrold ) break;
      memID++;
    }

  if ( memID == memTableSize )
    {
      if ( ptrold != NULL )
359
	memInternalProblem(__func__, "Item at %p not found.", ptrold);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
360
361
362
363
364
365
    }
  else
    {
      item = memTable[memID].item;

      sizeold = memTable[memID].size*memTable[memID].nobj;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
366

Uwe Schulzweida's avatar
Uwe Schulzweida committed
367
368
369
370
371
372
373
374
      memTable[memID].ptr   = ptr;
      memTable[memID].size  = size;
      memTable[memID].nobj  = 1;
      memTable[memID].mtype = REALLOC_FUNC;
      memTable[memID].line  = line;

      if ( file )
	{
375
	  size_t len = strlen(file);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
376
377
	  if ( len > MAXNAME-1 ) len = MAXNAME-1;

378
	  (void) memcpy(memTable[memID].file, file, len);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
379
380
381
382
383
384
385
386
387
	  memTable[memID].file[len] = '\0';
	}
      else
	{
	  (void) strcpy(memTable[memID].file, "unknown");
	}

      if ( caller )
	{
388
	  size_t len = strlen(caller);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
389
390
	  if ( len > MAXNAME-1 ) len = MAXNAME-1;

391
	  (void) memcpy(memTable[memID].caller, caller, len);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
	  memTable[memID].caller[len] = '\0';
	}
      else
	{
	  (void) strcpy(memTable[memID].caller, "unknown");
	}

      MemUsed -= sizeold;
      MemUsed += size;
      if ( MemUsed > MaxMemUsed ) MaxMemUsed = MemUsed;
    }

  return (item);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
407

Uwe Schulzweida's avatar
Uwe Schulzweida committed
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
void *Calloc(const char *caller, const char *file, int line, size_t nobjs, size_t size)
{
  void *ptr = NULL;
  int item = UNDEFID;

  memInit();

  if ( nobjs*size > 0 )
    {
      ptr = calloc(nobjs, size);

      if ( MEM_Debug )
	{
	  memAccess++;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
423
	  if ( ptr ) item = memListNewEntry(CALLOC_FUNC, ptr, size, nobjs, caller, file, line);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
424
425
426
427
428
429
430
431
432
433
434
435
436

	  memListPrintEntry(CALLOC_FUNC, item, size*nobjs, ptr, caller, file, line);
	}

      if ( ptr == NULL && dmemory_ExitOnError )
	memError(caller, file, line, size*nobjs);
    }
  else
    fprintf(stderr, "Warning (%s) : Allocation of 0 bytes! [ line %d file %s ]\n", caller, line, file);

  return(ptr);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
437

Uwe Schulzweida's avatar
Uwe Schulzweida committed
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
void *Malloc(const char *caller, const char *file, int line, size_t size)
{
  void *ptr = NULL;
  int item = UNDEFID;

  memInit();

  if ( size > 0 )
    {
      ptr = malloc(size);

      if ( MEM_Debug )
	{
	  memAccess++;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
453
	  if ( ptr ) item = memListNewEntry(MALLOC_FUNC, ptr, size, 1, caller, file, line);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
454
455
456
457
458
459
460
461
462
463
464
465
466

	  memListPrintEntry(MALLOC_FUNC, item, size, ptr, caller, file, line);
	}

      if ( ptr == NULL && dmemory_ExitOnError )
	memError(caller, file, line, size);
    }
  else
    fprintf(stderr, "Warning (%s) : Allocation of 0 bytes! [ line %d file %s ]\n", caller, line, file);

  return (ptr);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
467

Uwe Schulzweida's avatar
Uwe Schulzweida committed
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
void *Realloc(const char *caller, const char *file, int line, void *ptrold, size_t size)
{
  void *ptr = NULL;
  int item = UNDEFID;

  memInit();

  if ( size > 0 )
    {
      ptr = realloc(ptrold, size);

      if ( MEM_Debug )
	{
	  memAccess++;

	  if ( ptr )
	    {
	      item = memListChangeEntry(ptrold, ptr, size, caller, file, line);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
487
	      if ( item == UNDEFID ) item = memListNewEntry(REALLOC_FUNC, ptr, size, 1, caller, file, line);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
	    }

	  memListPrintEntry(REALLOC_FUNC, item, size, ptr, caller, file, line);
	}

      if ( ptr == NULL && dmemory_ExitOnError )
	memError(caller, file, line, size);
    }
  else
    fprintf(stderr, "Warning (%s) : Allocation of 0 bytes! [ line %d file %s ]\n", caller, line, file);

  return (ptr);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
502

Uwe Schulzweida's avatar
Uwe Schulzweida committed
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
void Free(const char *caller, const char *file, int line, void *ptr)
{
  int item;
  size_t size;

  memInit();

  if ( MEM_Debug )
    {
      if ( (item = memListDeleteEntry(ptr, &size)) >= 0 )
	{
	  memListPrintEntry(FREE_FUNC, item, size, ptr, caller, file, line);
	}
      else
	{
	  if ( ptr )
	    fprintf(stderr, "%s info: memory entry at %p not found. [line %4d file %s (%s)]\n",
520
		    __func__, ptr, line, file, caller);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
521
522
523
524
525
526
	}
    }

  free(ptr);
}

527
528
529
void *cdiXmalloc(size_t size, const char *filename, const char *functionname,
                 int line)
{
530
531
  void *value = malloc(size);
  if (size == 0 || value != NULL) ; else
532
533
534
535
536
537
538
539
    cdiAbort(filename, functionname, line, "malloc failed: %s",
             strerror(errno));
  return value;
}

void *cdiXcalloc(size_t nmemb, size_t size, const char *filename,
                 const char *functionname, int line)
{
540
541
  void *value = calloc(nmemb, size);
  if (size == 0 || value != NULL) ; else
542
543
544
545
546
547
548
549
550
    cdiAbort(filename, functionname, line, "calloc failed: %s",
             strerror(errno) );
  return value;
}

void *cdiXrealloc(void *p, size_t size, const char *functionname,
                  const char *filename, int line)
{
  void *value = realloc(p, size);
551
  if (size == 0 || value != NULL) ; else
552
553
554
555
    cdiAbort(filename, functionname, line, "realloc failed: %s",
             strerror(errno));
  return value;
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
556

Uwe Schulzweida's avatar
Uwe Schulzweida committed
557
558
559
560
561
size_t memTotal(void)
{
  size_t memtotal = 0;
#if  defined  (HAVE_MALLINFO)
  struct mallinfo meminfo = mallinfo();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
562
563
  if ( MEM_Debug )
    {
564
565
566
567
568
569
570
571
572
573
574
      fprintf(stderr, "arena      %8zu (non-mmapped space allocated from system)\n", (size_t)meminfo.arena);
      fprintf(stderr, "ordblks    %8zu (number of free chunks)\n", (size_t)meminfo.ordblks);
      fprintf(stderr, "smblks     %8zu (number of fastbin blocks)\n", (size_t) meminfo.smblks);
      fprintf(stderr, "hblks      %8zu (number of mmapped regions)\n", (size_t) meminfo.hblks);
      fprintf(stderr, "hblkhd     %8zu (space in mmapped regions)\n", (size_t) meminfo.hblkhd);
      fprintf(stderr, "usmblks    %8zu (maximum total allocated space)\n", (size_t) meminfo.usmblks);
      fprintf(stderr, "fsmblks    %8zu (maximum total allocated space)\n", (size_t) meminfo.fsmblks);
      fprintf(stderr, "uordblks   %8zu (total allocated space)\n", (size_t) meminfo.uordblks);
      fprintf(stderr, "fordblks   %8zu (total free space)\n", (size_t) meminfo.fordblks);
      fprintf(stderr, "Memory in use:   %8zu bytes\n", (size_t) meminfo.usmblks + (size_t)meminfo.uordblks);
      fprintf(stderr, "Total heap size: %8zu bytes\n", (size_t) meminfo.arena);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
575
576
577

      /* malloc_stats(); */
    }
578
  memtotal = (size_t)meminfo.arena;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
579
580
581
582
583
#endif

  return (memtotal);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
584

Uwe Schulzweida's avatar
Uwe Schulzweida committed
585
586
587
588
void memExitOnError(void)
{
  dmemory_ExitOnError = 1;
}
589
590
591
592
593
594
595
596
597
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */