annotate src/blocktype.c @ 298:70ad99077275 r21-0b47

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