34f655c1 |
1 | /* -*-c-*- |
2 | * |
8656dc50 |
3 | * $Id: arena.c,v 1.5 2004/04/08 01:36:11 mdw Exp $ |
34f655c1 |
4 | * |
5 | * Abstraction for memory allocation arenas |
6 | * |
7 | * (c) 2000 Straylight/Edgeware |
8 | */ |
9 | |
d4efbcd9 |
10 | /*----- Licensing notice --------------------------------------------------* |
34f655c1 |
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. |
d4efbcd9 |
18 | * |
34f655c1 |
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. |
d4efbcd9 |
23 | * |
34f655c1 |
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 | |
34f655c1 |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
32 | #include <stdlib.h> |
c6250df0 |
33 | #include <string.h> |
34f655c1 |
34 | |
35 | #include "arena.h" |
36 | |
37 | /*----- The standard arena ------------------------------------------------*/ |
38 | |
39 | static void *_alloc(arena *a, size_t sz) { return malloc(sz); } |
d4efbcd9 |
40 | static void *_realloc(arena *a, void *p, size_t sz, size_t osz) |
b5ea4de3 |
41 | { return realloc(p, sz); } |
ad505843 |
42 | static void _free(arena *a, void *p) { free(p); } |
34f655c1 |
43 | |
44 | static arena_ops stdlib_ops = { _alloc, _realloc, _free, 0 }; |
45 | arena arena_stdlib = { &stdlib_ops }; |
46 | |
47 | /*----- Global variables --------------------------------------------------*/ |
48 | |
49 | arena *arena_global = &arena_stdlib; |
50 | |
51 | /*----- Main code ---------------------------------------------------------*/ |
52 | |
53 | /* --- @arena_fakerealloc@ --- * |
54 | * |
55 | * Arguments: @arena *a@ = pointer to arena block |
56 | * @void *p@ = pointer to memory block to resize |
57 | * @size_t sz@ = size desired for the block |
b5ea4de3 |
58 | * @size_t osz@ = size of the old block |
34f655c1 |
59 | * |
60 | * Returns: --- |
61 | * |
62 | * Use: Standard fake @realloc@ function, for use if you don't |
63 | * support @realloc@ properly. |
64 | */ |
65 | |
b5ea4de3 |
66 | void *arena_fakerealloc(arena *a, void *p, size_t sz, size_t osz) |
34f655c1 |
67 | { |
68 | void *q = A_ALLOC(a, sz); |
69 | if (!q) |
70 | return (0); |
b5ea4de3 |
71 | memcpy(q, p, sz > osz ? osz : sz); |
34f655c1 |
72 | A_FREE(a, p); |
73 | return (q); |
74 | } |
75 | |
76 | /* --- Function equivalents of the macros --- */ |
77 | |
78 | void *a_alloc(arena *a, size_t sz) { return (A_ALLOC(a, sz)); } |
b5ea4de3 |
79 | void *a_realloc(arena *a, void *p, size_t sz, size_t osz) |
80 | { return A_REALLOC(a, p, sz, osz); } |
34f655c1 |
81 | void a_free(arena *a, void *p) { A_FREE(a, p); } |
82 | |
83 | /*----- That's all, folks -------------------------------------------------*/ |