chiark / gitweb /
Release 2.2.0. Yay.
[mLib] / mem / arena.c
CommitLineData
34f655c1 1/* -*-c-*-
34f655c1 2 *
3 * Abstraction for memory allocation arenas
4 *
5 * (c) 2000 Straylight/Edgeware
6 */
7
d4efbcd9 8/*----- Licensing notice --------------------------------------------------*
34f655c1 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.
d4efbcd9 16 *
34f655c1 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.
d4efbcd9 21 *
34f655c1 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
34f655c1 28/*----- Header files ------------------------------------------------------*/
29
30#include <stdlib.h>
c6250df0 31#include <string.h>
34f655c1 32
33#include "arena.h"
34
35/*----- The standard arena ------------------------------------------------*/
36
37static void *_alloc(arena *a, size_t sz) { return malloc(sz); }
d4efbcd9 38static void *_realloc(arena *a, void *p, size_t sz, size_t osz)
b5ea4de3 39 { return realloc(p, sz); }
ad505843 40static void _free(arena *a, void *p) { free(p); }
34f655c1 41
42static arena_ops stdlib_ops = { _alloc, _realloc, _free, 0 };
43arena arena_stdlib = { &stdlib_ops };
44
45/*----- Global variables --------------------------------------------------*/
46
47arena *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
b5ea4de3 56 * @size_t osz@ = size of the old block
34f655c1 57 *
58 * Returns: ---
59 *
60 * Use: Standard fake @realloc@ function, for use if you don't
61 * support @realloc@ properly.
62 */
63
b5ea4de3 64void *arena_fakerealloc(arena *a, void *p, size_t sz, size_t osz)
34f655c1 65{
66 void *q = A_ALLOC(a, sz);
67 if (!q)
68 return (0);
b5ea4de3 69 memcpy(q, p, sz > osz ? osz : sz);
34f655c1 70 A_FREE(a, p);
71 return (q);
72}
73
74/* --- Function equivalents of the macros --- */
75
76void *a_alloc(arena *a, size_t sz) { return (A_ALLOC(a, sz)); }
b5ea4de3 77void *a_realloc(arena *a, void *p, size_t sz, size_t osz)
78{ return A_REALLOC(a, p, sz, osz); }
34f655c1 79void a_free(arena *a, void *p) { A_FREE(a, p); }
80
81/*----- That's all, folks -------------------------------------------------*/