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