chiark / gitweb /
Reordered and tidied.
[mLib] / sub.h
1 /* -*-c-*-
2  *
3  * $Id: sub.h,v 1.1 1998/06/17 23:44:42 mdw Exp $
4  *
5  * Allocation of known-size blocks
6  *
7  * (c) 1998 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------*
11  *
12  * This file is part of the mLib utilities library.
13  *
14  * mLib is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * mLib is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with mLib; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 /*----- Revision history --------------------------------------------------*
30  *
31  * $Log: sub.h,v $
32  * Revision 1.1  1998/06/17 23:44:42  mdw
33  * Initial revision
34  *
35  */
36
37 #ifndef SUB_H
38 #define SUB_H
39
40 #ifdef __cplusplus
41   extern "C" {
42 #endif
43
44 /*----- Required header files ---------------------------------------------*/
45
46 #include <stdlib.h>
47
48 #ifndef ALLOC_H
49 #  include "alloc.h"
50 #endif
51
52 /*----- Functions provided ------------------------------------------------*/
53
54 /* --- @sub_alloc@ --- *
55  *
56  * Arguments:   @size_t s@ = size of chunk wanted
57  *
58  * Returns:     Pointer to a block at least as large as the one wanted.
59  *
60  * Use:         Allocates a small block of memory.  If there is no more
61  *              memory left, the exception @EXC_NOMEM@ is raised.
62  */
63
64 #ifdef TRACK_ENABLE
65 #  define sub_alloc(s) xmalloc(s)
66 #else
67 void *sub_alloc(size_t s);
68 #endif
69
70 /* --- @sub_free@ --- *
71  *
72  * Arguments:   @void *p@ = address of block to free
73  *              @size_t s@ = size of block
74  *
75  * Returns:     ---
76  *
77  * Use:         Frees a block allocated by @sub_alloc@.
78  */
79
80 #ifdef TRACK_ENABLE
81 #  define sub_free(p, s) free(p)
82 #else
83 void sub_free(void *p, size_t s);
84 #endif
85
86 /* --- @CREATE@ --- *
87  *
88  * Arguments:   @type@ = type of object required; must be passable to
89  *                      @sizeof@
90  *
91  * Returns:     Pointer to a block sufficiently big to hold an object of the
92  *              named type.
93  *
94  * Use:         Allocates a block of the required type.
95  */
96
97 #define CREATE(type) sub_alloc(sizeof(type))
98
99 /* --- @DESTROY@ --- *
100  *
101  * Arguments:   @void *p@ = pointer to an object
102  *
103  * Returns:     ---
104  *
105  * Use:         Frees the thing pointed to by @p@.
106  */
107
108 #define DESTROY(p) sub_free(p, sizeof(*p))
109
110 /* --- @sub_init@ --- *
111  *
112  * Arguments:   ---
113  *
114  * Returns:     ---
115  *
116  * Use:         Initialises the magic allocator.
117  */
118
119 void sub_init(void);
120
121 /*----- That's all, folks -------------------------------------------------*/
122
123 #ifdef __cplusplus
124   }
125 #endif
126
127 #endif