34f655c1 |
1 | /* -*-c-*- |
2 | * |
ad505843 |
3 | * $Id: arena.c,v 1.4 2001/06/22 19:32:59 mdw Exp $ |
34f655c1 |
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 $ |
ad505843 |
33 | * Revision 1.4 2001/06/22 19:32:59 mdw |
34 | * Remove a space. |
35 | * |
c6250df0 |
36 | * Revision 1.3 2000/08/06 11:00:18 mdw |
37 | * Daft bug fix. Include <string.h>. |
38 | * |
b5ea4de3 |
39 | * Revision 1.2 2000/07/16 12:29:16 mdw |
40 | * Change to arena `realloc' interface, to fix a design bug. |
41 | * |
34f655c1 |
42 | * Revision 1.1 2000/06/17 10:37:53 mdw |
43 | * Basic arena management. |
44 | * |
45 | */ |
46 | |
47 | /*----- Header files ------------------------------------------------------*/ |
48 | |
49 | #include <stdlib.h> |
c6250df0 |
50 | #include <string.h> |
34f655c1 |
51 | |
52 | #include "arena.h" |
53 | |
54 | /*----- The standard arena ------------------------------------------------*/ |
55 | |
56 | static void *_alloc(arena *a, size_t sz) { return malloc(sz); } |
b5ea4de3 |
57 | static void *_realloc(arena *a, void *p, size_t sz, size_t osz) |
58 | { return realloc(p, sz); } |
ad505843 |
59 | static void _free(arena *a, void *p) { free(p); } |
34f655c1 |
60 | |
61 | static arena_ops stdlib_ops = { _alloc, _realloc, _free, 0 }; |
62 | arena arena_stdlib = { &stdlib_ops }; |
63 | |
64 | /*----- Global variables --------------------------------------------------*/ |
65 | |
66 | arena *arena_global = &arena_stdlib; |
67 | |
68 | /*----- Main code ---------------------------------------------------------*/ |
69 | |
70 | /* --- @arena_fakerealloc@ --- * |
71 | * |
72 | * Arguments: @arena *a@ = pointer to arena block |
73 | * @void *p@ = pointer to memory block to resize |
74 | * @size_t sz@ = size desired for the block |
b5ea4de3 |
75 | * @size_t osz@ = size of the old block |
34f655c1 |
76 | * |
77 | * Returns: --- |
78 | * |
79 | * Use: Standard fake @realloc@ function, for use if you don't |
80 | * support @realloc@ properly. |
81 | */ |
82 | |
b5ea4de3 |
83 | void *arena_fakerealloc(arena *a, void *p, size_t sz, size_t osz) |
34f655c1 |
84 | { |
85 | void *q = A_ALLOC(a, sz); |
86 | if (!q) |
87 | return (0); |
b5ea4de3 |
88 | memcpy(q, p, sz > osz ? osz : sz); |
34f655c1 |
89 | A_FREE(a, p); |
90 | return (q); |
91 | } |
92 | |
93 | /* --- Function equivalents of the macros --- */ |
94 | |
95 | void *a_alloc(arena *a, size_t sz) { return (A_ALLOC(a, sz)); } |
b5ea4de3 |
96 | void *a_realloc(arena *a, void *p, size_t sz, size_t osz) |
97 | { return A_REALLOC(a, p, sz, osz); } |
34f655c1 |
98 | void a_free(arena *a, void *p) { A_FREE(a, p); } |
99 | |
100 | /*----- That's all, folks -------------------------------------------------*/ |