chiark / gitweb /
str: New str_matchx function optionally reports possible prefix.
[mLib] / alloc.c
1 /* -*-c-*-
2  *
3  * $Id: alloc.c,v 1.6 2004/04/08 01:36:11 mdw Exp $
4  *
5  * Memory allocation functions
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 Library General Public License as
16  * published by the Free Software Foundation; either version 2 of the
17  * License, or (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 Library General Public License for more details.
23  * 
24  * You should have received a copy of the GNU Library General Public
25  * License along with mLib; if not, write to the Free
26  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Header files ------------------------------------------------------*/
31
32 /* --- ANSI headers --- */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 /* --- Local headers --- */
39
40 #include "alloc.h"
41 #include "arena.h"
42 #include "exc.h"
43
44 /*----- Functions and macros ----------------------------------------------*/
45
46 /* --- @x_alloc@ --- *
47  *
48  * Arguments:   @arena *a@ = pointer to underlying arena
49  *              @size_t sz@ = size of block to allocate
50  *
51  * Returns:     Pointer to allocated block.
52  *
53  * Use:         Allocates memory.  If there's not enough memory, the
54  *              exception @EXC_NOMEM@ is thrown.
55  */
56
57 void *x_alloc(arena *a, size_t sz)
58 {
59   void *p = A_ALLOC(a, sz);
60   if (!p)
61     THROW(EXC_NOMEM);
62   return (p);
63 }
64
65 /* --- @x_strdup@ --- *
66  *
67  * Arguments:   @arena *a@ = pointer to underlying arena
68  *              @const char *s@ = pointer to a string
69  *
70  * Returns:     Pointer to a copy of the string.
71  *
72  * Use:         Copies a string (like @strdup@ would, if it existed).  If
73  *              there's not enough memory, the exception @EXC_NOMEM@ is
74  *              thrown.
75  */
76
77 char *x_strdup(arena *a, const char *s)
78 {
79   size_t sz = strlen(s) + 1;
80   char *p = x_alloc(a, sz);
81   memcpy(p, s, sz);
82   return (p);
83 }
84
85 /* --- @x_realloc@ --- *
86  *
87  * Arguments:   @arena *a@ = pointer to underlying arena
88  *              @void *p@ = pointer to a block of memory
89  *              @size_t sz@ = new size desired for the block
90  *              @size_t osz@ = size of the old block
91  *
92  * Returns:     Pointer to the resized memory block (which is almost
93  *              certainly not in the same place any more).
94  *
95  * Use:         Resizes a memory block.  If there's not enough memory, the
96  *              exception @EXC_NOMEM@ is thrown.
97  */
98
99 void *x_realloc(arena *a, void *p, size_t sz, size_t osz)
100 {
101   p = A_REALLOC(a, p, sz, osz);
102   if (!p)
103     THROW(EXC_NOMEM);
104   return (p);
105 }
106
107 /* --- @x_free@ --- *
108  *
109  * Arguments:   @arena *a@ = pointer to underlying arena
110  *              @void *p@ = pointer to a block of memory.
111  *
112  * Returns:     ---
113  *
114  * Use:         Frees a block of memory.
115  */
116
117 void (x_free)(arena *a, void *p) { x_free(a, p); }
118
119 /*----- Old functions for the standard arena ------------------------------*/
120
121 /* --- @xmalloc@ --- *
122  *
123  * Arguments:   @size_t sz@ = size of block to allocate
124  *
125  * Returns:     Pointer to allocated block.
126  *
127  * Use:         Allocates memory.  If there's not enough memory, the
128  *              exception @EXC_NOMEM@ is thrown.
129  */
130
131 void *(xmalloc)(size_t sz) { return xmalloc(sz); }
132
133 /* --- @xstrdup@ --- *
134  *
135  * Arguments:   @const char *s@ = pointer to a string
136  *
137  * Returns:     Pointer to a copy of the string.
138  *
139  * Use:         Copies a string (like @strdup@ would, if it existed).  If
140  *              there's not enough memory, the exception @EXC_NOMEM@ is
141  *              thrown.
142  */
143
144 char *(xstrdup)(const char *s) { return xstrdup(s); }
145
146 /* --- @xrealloc@ --- *
147  *
148  * Arguments:   @void *p@ = pointer to a block of memory
149  *              @size_t sz@ = new size desired for the block
150  *              @size_t osz@ = size of the old block
151  *
152  * Returns:     Pointer to the resized memory block (which is almost
153  *              certainly not in the same place any more).
154  *
155  * Use:         Resizes a memory block.  If there's not enough memory, the
156  *              exception @EXC_NOMEM@ is thrown.
157  */
158
159 void *(xrealloc)(void *p, size_t sz, size_t osz)
160 { return xrealloc(p, sz, osz); }
161
162 /* --- @xfree@ --- *
163  *
164  * Arguments:   @void *p@ = pointer to a block of memory.
165  *
166  * Returns:     ---
167  *
168  * Use:         Frees a block of memory.
169  */
170
171 void (xfree)(void *p) { xfree(p); }
172
173 /*----- That's all, folks -------------------------------------------------*/