| 1 |
/* |
| 2 |
* gmem.h |
| 3 |
* |
| 4 |
* Memory routines with out-of-memory checking. |
| 5 |
* |
| 6 |
* Copyright 1996-2003 Glyph & Cog, LLC |
| 7 |
*/ |
| 8 |
|
| 9 |
#ifndef GMEM_H |
| 10 |
#define GMEM_H |
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
#include <aconf.h> |
| 14 |
|
| 15 |
#if USE_EXCEPTIONS |
| 16 |
|
| 17 |
class GMemException { |
| 18 |
public: |
| 19 |
GMemException() {} |
| 20 |
~GMemException() {} |
| 21 |
}; |
| 22 |
|
| 23 |
// This used to be: |
| 24 |
// #define GMEM_EXCEP throw(GMemException) |
| 25 |
// but the throw decl was never really very useful, and is deprecated |
| 26 |
// as of C++11 and illegal as of C++17. |
| 27 |
#define GMEM_EXCEP |
| 28 |
|
| 29 |
#else // USE_EXCEPTIONS |
| 30 |
|
| 31 |
#define GMEM_EXCEP |
| 32 |
|
| 33 |
#endif // USE_EXCEPTIONS |
| 34 |
|
| 35 |
/* |
| 36 |
* Same as malloc, but prints error message and exits if malloc() |
| 37 |
* returns NULL. |
| 38 |
*/ |
| 39 |
#ifdef DEBUG_MEM |
| 40 |
extern void *gmalloc(int size, int ignore = 0) GMEM_EXCEP; |
| 41 |
#else |
| 42 |
extern void *gmalloc(int size) GMEM_EXCEP; |
| 43 |
#endif |
| 44 |
|
| 45 |
/* |
| 46 |
* Same as realloc, but prints error message and exits if realloc() |
| 47 |
* returns NULL. If <p> is NULL, calls malloc instead of realloc(). |
| 48 |
*/ |
| 49 |
extern void *grealloc(void *p, int size) GMEM_EXCEP; |
| 50 |
|
| 51 |
/* |
| 52 |
* These are similar to gmalloc and grealloc, but take an object count |
| 53 |
* and size. The result is similar to allocating nObjs * objSize |
| 54 |
* bytes, but there is an additional error check that the total size |
| 55 |
* doesn't overflow an int. |
| 56 |
*/ |
| 57 |
extern void *gmallocn(int nObjs, int objSize) GMEM_EXCEP; |
| 58 |
extern void *greallocn(void *p, int nObjs, int objSize) GMEM_EXCEP; |
| 59 |
|
| 60 |
/* |
| 61 |
* Same as gmalloc and gmallocn, but allow a 64-bit size on 64-bit |
| 62 |
* systems. |
| 63 |
*/ |
| 64 |
#ifdef DEBUG_MEM |
| 65 |
extern void *gmalloc64(size_t size, int ignore = 0) GMEM_EXCEP; |
| 66 |
#else |
| 67 |
extern void *gmalloc64(size_t size) GMEM_EXCEP; |
| 68 |
#endif |
| 69 |
extern void *gmallocn64(int nObjs, size_t objSize) GMEM_EXCEP; |
| 70 |
|
| 71 |
/* |
| 72 |
* Same as free, but checks for and ignores NULL pointers. |
| 73 |
*/ |
| 74 |
extern void gfree(void *p); |
| 75 |
|
| 76 |
/* |
| 77 |
* Report a memory error. |
| 78 |
*/ |
| 79 |
extern void gMemError(const char *msg) GMEM_EXCEP; |
| 80 |
|
| 81 |
#ifdef DEBUG_MEM |
| 82 |
/* |
| 83 |
* Report on unfreed memory. |
| 84 |
*/ |
| 85 |
extern void gMemReport(FILE *f); |
| 86 |
#else |
| 87 |
#define gMemReport(f) |
| 88 |
#endif |
| 89 |
|
| 90 |
/* |
| 91 |
* Allocate memory and copy a string into it. |
| 92 |
*/ |
| 93 |
extern char *copyString(const char *s); |
| 94 |
|
| 95 |
#endif |