annotate src/blocktype.c @ 5894:23178aa71f8b

Define ALIGNOF using C11 and C++11 operators. See <CAHCOHQmG51R61KwGUNY7T5t9tXxzbyg=aGijUKYstbE+wL2-6Q@mail.gmail.com> in xemacs-patches for more information.
author Jerry James <james@xemacs.org>
date Mon, 20 Apr 2015 15:09:11 -0600
parents 308d34e9f07d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 /* Fixed-size block allocator.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2 Copyright (C) 1994 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4 This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5
5402
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 665
diff changeset
6 XEmacs is free software: you can redistribute it and/or modify it
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
5402
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 665
diff changeset
8 Free Software Foundation, either version 3 of the License, or (at your
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 665
diff changeset
9 option) any later version.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
5402
308d34e9f07d Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents: 665
diff changeset
17 along with XEmacs. If not, see <http://www.gnu.org/licenses/>. */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 /* Synched up with: Not in FSF. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21 /* Authorship:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 Ben Wing: December 1994, for 19.12.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28 ------------------------------------------------------------------------------
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 A "block-type object" is used to efficiently allocate and free blocks
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 of a particular size. Freed blocks are remembered in a free list and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 are reused as necessary to allocate new blocks, so as to avoid as
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 much as possible making calls to malloc() and free().
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 This is a container object. Declare a block-type object of a specific type
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 as follows:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 struct mytype_blocktype {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 Blocktype_declare (mytype);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 };
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 Use the following functions/macros:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 structype *Blocktype_new(structype)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 [MACRO] Create a new block-type object of the specified type.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 The argument to this call should be the type of object to be
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47 created, e.g. foobar_blocktype.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 type *Blocktype_alloc(b)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 [MACRO] Allocate a block of the proper type for the specified
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 block-type object and return a pointer to it.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 Blocktype_free(b, block)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 Free a block of the type corresponding to the specified block-type
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 object.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 Blocktype_delete(b)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 Destroy a block-type object and the memory allocated to it.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 /* This file has been Mule-ized. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 #include <config.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 #include "lisp.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 #include "blocktype.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 typedef struct blocktype
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 Blocktype_declare (void);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 } Blocktype;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 struct block_internal
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 void *next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 };
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 void *
665
fdefd0186b75 [xemacs-hg @ 2001-09-20 06:28:42 by ben]
ben
parents: 647
diff changeset
77 Blocktype_newf (Bytecount elsize)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 Blocktype *b = xnew (Blocktype);
665
fdefd0186b75 [xemacs-hg @ 2001-09-20 06:28:42 by ben]
ben
parents: 647
diff changeset
80 b->elsize = max (elsize, (Bytecount) sizeof (void *));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 b->free = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 return (void *) b;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86 Blocktype_allocf (void *bbb)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 Blocktype *b = (Blocktype *) bbb;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89 if (b->free)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 b->tempel = b->free;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 b->free = ((struct block_internal *) (b->free))->next;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 b->tempel = (void *) xmalloc (b->elsize);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 Blocktype_free (void *bbb, void *el)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101 Blocktype *b = (Blocktype *) bbb;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 ((struct block_internal *) el)->next = b->free;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103 b->free = el;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 }