1 /* Fixed-size block allocator.
2 Copyright (C) 1994 Free Software Foundation, Inc.
4 This file is part of SXEmacs
6 SXEmacs 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 your option) any later version.
11 SXEmacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* Synched up with: Not in FSF. */
24 Ben Wing: December 1994, for 19.12.
29 ------------------------------------------------------------------------------
31 A "block-type object" is used to efficiently allocate and free blocks
32 of a particular size. Freed blocks are remembered in a free list and
33 are reused as necessary to allocate new blocks, so as to avoid as
34 much as possible making calls to malloc() and free().
36 This is a container object. Declare a block-type object of a specific type
39 struct mytype_blocktype {
40 Blocktype_declare (mytype);
43 Use the following functions/macros:
45 structype *Blocktype_new(structype)
46 [MACRO] Create a new block-type object of the specified type.
47 The argument to this call should be the type of object to be
48 created, e.g. foobar_blocktype.
49 type *Blocktype_alloc(b)
50 [MACRO] Allocate a block of the proper type for the specified
51 block-type object and return a pointer to it.
52 Blocktype_free(b, block)
53 Free a block of the type corresponding to the specified block-type
56 Destroy a block-type object and the memory allocated to it.
60 /* This file has been Mule-ized. */
65 #include "blocktype.h"
67 typedef struct blocktype {
68 Blocktype_declare(void);
71 struct block_internal {
75 void *Blocktype_newf(size_t elsize)
77 Blocktype *b = xnew(Blocktype);
78 b->elsize = max(elsize, sizeof(void *));
83 #if defined HAVE_BDWGC && defined EF_USE_BDWGC
84 void Blocktype_allocf(void *bbb)
86 ((Blocktype*)bbb)->tempel = (void*)xmalloc(((Blocktype*)bbb)->elsize);
90 void Blocktype_allocf(void *bbb)
92 Blocktype *b = (Blocktype *) bbb;
95 b->free = ((struct block_internal *)(b->free))->next;
97 b->tempel = (void *)xmalloc(b->elsize);
101 #if defined HAVE_BDWGC && defined EF_USE_BDWGC
102 void Blocktype_free(void *SXE_UNUSED(bbb), void *el)
108 void Blocktype_free(void *bbb, void *el)
110 Blocktype *b = (Blocktype *) bbb;
111 ((struct block_internal *)el)->next = b->free;