2 * malloc.h: safe wrappers around malloc, realloc, free, strdup
11 * smalloc should guarantee to return a useful pointer.
13 void *smalloc(size_t size
);
16 * srealloc should guaranteeably be able to realloc NULL
18 void *srealloc(void *p
, size_t size
);
21 * sfree should guaranteeably deal gracefully with freeing NULL
26 * dupstr is like strdup, but with the never-return-NULL property
27 * of smalloc (and also reliably defined in all environments :-)
29 char *dupstr(const char *s
);
32 * dupfmt is a bit like printf, but does its own allocation and
33 * returns a dynamic string. It also supports a different (and
34 * much less featureful) set of format directives:
36 * - %D takes no argument, and gives the current date and time in
37 * a format suitable for an HTTP Date header
38 * - %d takes an int argument and formats it like normal %d (but
39 * doesn't support any of the configurability of standard
41 * - %s takes a const char * and formats it like normal %s
42 * (again, no fine-tuning available)
43 * - %S takes an int followed by a const char *. If the int is
44 * zero, it behaves just like %s. If the int is nonzero, it
45 * transforms the string by stuffing a \r before every \n.
47 char *dupfmt(const char *fmt
, ...);
50 * snew allocates one instance of a given type, and casts the
51 * result so as to type-check that you're assigning it to the
52 * right kind of pointer. Protects against allocation bugs
53 * involving allocating the wrong size of thing.
56 ( (type *) smalloc (sizeof (type)) )
59 * snewn allocates n instances of a given type, for arrays.
61 #define snewn(number, type) \
62 ( (type *) smalloc ((number) * sizeof (type)) )
65 * sresize wraps realloc so that you specify the new number of
66 * elements and the type of the element, with the same type-
67 * checking advantages. Also type-checks the input pointer.
69 #define sresize(array, number, type) \
70 ( (void)sizeof((array)-(type *)0), \
71 (type *) srealloc ((array), (number) * sizeof (type)) )
73 #endif /* AGEDU_MALLOC_H */