annotate src/dynarr.c @ 5125:b5df3737028a ben-lisp-object

merge
author Ben Wing <ben@xemacs.org>
date Wed, 24 Feb 2010 01:58:04 -0600
parents d1247f3cc363 838630c0734f
children 2a462149bd6a
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28 A "dynamic array" is a contiguous array of fixed-size elements where there
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29 is no upper limit (except available memory) on the number of elements in the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 array. Because the elements are maintained contiguously, space is used
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 efficiently (no per-element pointers necessary) and random access to a
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 particular element is in constant time. At any one point, the block of memory
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 that holds the array has an upper limit; if this limit is exceeded, the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 memory is realloc()ed into a new array that is twice as big. Assuming that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 the time to grow the array is on the order of the new size of the array
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 block, this scheme has a provably constant amortized time (i.e. average
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 time over all additions).
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 void *Dynarr_new(type)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 [MACRO] Create a new dynamic-array object, with each element of the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 specified type. The return value is cast to (type##_dynarr).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 This requires following the convention that types are declared in
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 such a way that this type concatenation works. In particular, TYPE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 must be a symbol, not an arbitrary C type.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 Dynarr_add(d, el)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 [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
64 to the element; the element itself is stored in the array, however.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65 No function call is performed unless the array needs to be resized.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 Dynarr_add_many(d, base, len)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 [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
69 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
70 just make space for the elements; don't actually add them.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 Dynarr_insert_many_at_start(d, base, len)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 [MACRO] Append LEN elements to the beginning of the dynamic array.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 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
75 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
76 add them.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 Dynarr_insert_many(d, base, len, start)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 Insert LEN elements to the dynamic array starting at position
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 START. 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
81 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
82 add them.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
83
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
84 Dynarr_delete(d, i)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
85 [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
86
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
87 Dynarr_delete_many(d, start, len)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
88 Delete LEN elements from the dynamic array starting at position
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
89 START.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
90
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
91 Dynarr_delete_by_pointer(d, p)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
92 [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
93 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
94 P should be obtained using Dynarr_atp().
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 int Dynarr_length(d)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 [MACRO] Return the number of elements currently in a dynamic array.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 int Dynarr_largest(d)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 [MACRO] Return the maximum value that Dynarr_length(d) would
4844
91b3d00e717f Various cleanups for Dynarr code, from Unicode-internal ws
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
101 ever have returned. This is used esp. in the redisplay code,
91b3d00e717f Various cleanups for Dynarr code, from Unicode-internal ws
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
102 which reuses dynarrs for performance reasons.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 type Dynarr_at(d, i)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105 [MACRO] Return the element at the specified index (no bounds checking
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106 done on the index). The element itself is returned, not a pointer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 to it.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109 type *Dynarr_atp(d, i)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110 [MACRO] Return a pointer to the element at the specified index (no
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 bounds checking done on the index). The pointer may not be valid
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 after an element is added to or removed from the array.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114 Dynarr_reset(d)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115 [MACRO] Reset the length of a dynamic array to 0.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 Dynarr_free(d)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 Destroy a dynamic array and the memory allocated to it.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120 Use the following global variable:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122 Dynarr_min_size
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
123 Minimum allowable size for a dynamic array when it is resized.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 #include <config.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128 #include "lisp.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129
4952
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
130 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
131 { XD_ASCII_STRING, 0 },
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
132 { XD_END }
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
133 };
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
134
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
135 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
136 sizeof (const Ascbyte *),
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
137 const_Ascbyte_ptr_description_1
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
138 };
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
139
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
140 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
141 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
142 { XD_END }
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
143 };
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
144
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
145 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
146 sizeof (const_Ascbyte_ptr_dynarr),
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
147 const_Ascbyte_ptr_dynarr_description_1
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
148 };
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
149
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
150
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
151 static int Dynarr_min_size = 8;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 static void
3210
72b7d685c194 [xemacs-hg @ 2006-01-20 17:59:48 by crestani]
crestani
parents: 3092
diff changeset
154 Dynarr_realloc (Dynarr *dy, int new_size)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156 if (DUMPEDP (dy->base))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157 {
3293
168191f9515f [xemacs-hg @ 2006-03-20 21:20:19 by crestani]
crestani
parents: 3210
diff changeset
158 void *new_base = malloc (new_size * dy->elsize);
3210
72b7d685c194 [xemacs-hg @ 2006-01-20 17:59:48 by crestani]
crestani
parents: 3092
diff changeset
159 memcpy (new_base, dy->base,
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
160 (Dynarr_max (dy) < new_size ? Dynarr_max (dy) : new_size) *
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
161 dy->elsize);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162 dy->base = new_base;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164 else
3210
72b7d685c194 [xemacs-hg @ 2006-01-20 17:59:48 by crestani]
crestani
parents: 3092
diff changeset
165 dy->base = xrealloc (dy->base, new_size * dy->elsize);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 void *
5118
e0db3c197671 merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
169 Dynarr_newf (Bytecount elsize)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 Dynarr *d = xnew_and_zero (Dynarr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172 d->elsize = elsize;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174 return d;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
177 #ifdef NEW_GC
5118
e0db3c197671 merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
178 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
179 0, 0,
e0db3c197671 merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
180 Dynarr);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
181
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
182 static void
5118
e0db3c197671 merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
183 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
184 {
5118
e0db3c197671 merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
185 void *new_base =
e0db3c197671 merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
186 XPNTR (alloc_sized_lrecord_array (dy->elsize, new_size, dy->lisp_imp));
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
187 if (dy->base)
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
188 memcpy (new_base, dy->base,
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
189 (Dynarr_max (dy) < new_size ? Dynarr_max (dy) : new_size) *
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
190 dy->elsize);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
191 dy->base = new_base;
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
192 }
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
193
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
194 void *
5118
e0db3c197671 merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
195 Dynarr_lisp_newf (Bytecount elsize,
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
196 const struct lrecord_implementation *dynarr_imp,
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
197 const struct lrecord_implementation *imp)
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
198 {
5120
d1247f3cc363 latest work on lisp-object workspace;
Ben Wing <ben@xemacs.org>
parents: 5118
diff changeset
199 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
200 dynarr_imp));
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
201 d->elsize = elsize;
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
202 d->lisp_imp = imp;
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
203
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
204 return d;
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
205 }
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
206 #endif /* not NEW_GC */
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
207
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 void
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
209 Dynarr_resize (void *d, Elemcount size)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 int newsize;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212 double multiplier;
1318
b531bf8658e9 [xemacs-hg @ 2003-02-21 06:56:46 by ben]
ben
parents: 793
diff changeset
213 Dynarr *dy = (Dynarr *) Dynarr_verify (d);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
215 if (Dynarr_max (dy) <= 8)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 multiplier = 2;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 multiplier = 1.5;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
220 for (newsize = Dynarr_max (dy); newsize < size;)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221 newsize = max (Dynarr_min_size, (int) (multiplier * newsize));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223 /* 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
224 if (newsize > Dynarr_max (dy))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225 {
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
226 #ifdef NEW_GC
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
227 if (dy->lisp_imp)
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
228 Dynarr_lisp_realloc (dy, newsize);
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
229 else
3210
72b7d685c194 [xemacs-hg @ 2006-01-20 17:59:48 by crestani]
crestani
parents: 3092
diff changeset
230 Dynarr_realloc (dy, newsize);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
231 #else /* not NEW_GC */
3210
72b7d685c194 [xemacs-hg @ 2006-01-20 17:59:48 by crestani]
crestani
parents: 3092
diff changeset
232 Dynarr_realloc (dy, newsize);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
233 #endif /* not NEW_GC */
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
234 dy->max_ = newsize;
428
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 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238 /* Add a number of contiguous elements to the array starting at START. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239 void
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
240 Dynarr_insert_many (void *d, const void *el, int len, int start)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241 {
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
242 Dynarr *dy = Dynarr_verify_mod (d);
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
243
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
244 Dynarr_resize_if (dy, len);
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
245
4844
91b3d00e717f Various cleanups for Dynarr code, from Unicode-internal ws
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
246 /* #### 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
247 between len and largest. */
5023
838630c0734f error-checking, Windows shutdown changes
Ben Wing <ben@xemacs.org>
parents: 4976
diff changeset
248 dynarr_checking_assert (start >= 0 && start <= Dynarr_length (dy));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
250 if (start != Dynarr_length (dy))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 memmove ((char *) dy->base + (start + len)*dy->elsize,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 (char *) dy->base + start*dy->elsize,
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
254 (Dynarr_length (dy) - start)*dy->elsize);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 }
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
256 /* 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
257 don't write into it" */
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
258 if (el)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
259 memcpy ((char *) dy->base + start*dy->elsize, el, len*dy->elsize);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
261 Dynarr_set_length_1 (dy, Dynarr_length (dy) + len);
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
262 (void) Dynarr_verify_mod (dy);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263 }
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 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266 Dynarr_delete_many (void *d, int start, int len)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
267 {
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
268 Dynarr *dy = Dynarr_verify_mod (d);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269
5023
838630c0734f error-checking, Windows shutdown changes
Ben Wing <ben@xemacs.org>
parents: 4976
diff changeset
270 dynarr_checking_assert (start >= 0 && len >= 0 &&
838630c0734f error-checking, Windows shutdown changes
Ben Wing <ben@xemacs.org>
parents: 4976
diff changeset
271 start + len <= Dynarr_length (dy));
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
272
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 memmove ((char *) dy->base + start*dy->elsize,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 (char *) dy->base + (start + len)*dy->elsize,
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
275 (Dynarr_length (dy) - start - len)*dy->elsize);
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
276
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
277 Dynarr_set_length_1 (dy, Dynarr_length (dy) - len);
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
278 (void) Dynarr_verify_mod (dy);
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282 Dynarr_free (void *d)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 Dynarr *dy = (Dynarr *) d;
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
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
287 if (dy->base && !DUMPEDP (dy->base))
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
288 {
4117
229bd619740a [xemacs-hg @ 2007-08-15 11:06:02 by crestani]
crestani
parents: 3293
diff changeset
289 if (!dy->lisp_imp)
4976
16112448d484 Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents: 4967
diff changeset
290 xfree (dy->base);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
291 }
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
292 if(!DUMPEDP (dy))
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
293 {
4117
229bd619740a [xemacs-hg @ 2007-08-15 11:06:02 by crestani]
crestani
parents: 3293
diff changeset
294 if (!dy->lisp_imp)
4976
16112448d484 Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents: 4967
diff changeset
295 xfree (dy);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
296 }
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
297 #else /* not NEW_GC */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 if (dy->base && !DUMPEDP (dy->base))
4976
16112448d484 Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents: 4967
diff changeset
299 xfree (dy->base);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300 if(!DUMPEDP (dy))
4976
16112448d484 Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents: 4967
diff changeset
301 xfree (dy);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
302 #endif /* not NEW_GC */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305 #ifdef MEMORY_USAGE_STATS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
306
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
307 /* Return memory usage for Dynarr D. The returned value is the total
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
308 amount of bytes actually being used for the Dynarr, including all
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
309 overhead. The extra amount of space in the Dynarr that is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
310 allocated beyond what was requested is returned in DYNARR_OVERHEAD
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
311 in STATS. The extra amount of space that malloc() allocates beyond
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312 what was requested of it is returned in MALLOC_OVERHEAD in STATS.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313 See the comment above the definition of this structure. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314
665
fdefd0186b75 [xemacs-hg @ 2001-09-20 06:28:42 by ben]
ben
parents: 647
diff changeset
315 Bytecount
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316 Dynarr_memory_usage (void *d, struct overhead_stats *stats)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317 {
665
fdefd0186b75 [xemacs-hg @ 2001-09-20 06:28:42 by ben]
ben
parents: 647
diff changeset
318 Bytecount total = 0;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 Dynarr *dy = (Dynarr *) d;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321 /* 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
322 memory that malloc() will claim as "requested" was actually
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 requested. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325 if (dy->base)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 {
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
327 Bytecount malloc_used =
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
328 malloced_storage_size (dy->base, dy->elsize * Dynarr_max (dy), 0);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 /* #### 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
330 prefer that we use dy->len instead of dy->largest here. */
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
331 Bytecount was_requested = dy->elsize * Dynarr_largest (dy);
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
332 Bytecount dynarr_overhead =
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
333 dy->elsize * (Dynarr_max (dy) - Dynarr_largest (dy));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 total += malloc_used;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336 stats->was_requested += was_requested;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337 stats->dynarr_overhead += dynarr_overhead;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338 /* And the remainder must be malloc overhead. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 stats->malloc_overhead +=
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340 malloc_used - was_requested - dynarr_overhead;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343 total += malloced_storage_size (d, sizeof (*dy), stats);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
344
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
345 return total;
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348 #endif /* MEMORY_USAGE_STATS */
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
349
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
350 /* 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
351 nearly always occurs in LIFO (stack) order.
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
352
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
353 #### 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
354
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
355 typedef struct
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
356 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
357 Dynarr_declare (char_dynarr *);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
358 } char_dynarr_dynarr;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
359
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
360 char_dynarr_dynarr *stack_like_free_list;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
361 char_dynarr_dynarr *stack_like_in_use_list;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
362
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
363 void *
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
364 stack_like_malloc (Bytecount size)
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
365 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
366 char_dynarr *this_one;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
367 if (!stack_like_free_list)
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
368 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
369 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
370 char_dynarr *);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
371 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
372 char_dynarr *);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
373 }
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
374
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
375 if (Dynarr_length (stack_like_free_list) > 0)
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
376 this_one = Dynarr_pop (stack_like_free_list);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
377 else
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
378 this_one = Dynarr_new (char);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
379 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
380 Dynarr_reset (this_one);
91b3d00e717f Various cleanups for Dynarr code, from Unicode-internal ws
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
381 Dynarr_add_many (this_one, 0, size);
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
382 return Dynarr_begin (this_one);
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
383 }
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
384
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
385 void
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
386 stack_like_free (void *val)
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
387 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
388 int len = Dynarr_length (stack_like_in_use_list);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
389 assert (len > 0);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
390 /* 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
391 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
392 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
393 calls. */
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
394 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
395 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
396 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
397 Dynarr_add (stack_like_free_list, this_one);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
398 }
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
399 else
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
400 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
401 /* Find the item and delete it. */
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
402 int i;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
403 assert (len >= 2);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
404 for (i = len - 2; i >= 0; i--)
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
405 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
406 val)
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
407 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
408 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
409 Dynarr_add (stack_like_free_list, this_one);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
410 Dynarr_delete (stack_like_in_use_list, i);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
411 return;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
412 }
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
413
2500
3d8143fc88e1 [xemacs-hg @ 2005-01-24 23:33:30 by ben]
ben
parents: 2367
diff changeset
414 ABORT ();
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
415 }
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
416 }