comparison interps/cfunge/cfunge-src/lib/libghthash/cfunge_mempool.c @ 996:859f9b4339e6

<Gregor> tar xf egobot.tar.xz
author HackBot
date Sun, 09 Dec 2012 19:30:08 +0000
parents
children
comparison
equal deleted inserted replaced
995:6883f5911eb7 996:859f9b4339e6
1 /* -*- mode: C; coding: utf-8; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
2 *
3 * cfunge - A standard-conforming Befunge93/98/109 interpreter in C.
4 * Copyright (C) 2008 Arvid Norlander <anmaster AT tele2 DOT se>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at the proxy's option) any later version. Arvid Norlander is a
10 * proxy who can decide which future versions of the GNU General Public
11 * License can be used.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 // This file does not come from libght, however this code is for libght
23 // exclusively.
24
25
26 /*
27 The basic idea:
28 * Pools consist of a header and a data block.
29 * We allocate a huge array, of fixed size.
30 * The pool headers are stored in an array, should one get filled we realloc
31 that array and start a new pool. We never grow a pool itself.
32 Allocation strategy:
33 * If first_free is not NULL we allocate from that linked list.
34 * If first_free is NULL we try to allocate the next object after the last
35 currently allocated block.
36 * If that is not possible we check the next memory pool (if any) or create a
37 new memory pool.
38
39 Notes:
40 * For valgrind purposes we treat the whole thing as one pool.
41
42 */
43
44 #include "cfunge_mempool.h"
45
46 #include <assert.h>
47
48 // Number of items in a pool.
49 #define POOL_ARRAY_COUNT 4096
50
51 #ifdef ENABLE_VALGRIND
52 # include <valgrind/valgrind.h>
53 # include <valgrind/memcheck.h>
54 #else
55 # define VALGRIND_MAKE_MEM_NOACCESS(x, y) /* NO-OP */
56 # define VALGRIND_CREATE_MEMPOOL(x, y, z) /* NO-OP */
57 # define VALGRIND_DESTROY_MEMPOOL(x) /* NO-OP */
58 # define VALGRIND_MEMPOOL_ALLOC(x, y, z) /* NO-OP */
59 # define VALGRIND_MEMPOOL_FREE(x, y) /* NO-OP */
60 # define VALGRIND_MOVE_MEMPOOL(x, y) /* NO-OP */
61 #endif
62
63
64 // I hate the preprocessor
65 #define CF_MEMPOOL_FUNC_INTERN(m_funcname, m_variant) \
66 cf_mempool_ ## m_variant ## _ ## m_funcname
67
68 #define CF_MEMPOOL_FUNC(m_funcname, m_variant) \
69 CF_MEMPOOL_FUNC_INTERN(m_funcname, m_variant)
70
71 #define CF_MEMPOOL_VARIANT fspace
72 #define CF_MEMPOOL_DATATYPE struct s_fspace_hash_entry
73 #define memory_block memory_block_fspace
74 #define pool_header pool_fspace_header
75 #define pools fspace_pools
76 #define pools_size fspace_pools_size
77 #define free_list fspace_free_list
78 #include "cfunge_mempool_priv.h"
79
80 #undef CF_MEMPOOL_VARIANT
81 #undef CF_MEMPOOL_DATATYPE
82 #undef memory_block
83 #undef pool_header
84 #undef pools
85 #undef pools_size
86 #undef free_list
87
88 #ifdef CFUN_EXACT_BOUNDS
89 # define CF_MEMPOOL_VARIANT fspacecount
90 # define CF_MEMPOOL_DATATYPE struct s_fspacecount_hash_entry
91 # define memory_block memory_block_fspacecount
92 # define pool_header pool_fspacecount_header
93 # define pools fspacecount_pools
94 # define pools_size fspacecount_pools_size
95 # define free_list fspacecount_free_list
96 # include "cfunge_mempool_priv.h"
97 #endif