annotate src/dynarr.c @ 5166:b50624d3ae55

open-database.message
author Aidan Kehoe <kehoea@parhasard.net>
date Fri, 26 Mar 2010 15:06:28 +0000
parents 1fae11d56ad2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1318
b531bf8658e9 [xemacs-hg @ 2003-02-21 06:56:46 by ben]
ben
parents: 793
diff changeset
1 /* Support for dynamic arrays.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2 Copyright (C) 1993 Sun Microsystems, Inc.
4952
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
3 Copyright (C) 2002, 2003, 2004, 2005, 2010 Ben Wing.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5 This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 XEmacs is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 Free Software Foundation; either version 2, or (at your option) any
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10 later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12 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
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15 for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 along with XEmacs; see the file COPYING. If not, write to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20 Boston, MA 02111-1307, USA. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22 /* Synched up with: Not in FSF. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24 /* Written by Ben Wing, December 1993. */
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
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
28 A "dynamic array" or "dynarr" is a contiguous array of fixed-size elements
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
29 where there is no upper limit (except available memory) on the number of
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
30 elements in the array. Because the elements are maintained contiguously,
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
31 space is used efficiently (no per-element pointers necessary) and random
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
32 access to a particular element is in constant time. At any one point, the
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
33 block of memory that holds the array has an upper limit; if this limit is
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
34 exceeded, the memory is realloc()ed into a new array that is twice as big.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
35 Assuming that the time to grow the array is on the order of the new size of
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
36 the array block, this scheme has a provably constant amortized time
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
37 \(i.e. average time over all additions).
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 When you add elements or retrieve elements, pointers are used. Note that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 the element itself (of whatever size it is), and not the pointer to it,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 is stored in the array; thus you do not have to allocate any heap memory
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 on your own. Also, returned pointers are only guaranteed to be valid
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 until the next operation that changes the length of the array.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 This is a container object. Declare a dynamic array of a specific type
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 as follows:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
48 typedef struct
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
49 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
50 Dynarr_declare (mytype);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
51 } mytype_dynarr;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 Use the following functions/macros:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
55
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
56 ************* Dynarr creation *************
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
57
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 void *Dynarr_new(type)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 [MACRO] Create a new dynamic-array object, with each element of the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 specified type. The return value is cast to (type##_dynarr).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 This requires following the convention that types are declared in
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 such a way that this type concatenation works. In particular, TYPE
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
63 must be a symbol, not an arbitrary C type. To make dynarrs of
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
64 complex types, a typedef must be declared, e.g.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
65
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
66 typedef unsigned char *unsigned_char_ptr;
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
67
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
68 and then you can say
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
69
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
70 unsigned_char_ptr_dynarr *dyn = Dynarr_new (unsigned_char_ptr);
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
71
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
72 void *Dynarr_new2(dynarr_type, type)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
73 [MACRO] Create a new dynamic-array object, with each element of the
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
74 specified type. The array itself is of type DYNARR_TYPE. This makes
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
75 it possible to create dynarrs over complex types without the need
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
76 to create typedefs, as described above. Use is as follows:
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
77
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
78 ucharptr_dynarr *dyn = Dynarr_new2 (ucharptr_dynarr *, unsigned char *);
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
79
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
80 Dynarr_free(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
81 Destroy a dynamic array and the memory allocated to it.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
82
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
83 ************* Dynarr access *************
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
84
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
85 type Dynarr_at(d, i)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
86 [MACRO] Return the element at the specified index. The index must be
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
87 between 0 and Dynarr_largest(d), inclusive. With error-checking
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
88 enabled, bounds checking on the index is in the form of asserts() --
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
89 an out-of-bounds index causes an abort. The element itself is
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
90 returned, not a pointer to it.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
91
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
92 type *Dynarr_atp(d, i)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
93 [MACRO] Return a pointer to the element at the specified index.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
94 Restrictions and bounds checking on the index is as for Dynarr_at.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
95 The pointer may not be valid after an element is added to or
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
96 (conceivably) removed from the array, because this may trigger a
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
97 realloc() performed on the underlying dynarr storage, which may
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
98 involve moving the entire underlying storage to a new location in
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
99 memory.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
100
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
101 type *Dynarr_begin(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
102 [MACRO] Return a pointer to the first element in the dynarr. See
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
103 Dynarr_atp() for warnings about when the pointer might become invalid.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
104
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
105 type *Dynarr_lastp(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
106 [MACRO] Return a pointer to the last element in the dynarr. See
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
107 Dynarr_atp() for warnings about when the pointer might become invalid.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
108
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
109 type *Dynarr_past_lastp(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
110 [MACRO] Return a pointer to the beginning of the element just past the
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
111 last one. WARNING: This may not point to valid memory; however, the
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
112 byte directly before will be pointer will be valid memory. This macro
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
113 might be useful for various reasons, e.g. as a stopping point in a loop
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
114 (although Dynarr_lastp() could be used just as well) or as a place to
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
115 start writing elements if Dynarr_length() < Dynarr_largest().
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
116
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
117 ************* Dynarr length/size retrieval and setting *************
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
118
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
119 int Dynarr_length(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
120 [MACRO] Return the number of elements currently in a dynamic array.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
121
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
122 int Dynarr_largest(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
123 [MACRO] Return the maximum value that Dynarr_length(d) would
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
124 ever have returned. This is used esp. in the redisplay code,
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
125 which reuses dynarrs for performance reasons.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
126
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
127 int Dynarr_max(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
128 [MACRO] Return the maximum number of elements that can fit in the
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
129 dynarr before it needs to be resized.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
130
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
131 Note that Dynarr_length(d) <= Dynarr_largest(d) <= Dynarr_max(d).
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
132
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
133 Bytecount Dynarr_sizeof(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
134 [MACRO] Return the total size of the elements currently in dynarr
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
135 D. This
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
136
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
137 Dynarr_set_lengthr(d, len)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
138 [MACRO] Set the length of D to LEN, which must be between 0 and
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
139 Dynarr_largest(d), inclusive. With error-checking enabled, an
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
140 assertion failure will result from trying to set the length
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
141 to less than zero or greater than Dynarr_largest(d). The
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
142 restriction to Dynarr_largest() is to ensure that
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
143
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
144 Dynarr_set_length(d, len)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
145 [MACRO] Set the length of D to LEN, resizing the dynarr as
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
146 necessary to make sure enough space is available. there are no
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
147 restrictions on LEN other than available memory and that it must
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
148 be at least 0. Note that
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
149
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
150 Dynarr_set_length_and_zero(d, len)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
151 [MACRO] Like Dynarr_set_length(d, len) but also, if increasing
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
152 the length, zero out the memory between the old and new lengths,
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
153 i.e. starting just past the previous last element and up through
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
154 the new last element.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
155
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
156 Dynarr_incrementr(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
157 [MACRO] Increments the length of D by 1. Equivalent to
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
158 Dynarr_set_lengthr(d, Dynarr_length(d) + 1).
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
159
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
160 Dynarr_increment(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
161 [MACRO] Increments the length of D by 1. Equivalent to
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
162 Dynarr_set_length(d, Dynarr_length(d) + 1).
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
163
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
164 Dynarr_reset(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
165 [MACRO] Reset the length of a dynamic array to 0.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
166
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
167 Dynarr_resize(d, maxval)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
168 Resize the internal dynarr storage to so that it can hold at least
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
169 MAXVAL elements. Resizing is done using a geometric series
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
170 (repeatedly multiply the old maximum by a constant, normally 1.5,
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
171 till a large enough size is reached), so this will be efficient
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
172 even if resizing larger by one element at a time. This is mostly
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
173 an internal function.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
174
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
175
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
176
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
177 ************* Adding/deleting elements to/from a dynarr *************
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179 Dynarr_add(d, el)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 [MACRO] Add an element to the end of a dynamic array. EL is a pointer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 to the element; the element itself is stored in the array, however.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182 No function call is performed unless the array needs to be resized.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 Dynarr_add_many(d, base, len)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 [MACRO] Add LEN elements to the end of the dynamic array. The elements
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
186 should be contiguous in memory, starting at BASE. If BASE if NULL,
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
187 just make space for the elements; don't actually add them.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
189 Dynarr_prepend_many(d, base, len)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
190 [MACRO] Prepend LEN elements to the beginning of the dynamic array.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 The elements should be contiguous in memory, starting at BASE.
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
192 If BASE if NULL, just make space for the elements; don't actually
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
193 add them.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
195 Dynarr_insert_many(d, base, len, pos)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196 Insert LEN elements to the dynamic array starting at position
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
197 POS. The elements should be contiguous in memory, starting at BASE.
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
198 If BASE if NULL, just make space for the elements; don't actually
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
199 add them.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
200
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
201 type Dynarr_pop(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
202 [MACRO] Pop the last element off the dynarr and return it.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
203
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
204 Dynarr_delete(d, i)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
205 [MACRO] Delete an element from the dynamic array at position I.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
206
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
207 Dynarr_delete_many(d, pos, len)
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
208 Delete LEN elements from the dynamic array starting at position
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
209 POS.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
210
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
211 Dynarr_zero_many(d, pos, len)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
212 Zero out LEN elements in the dynarr D starting at position POS.
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
213
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
214 Dynarr_delete_by_pointer(d, p)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
215 [MACRO] Delete an element from the dynamic array at pointer P,
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
216 which must point within the block of memory that stores the data.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
217 P should be obtained using Dynarr_atp().
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
219 ************* Dynarr locking *************
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
221 Dynarr_lock(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
222 Lock the dynarr against further locking or writing. With error-checking
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
223 enabled, any attempts to write into a locked dynarr or re-lock an
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
224 already locked one will cause an assertion failure and abort.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
226 Dynarr_unlock(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
227 Unlock a locked dynarr, allowing writing into it.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
229 ************* Dynarr global variables *************
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 Dynarr_min_size
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
232 Minimum allowable size for a dynamic array when it is resized.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 #include <config.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237 #include "lisp.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238
4952
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
239 static const struct memory_description const_Ascbyte_ptr_description_1[] = {
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
240 { XD_ASCII_STRING, 0 },
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
241 { XD_END }
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
242 };
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
243
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
244 const struct sized_memory_description const_Ascbyte_ptr_description = {
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
245 sizeof (const Ascbyte *),
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
246 const_Ascbyte_ptr_description_1
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
247 };
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
248
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
249 static const struct memory_description const_Ascbyte_ptr_dynarr_description_1[] = {
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
250 XD_DYNARR_DESC (const_Ascbyte_ptr_dynarr, &const_Ascbyte_ptr_description),
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
251 { XD_END }
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
252 };
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
253
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
254 const struct sized_memory_description const_Ascbyte_ptr_dynarr_description = {
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
255 sizeof (const_Ascbyte_ptr_dynarr),
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
256 const_Ascbyte_ptr_dynarr_description_1
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
257 };
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
258
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
259
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
260 static Elemcount Dynarr_min_size = 8;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262 static void
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
263 Dynarr_realloc (Dynarr *dy, Elemcount new_size)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 if (DUMPEDP (dy->base))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266 {
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
267 void *new_base = malloc (new_size * Dynarr_elsize (dy));
3210
72b7d685c194 [xemacs-hg @ 2006-01-20 17:59:48 by crestani]
crestani
parents: 3092
diff changeset
268 memcpy (new_base, dy->base,
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
269 (Dynarr_max (dy) < new_size ? Dynarr_max (dy) : new_size) *
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
270 Dynarr_elsize (dy));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271 dy->base = new_base;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 else
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
274 dy->base = xrealloc (dy->base, new_size * Dynarr_elsize (dy));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
277 void *
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
278 Dynarr_newf (Bytecount elsize)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
279 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
280 Dynarr *d = xnew_and_zero (Dynarr);
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
281 d->elsize_ = elsize;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283 return d;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
286 #ifdef NEW_GC
5118
e0db3c197671 merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
287 DEFINE_DUMPABLE_INTERNAL_LISP_OBJECT ("dynarr", dynarr,
e0db3c197671 merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
288 0, 0,
e0db3c197671 merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
289 Dynarr);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
290
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
291 static void
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
292 Dynarr_lisp_realloc (Dynarr *dy, Elemcount new_size)
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
293 {
5118
e0db3c197671 merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
294 void *new_base =
5126
Ben Wing <ben@xemacs.org>
parents: 5125 5038
diff changeset
295 XPNTR (alloc_sized_lrecord_array (Dynarr_elsize (dy), new_size,
Ben Wing <ben@xemacs.org>
parents: 5125 5038
diff changeset
296 dy->lisp_imp));
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
297 if (dy->base)
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
298 memcpy (new_base, dy->base,
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
299 (Dynarr_max (dy) < new_size ? Dynarr_max (dy) : new_size) *
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
300 Dynarr_elsize (dy));
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
301 dy->base = new_base;
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
302 }
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
303
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
304 void *
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
305 Dynarr_lisp_newf (Bytecount elsize,
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
306 const struct lrecord_implementation *dynarr_imp,
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
307 const struct lrecord_implementation *imp)
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
308 {
5120
d1247f3cc363 latest work on lisp-object workspace;
Ben Wing <ben@xemacs.org>
parents: 5118
diff changeset
309 Dynarr *d = (Dynarr *) XPNTR (alloc_sized_lrecord (sizeof (Dynarr),
d1247f3cc363 latest work on lisp-object workspace;
Ben Wing <ben@xemacs.org>
parents: 5118
diff changeset
310 dynarr_imp));
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
311 d->elsize_ = elsize;
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
312 d->lisp_imp = imp;
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
313
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
314 return d;
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
315 }
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
316 #endif /* not NEW_GC */
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
317
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318 void
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
319 Dynarr_resize (void *d, Elemcount size)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 {
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
321 Elemcount newsize;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 double multiplier;
1318
b531bf8658e9 [xemacs-hg @ 2003-02-21 06:56:46 by ben]
ben
parents: 793
diff changeset
323 Dynarr *dy = (Dynarr *) Dynarr_verify (d);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
325 if (Dynarr_max (dy) <= 8)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 multiplier = 2;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 multiplier = 1.5;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
330 for (newsize = Dynarr_max (dy); newsize < size;)
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
331 newsize = max (Dynarr_min_size, (Elemcount) (multiplier * newsize));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 /* Don't do anything if the array is already big enough. */
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
334 if (newsize > Dynarr_max (dy))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 {
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
336 #ifdef NEW_GC
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
337 if (dy->lisp_imp)
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
338 Dynarr_lisp_realloc (dy, newsize);
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
339 else
3210
72b7d685c194 [xemacs-hg @ 2006-01-20 17:59:48 by crestani]
crestani
parents: 3092
diff changeset
340 Dynarr_realloc (dy, newsize);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
341 #else /* not NEW_GC */
3210
72b7d685c194 [xemacs-hg @ 2006-01-20 17:59:48 by crestani]
crestani
parents: 3092
diff changeset
342 Dynarr_realloc (dy, newsize);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
343 #endif /* not NEW_GC */
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
344 dy->max_ = newsize;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
345 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
348 /* Add a number of contiguous elements to the array starting at POS. */
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
349
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350 void
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
351 Dynarr_insert_many (void *d, const void *base, Elemcount len, Elemcount pos)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 {
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
353 Dynarr *dy = Dynarr_verify_mod (d);
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
354 Elemcount old_len = Dynarr_length (dy);
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
355
4844
91b3d00e717f Various cleanups for Dynarr code, from Unicode-internal ws
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
356 /* #### This could conceivably be wrong, if code wants to access stuff
91b3d00e717f Various cleanups for Dynarr code, from Unicode-internal ws
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
357 between len and largest. */
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
358 dynarr_checking_assert (pos >= 0 && pos <= old_len);
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
359 dynarr_checking_assert (len >= 0);
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
360 Dynarr_increase_length (dy, old_len + len);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
362 if (pos != old_len)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 {
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
364 memmove ((Rawbyte *) dy->base + (pos + len)*Dynarr_elsize (dy),
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
365 (Rawbyte *) dy->base + pos*Dynarr_elsize (dy),
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
366 (old_len - pos)*Dynarr_elsize (dy));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367 }
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
368 /* Some functions call us with a value of 0 to mean "reserve space but
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
369 don't write into it" */
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
370 if (base)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
371 memcpy ((Rawbyte *) dy->base + pos*Dynarr_elsize (dy), base,
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
372 len*Dynarr_elsize (dy));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
373 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375 void
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
376 Dynarr_delete_many (void *d, Elemcount pos, Elemcount len)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377 {
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
378 Dynarr *dy = Dynarr_verify_mod (d);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
380 dynarr_checking_assert (pos >= 0 && len >= 0 &&
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
381 pos + len <= Dynarr_length (dy));
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
382
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
383 memmove ((Rawbyte *) dy->base + pos*Dynarr_elsize (dy),
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
384 (Rawbyte *) dy->base + (pos + len)*Dynarr_elsize (dy),
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
385 (Dynarr_length (dy) - pos - len)*Dynarr_elsize (dy));
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
386
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
387 Dynarr_set_length_1 (dy, Dynarr_length (dy) - len);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
390 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391 Dynarr_free (void *d)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393 Dynarr *dy = (Dynarr *) d;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
394
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
395 #ifdef NEW_GC
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
396 if (dy->base && !DUMPEDP (dy->base))
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
397 {
4117
229bd619740a [xemacs-hg @ 2007-08-15 11:06:02 by crestani]
crestani
parents: 3293
diff changeset
398 if (!dy->lisp_imp)
4976
16112448d484 Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents: 4967
diff changeset
399 xfree (dy->base);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
400 }
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
401 if(!DUMPEDP (dy))
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
402 {
4117
229bd619740a [xemacs-hg @ 2007-08-15 11:06:02 by crestani]
crestani
parents: 3293
diff changeset
403 if (!dy->lisp_imp)
4976
16112448d484 Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents: 4967
diff changeset
404 xfree (dy);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
405 }
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
406 #else /* not NEW_GC */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407 if (dy->base && !DUMPEDP (dy->base))
4976
16112448d484 Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents: 4967
diff changeset
408 xfree (dy->base);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409 if(!DUMPEDP (dy))
4976
16112448d484 Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents: 4967
diff changeset
410 xfree (dy);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
411 #endif /* not NEW_GC */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
412 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
413
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
414 #ifdef MEMORY_USAGE_STATS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
415
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
416 /* Return memory usage for dynarr D. The returned value is the total
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
417 amount of bytes actually being used for the dynarr, including all
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
418 overhead. The extra amount of space in the dynarr that is
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419 allocated beyond what was requested is returned in DYNARR_OVERHEAD
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
420 in STATS. The extra amount of space that malloc() allocates beyond
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
421 what was requested of it is returned in MALLOC_OVERHEAD in STATS.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422 See the comment above the definition of this structure. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423
665
fdefd0186b75 [xemacs-hg @ 2001-09-20 06:28:42 by ben]
ben
parents: 647
diff changeset
424 Bytecount
5157
1fae11d56ad2 redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents: 5126
diff changeset
425 Dynarr_memory_usage (void *d, struct usage_stats *stats)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426 {
665
fdefd0186b75 [xemacs-hg @ 2001-09-20 06:28:42 by ben]
ben
parents: 647
diff changeset
427 Bytecount total = 0;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428 Dynarr *dy = (Dynarr *) d;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 /* We have to be a bit tricky here because not all of the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 memory that malloc() will claim as "requested" was actually
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432 requested. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 if (dy->base)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 {
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
436 Bytecount malloc_used =
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
437 malloced_storage_size (dy->base, Dynarr_elsize (dy) * Dynarr_max (dy),
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
438 0);
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
439 /* #### This may or may not be correct. Some dynarrs would
4844
91b3d00e717f Various cleanups for Dynarr code, from Unicode-internal ws
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
440 prefer that we use dy->len instead of dy->largest here. */
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
441 Bytecount was_requested = Dynarr_elsize (dy) * Dynarr_largest (dy);
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
442 Bytecount dynarr_overhead =
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
443 Dynarr_elsize (dy) * (Dynarr_max (dy) - Dynarr_largest (dy));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445 total += malloc_used;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446 stats->was_requested += was_requested;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 stats->dynarr_overhead += dynarr_overhead;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448 /* And the remainder must be malloc overhead. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449 stats->malloc_overhead +=
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
450 malloc_used - was_requested - dynarr_overhead;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 total += malloced_storage_size (d, sizeof (*dy), stats);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455 return total;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
458 #endif /* MEMORY_USAGE_STATS */
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
459
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
460 /* Version of malloc() that will be extremely efficient when allocation
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
461 nearly always occurs in LIFO (stack) order.
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
462
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
463 #### Perhaps shouldn't be in this file, but where else? */
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
464
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
465 typedef struct
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
466 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
467 Dynarr_declare (char_dynarr *);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
468 } char_dynarr_dynarr;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
469
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
470 char_dynarr_dynarr *stack_like_free_list;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
471 char_dynarr_dynarr *stack_like_in_use_list;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
472
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
473 void *
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
474 stack_like_malloc (Bytecount size)
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
475 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
476 char_dynarr *this_one;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
477 if (!stack_like_free_list)
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
478 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
479 stack_like_free_list = Dynarr_new2 (char_dynarr_dynarr,
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
480 char_dynarr *);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
481 stack_like_in_use_list = Dynarr_new2 (char_dynarr_dynarr,
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
482 char_dynarr *);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
483 }
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
484
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
485 if (Dynarr_length (stack_like_free_list) > 0)
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
486 this_one = Dynarr_pop (stack_like_free_list);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
487 else
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
488 this_one = Dynarr_new (char);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
489 Dynarr_add (stack_like_in_use_list, this_one);
4844
91b3d00e717f Various cleanups for Dynarr code, from Unicode-internal ws
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
490 Dynarr_reset (this_one);
91b3d00e717f Various cleanups for Dynarr code, from Unicode-internal ws
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
491 Dynarr_add_many (this_one, 0, size);
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
492 return Dynarr_begin (this_one);
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
493 }
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
494
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
495 void
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
496 stack_like_free (void *val)
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
497 {
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
498 Elemcount len = Dynarr_length (stack_like_in_use_list);
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
499 assert (len > 0);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
500 /* The vast majority of times, we will be called in a last-in first-out
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
501 order, and the item at the end of the list will be the one we're
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
502 looking for, so just check for this first and avoid any function
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
503 calls. */
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
504 if (Dynarr_begin (Dynarr_at (stack_like_in_use_list, len - 1)) == val)
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
505 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
506 char_dynarr *this_one = Dynarr_pop (stack_like_in_use_list);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
507 Dynarr_add (stack_like_free_list, this_one);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
508 }
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
509 else
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
510 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
511 /* Find the item and delete it. */
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
512 int i;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
513 assert (len >= 2);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
514 for (i = len - 2; i >= 0; i--)
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
515 if (Dynarr_begin (Dynarr_at (stack_like_in_use_list, i)) ==
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
516 val)
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
517 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
518 char_dynarr *this_one = Dynarr_at (stack_like_in_use_list, i);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
519 Dynarr_add (stack_like_free_list, this_one);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
520 Dynarr_delete (stack_like_in_use_list, i);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
521 return;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
522 }
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
523
2500
3d8143fc88e1 [xemacs-hg @ 2005-01-24 23:33:30 by ben]
ben
parents: 2367
diff changeset
524 ABORT ();
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
525 }
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
526 }