Release 2.3.3.1.
[mLib] / mem / arena.c
1 /* -*-c-*-
2 *
3 * Abstraction for memory allocation arenas
4 *
5 * (c) 2000 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "arena.h"
34
35 /*----- The standard arena ------------------------------------------------*/
36
37 static void *_alloc(arena *a, size_t sz) { return malloc(sz); }
38 static void *_realloc(arena *a, void *p, size_t sz, size_t osz)
39 { return realloc(p, sz); }
40 static void _free(arena *a, void *p) { free(p); }
41
42 static arena_ops stdlib_ops = { _alloc, _realloc, _free, 0 };
43 arena arena_stdlib = { &stdlib_ops };
44
45 /*----- Global variables --------------------------------------------------*/
46
47 arena *arena_global = &arena_stdlib;
48
49 /*----- Main code ---------------------------------------------------------*/
50
51 /* --- @arena_fakerealloc@ --- *
52 *
53 * Arguments: @arena *a@ = pointer to arena block
54 * @void *p@ = pointer to memory block to resize
55 * @size_t sz@ = size desired for the block
56 * @size_t osz@ = size of the old block
57 *
58 * Returns: ---
59 *
60 * Use: Standard fake @realloc@ function, for use if you don't
61 * support @realloc@ properly.
62 */
63
64 void *arena_fakerealloc(arena *a, void *p, size_t sz, size_t osz)
65 {
66 void *q = A_ALLOC(a, sz);
67 if (!q)
68 return (0);
69 memcpy(q, p, sz > osz ? osz : sz);
70 A_FREE(a, p);
71 return (q);
72 }
73
74 /* --- Function equivalents of the macros --- */
75
76 void *a_alloc(arena *a, size_t sz) { return (A_ALLOC(a, sz)); }
77 void *a_realloc(arena *a, void *p, size_t sz, size_t osz)
78 { return A_REALLOC(a, p, sz, osz); }
79 void a_free(arena *a, void *p) { A_FREE(a, p); }
80
81 /*----- That's all, folks -------------------------------------------------*/