chiark / gitweb /
Version bump.
[mLib] / arena.c
1 /* -*-c-*-
2  *
3  * $Id: arena.c,v 1.1 2000/06/17 10:37:53 mdw Exp $
4  *
5  * Abstraction for memory allocation arenas
6  *
7  * (c) 2000 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 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: arena.c,v $
33  * Revision 1.1  2000/06/17 10:37:53  mdw
34  * Basic arena management.
35  *
36  */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include <stdlib.h>
41
42 #include "arena.h"
43
44 /*----- The standard arena ------------------------------------------------*/
45
46 static void *_alloc(arena *a, size_t sz) { return malloc(sz); }
47 static void *_realloc(arena *a, void *p, size_t sz){ return realloc(p, sz); }
48 static void _free(arena *a, void *p) { free (p); }
49
50 static arena_ops stdlib_ops = { _alloc, _realloc, _free, 0 };
51 arena arena_stdlib = { &stdlib_ops };
52
53 /*----- Global variables --------------------------------------------------*/
54
55 arena *arena_global = &arena_stdlib;
56
57 /*----- Main code ---------------------------------------------------------*/
58
59 /* --- @arena_fakerealloc@ --- *
60  *
61  * Arguments:   @arena *a@ = pointer to arena block
62  *              @void *p@ = pointer to memory block to resize
63  *              @size_t sz@ = size desired for the block
64  *
65  * Returns:     ---
66  *
67  * Use:         Standard fake @realloc@ function, for use if you don't
68  *              support @realloc@ properly.
69  */
70
71 void *arena_fakerealloc(arena *a, void *p, size_t sz)
72 {
73   void *q = A_ALLOC(a, sz);
74   if (!q)
75     return (0);
76   memcpy(q, p, sz);
77   A_FREE(a, p);
78   return (q);
79 }
80
81 /* --- Function equivalents of the macros --- */
82
83 void *a_alloc(arena *a, size_t sz) { return (A_ALLOC(a, sz)); }
84 void *a_realloc(arena *a, void *p, size_t sz) { return A_REALLOC(a, p, sz); }
85 void a_free(arena *a, void *p) { A_FREE(a, p); }
86
87 /*----- That's all, folks -------------------------------------------------*/