Partially sync files.el from XEmacs 21.5 for wildcard support.
[sxemacs] / src / mem / blocktype.c
1 /* Fixed-size block allocator.
2    Copyright (C) 1994 Free Software Foundation, Inc.
3
4 This file is part of SXEmacs
5
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.
10
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.
15
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/>. */
18
19
20 /* Synched up with: Not in FSF. */
21
22 /* Authorship:
23
24    Ben Wing: December 1994, for 19.12.
25  */
26
27 /*
28
29 ------------------------------------------------------------------------------
30
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().
35
36 This is a container object.  Declare a block-type object of a specific type
37 as follows:
38
39 struct mytype_blocktype {
40   Blocktype_declare (mytype);
41 };
42
43 Use the following functions/macros:
44
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
54       object.
55    Blocktype_delete(b)
56       Destroy a block-type object and the memory allocated to it.
57
58 */
59
60 /* This file has been Mule-ized. */
61
62 #include <config.h>
63 #include "lisp.h"
64
65 #include "blocktype.h"
66
67 typedef struct blocktype {
68         Blocktype_declare(void);
69 } Blocktype;
70
71 struct block_internal {
72         void *next;
73 };
74
75 void *Blocktype_newf(size_t elsize)
76 {
77         Blocktype *b = xnew(Blocktype);
78         b->elsize = max(elsize, sizeof(void *));
79         b->free = 0;
80         return (void *)b;
81 }
82
83 #if defined HAVE_BDWGC && defined EF_USE_BDWGC
84 void Blocktype_allocf(void *bbb)
85 {
86         ((Blocktype*)bbb)->tempel = (void*)xmalloc(((Blocktype*)bbb)->elsize);
87         return;
88 }
89 #else  /* !BDWGC */
90 void Blocktype_allocf(void *bbb)
91 {
92         Blocktype *b = (Blocktype *) bbb;
93         if (b->free) {
94                 b->tempel = b->free;
95                 b->free = ((struct block_internal *)(b->free))->next;
96         } else
97                 b->tempel = (void *)xmalloc(b->elsize);
98 }
99 #endif  /* BDWGC */
100
101 #if defined HAVE_BDWGC && defined EF_USE_BDWGC
102 void Blocktype_free(void *SXE_UNUSED(bbb), void *el)
103 {
104         xfree(el);
105         return;
106 }
107 #else  /* !BDWGC */
108 void Blocktype_free(void *bbb, void *el)
109 {
110         Blocktype *b = (Blocktype *) bbb;
111         ((struct block_internal *)el)->next = b->free;
112         b->free = el;
113 }
114 #endif  /* BDWGC */