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