2 * malloc.h: safe wrappers around malloc, realloc, free, strdup
5 #ifndef UMLWRAP_MALLOC_H
6 #define UMLWRAP_MALLOC_H
11 * smalloc should guarantee to return a useful pointer - Halibut
12 * can do nothing except die when it's out of memory anyway.
14 void *smalloc(size_t size);
17 * srealloc should guaranteeably be able to realloc NULL
19 void *srealloc(void *p, size_t size);
22 * sfree should guaranteeably deal gracefully with freeing NULL
27 * dupstr is like strdup, but with the never-return-NULL property
28 * of smalloc (and also reliably defined in all environments :-)
30 char *dupstr(const char *s);
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.
39 ( (type *) smalloc (sizeof (type)) )
42 * snewn allocates n instances of a given type, for arrays.
44 #define snewn(number, type) \
45 ( (type *) smalloc ((number) * sizeof (type)) )
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.
52 #define sresize(array, number, type) \
53 ( (void)sizeof((array)-(type *)0), \
54 (type *) srealloc ((array), (number) * sizeof (type)) )
56 #endif /* UMLWRAP_MALLOC_H */