Change the magic number used to introduce a trie file, so that instead
[sgt/agedu] / alloc.h
CommitLineData
70322ae3 1/*
995db599 2 * alloc.h: safe wrappers around malloc, realloc, free, strdup
70322ae3 3 */
4
995db599 5#ifndef AGEDU_ALLOC_H
6#define AGEDU_ALLOC_H
70322ae3 7
8#include <stddef.h>
9
10/*
11 * smalloc should guarantee to return a useful pointer.
12 */
13void *smalloc(size_t size);
14
15/*
16 * srealloc should guaranteeably be able to realloc NULL
17 */
18void *srealloc(void *p, size_t size);
19
20/*
21 * sfree should guaranteeably deal gracefully with freeing NULL
22 */
23void sfree(void *p);
24
25/*
26 * dupstr is like strdup, but with the never-return-NULL property
27 * of smalloc (and also reliably defined in all environments :-)
28 */
29char *dupstr(const char *s);
30
31/*
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:
35 *
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
40 * printf)
41 * - %s takes a const char * and formats it like normal %s
42 * (again, no fine-tuning available)
25b3befe 43 * - %h takes a const char * but escapes it so that it's safe for
44 * HTML
70322ae3 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.
48 */
49char *dupfmt(const char *fmt, ...);
50
51/*
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.
56 */
57#define snew(type) \
58 ( (type *) smalloc (sizeof (type)) )
59
60/*
61 * snewn allocates n instances of a given type, for arrays.
62 */
63#define snewn(number, type) \
64 ( (type *) smalloc ((number) * sizeof (type)) )
65
66/*
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.
70 */
71#define sresize(array, number, type) \
72 ( (void)sizeof((array)-(type *)0), \
73 (type *) srealloc ((array), (number) * sizeof (type)) )
74
995db599 75#endif /* AGEDU_ALLOC_H */