| 1 | /* |
| 2 | * malloc.h: safe wrappers around malloc, realloc, free, strdup |
| 3 | */ |
| 4 | |
| 5 | #ifndef UMLWRAP_MALLOC_H |
| 6 | #define UMLWRAP_MALLOC_H |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | |
| 10 | /* |
| 11 | * smalloc should guarantee to return a useful pointer - Halibut |
| 12 | * can do nothing except die when it's out of memory anyway. |
| 13 | */ |
| 14 | void *smalloc(size_t size); |
| 15 | |
| 16 | /* |
| 17 | * srealloc should guaranteeably be able to realloc NULL |
| 18 | */ |
| 19 | void *srealloc(void *p, size_t size); |
| 20 | |
| 21 | /* |
| 22 | * sfree should guaranteeably deal gracefully with freeing NULL |
| 23 | */ |
| 24 | void sfree(void *p); |
| 25 | |
| 26 | /* |
| 27 | * dupstr is like strdup, but with the never-return-NULL property |
| 28 | * of smalloc (and also reliably defined in all environments :-) |
| 29 | */ |
| 30 | char *dupstr(const char *s); |
| 31 | |
| 32 | /* |
| 33 | * snew allocates one instance of a given type, and casts the |
| 34 | * result so as to type-check that you're assigning it to the |
| 35 | * right kind of pointer. Protects against allocation bugs |
| 36 | * involving allocating the wrong size of thing. |
| 37 | */ |
| 38 | #define snew(type) \ |
| 39 | ( (type *) smalloc (sizeof (type)) ) |
| 40 | |
| 41 | /* |
| 42 | * snewn allocates n instances of a given type, for arrays. |
| 43 | */ |
| 44 | #define snewn(number, type) \ |
| 45 | ( (type *) smalloc ((number) * sizeof (type)) ) |
| 46 | |
| 47 | /* |
| 48 | * sresize wraps realloc so that you specify the new number of |
| 49 | * elements and the type of the element, with the same type- |
| 50 | * checking advantages. Also type-checks the input pointer. |
| 51 | */ |
| 52 | #define sresize(array, number, type) \ |
| 53 | ( (void)sizeof((array)-(type *)0), \ |
| 54 | (type *) srealloc ((array), (number) * sizeof (type)) ) |
| 55 | |
| 56 | #endif /* UMLWRAP_MALLOC_H */ |