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 * - %h takes a const char * but escapes it so that it's safe for
45 * - %S takes an int followed by a const char *. If the int is
46 * zero, it behaves just like %s. If the int is nonzero, it
47 * transforms the string by stuffing a \r before every \n.
49 char *dupfmt(const char *fmt
, ...);
52 * snew allocates one instance of a given type, and casts the
53 * result so as to type-check that you're assigning it to the
54 * right kind of pointer. Protects against allocation bugs
55 * involving allocating the wrong size of thing.
58 ( (type *) smalloc (sizeof (type)) )
61 * snewn allocates n instances of a given type, for arrays.
63 #define snewn(number, type) \
64 ( (type *) smalloc ((number) * sizeof (type)) )
67 * sresize wraps realloc so that you specify the new number of
68 * elements and the type of the element, with the same type-
69 * checking advantages. Also type-checks the input pointer.
71 #define sresize(array, number, type) \
72 ( (void)sizeof((array)-(type *)0), \
73 (type *) srealloc ((array), (number) * sizeof (type)) )
75 #endif /* AGEDU_MALLOC_H */