annotate src/array.c @ 5196:c87ec061e8cc

Fix compilation --without-debug. src/ChangeLog addition: 2010-04-09 Didier Verna <didier@xemacs.org> * fontcolor-xlike-inc.c (DEBUG_FONTS2): * fontcolor-xlike-inc.c (DEBUG_FONTS3): * fontcolor-xlike-inc.c (DEBUG_FONTS4): * fontcolor-xlike-inc.c (DEBUG_FONTS_LISP1): * fontcolor-xlike-inc.c (DEBUG_FONTS_LISP2): * fontcolor-xlike-inc.c (DEBUG_FONTS_LISP3): * fontcolor-xlike-inc.c (DEBUG_FONTS_LISP4): Conditionalize definitions on DEBUG_XEMACS, provide empty definitions otherwise.
author Didier Verna <didier@xemacs.org>
date Fri, 09 Apr 2010 14:51:11 +0200
parents 5ddbab03b0e6
children 308d34e9f07d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5168
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
1 /* Support for dynarrs and other types of dynamic arrays.
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
2 Copyright (c) 1994, 1995 Free Software Foundation, Inc.
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
3 Copyright (c) 1993, 1995 Sun Microsystems, Inc.
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
4 Copyright (c) 1995, 1996, 2000, 2002, 2003, 2004, 2005, 2010 Ben Wing.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 XEmacs is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 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
10 Free Software Foundation; either version 2, or (at your option) any
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11 later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 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
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16 for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 along with XEmacs; see the file COPYING. If not, write to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21 Boston, MA 02111-1307, USA. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 /* Synched up with: Not in FSF. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 /* Written by Ben Wing, December 1993. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26
5168
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
27 #include <config.h>
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
28 #include "lisp.h"
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
29
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
30 #include "insdel.h"
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
31
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
32
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
33 /*****************************************************************************/
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
34 /* "dynarr" a.k.a. dynamic array */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
35 /*****************************************************************************/
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
36
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 /*
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
38 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
39 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
40 elements in the array. Because the elements are maintained contiguously,
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
41 space is used efficiently (no per-element pointers necessary) and random
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
42 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
43 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
44 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
45 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
46 the array block, this scheme has a provably constant amortized time
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
47 \(i.e. average time over all additions).
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 When you add elements or retrieve elements, pointers are used. Note that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 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
51 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
52 on your own. Also, returned pointers are only guaranteed to be valid
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 until the next operation that changes the length of the array.
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 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
56 as follows:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
58 typedef struct
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
59 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
60 Dynarr_declare (mytype);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
61 } mytype_dynarr;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 Use the following functions/macros:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64
5038
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 ************* Dynarr creation *************
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
67
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 void *Dynarr_new(type)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 [MACRO] Create a new dynamic-array object, with each element of the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 specified type. The return value is cast to (type##_dynarr).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 This requires following the convention that types are declared in
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 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
73 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
74 complex types, a typedef must be declared, e.g.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
75
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
76 typedef unsigned char *unsigned_char_ptr;
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 and then you can say
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 unsigned_char_ptr_dynarr *dyn = Dynarr_new (unsigned_char_ptr);
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
81
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
82 void *Dynarr_new2(dynarr_type, type)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
83 [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
84 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
85 it possible to create dynarrs over complex types without the need
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
86 to create typedefs, as described above. Use is as follows:
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
87
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
88 ucharptr_dynarr *dyn = Dynarr_new2 (ucharptr_dynarr *, unsigned char *);
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
89
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
90 Dynarr_free(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
91 Destroy a dynamic array and the memory allocated to it.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
92
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
93 ************* Dynarr access *************
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
94
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
95 type Dynarr_at(d, i)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
96 [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
97 between 0 and Dynarr_largest(d), inclusive. With error-checking
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
98 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
99 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
100 returned, not a pointer to it.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
101
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
102 type *Dynarr_atp(d, i)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
103 [MACRO] Return a pointer to the element at the specified index.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
104 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
105 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
106 (conceivably) removed from the array, because this may trigger a
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
107 realloc() performed on the underlying dynarr storage, which may
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
108 involve moving the entire underlying storage to a new location in
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
109 memory.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
110
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
111 type *Dynarr_begin(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
112 [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
113 Dynarr_atp() for warnings about when the pointer might become invalid.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
114
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
115 type *Dynarr_lastp(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
116 [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
117 Dynarr_atp() for warnings about when the pointer might become invalid.
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 type *Dynarr_past_lastp(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
120 [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
121 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
122 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
123 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
124 (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
125 start writing elements if Dynarr_length() < Dynarr_largest().
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 ************* Dynarr length/size retrieval and setting *************
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
128
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
129 int Dynarr_length(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
130 [MACRO] Return the number of elements currently in a dynamic array.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
131
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
132 int Dynarr_largest(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
133 [MACRO] Return the maximum value that Dynarr_length(d) would
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
134 ever have returned. This is used esp. in the redisplay code,
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
135 which reuses dynarrs for performance reasons.
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 int Dynarr_max(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
138 [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
139 dynarr before it needs to be resized.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
140
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
141 Note that Dynarr_length(d) <= Dynarr_largest(d) <= Dynarr_max(d).
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
142
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
143 Bytecount Dynarr_sizeof(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
144 [MACRO] Return the total size of the elements currently in dynarr
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
145 D. This
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
146
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
147 Dynarr_set_lengthr(d, len)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
148 [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
149 Dynarr_largest(d), inclusive. With error-checking enabled, an
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
150 assertion failure will result from trying to set the length
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
151 to less than zero or greater than Dynarr_largest(d). The
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
152 restriction to Dynarr_largest() is to ensure that
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
153
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
154 Dynarr_set_length(d, len)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
155 [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
156 necessary to make sure enough space is available. there are no
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
157 restrictions on LEN other than available memory and that it must
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
158 be at least 0. Note that
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_set_length_and_zero(d, len)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
161 [MACRO] Like Dynarr_set_length(d, len) but also, if increasing
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
162 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
163 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
164 the new last element.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
165
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
166 Dynarr_incrementr(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
167 [MACRO] Increments the length of D by 1. Equivalent to
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
168 Dynarr_set_lengthr(d, Dynarr_length(d) + 1).
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
169
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
170 Dynarr_increment(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
171 [MACRO] Increments the length of D by 1. Equivalent to
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
172 Dynarr_set_length(d, Dynarr_length(d) + 1).
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
173
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
174 Dynarr_reset(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
175 [MACRO] Reset the length of a dynamic array to 0.
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 Dynarr_resize(d, maxval)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
178 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
179 MAXVAL elements. Resizing is done using a geometric series
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
180 (repeatedly multiply the old maximum by a constant, normally 1.5,
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
181 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
182 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
183 an internal function.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
184
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
185
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
186
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
187 ************* Adding/deleting elements to/from a dynarr *************
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189 Dynarr_add(d, el)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 [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
191 to the element; the element itself is stored in the array, however.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192 No function call is performed unless the array needs to be resized.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194 Dynarr_add_many(d, base, len)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195 [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
196 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
197 just make space for the elements; don't actually add them.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
199 Dynarr_prepend_many(d, base, len)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
200 [MACRO] Prepend LEN elements to the beginning of the dynamic array.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201 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
202 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
203 add them.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
205 Dynarr_insert_many(d, base, len, pos)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206 Insert LEN elements to the dynamic array starting at position
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
207 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
208 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
209 add them.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
210
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
211 type Dynarr_pop(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
212 [MACRO] Pop the last element off the dynarr and return it.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
213
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
214 Dynarr_delete(d, i)
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 position I.
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
216
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
217 Dynarr_delete_many(d, pos, len)
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
218 Delete LEN elements from the dynamic array starting at position
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
219 POS.
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
220
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
221 Dynarr_zero_many(d, pos, len)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
222 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
223
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
224 Dynarr_delete_by_pointer(d, p)
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 665
diff changeset
225 [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
226 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
227 P should be obtained using Dynarr_atp().
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 locking *************
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
231 Dynarr_lock(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
232 Lock the dynarr against further locking or writing. With error-checking
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
233 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
234 already locked one will cause an assertion failure and abort.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
236 Dynarr_unlock(d)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
237 Unlock a locked dynarr, allowing writing into it.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
239 ************* Dynarr global variables *************
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241 Dynarr_min_size
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
242 Minimum allowable size for a dynamic array when it is resized.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245
4952
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
246 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
247 { XD_ASCII_STRING, 0 },
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
248 { XD_END }
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
249 };
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
250
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
251 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
252 sizeof (const Ascbyte *),
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
253 const_Ascbyte_ptr_description_1
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
254 };
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
255
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
256 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
257 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
258 { XD_END }
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
259 };
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
260
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
261 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
262 sizeof (const_Ascbyte_ptr_dynarr),
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
263 const_Ascbyte_ptr_dynarr_description_1
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
264 };
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
265
19a72041c5ed Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents: 4844
diff changeset
266
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
267 static Elemcount Dynarr_min_size = 8;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
268
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269 static void
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
270 Dynarr_realloc (Dynarr *dy, Elemcount new_size)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 if (DUMPEDP (dy->base))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 {
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
274 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
275 memcpy (new_base, dy->base,
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
276 (Dynarr_max (dy) < new_size ? Dynarr_max (dy) : new_size) *
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
277 Dynarr_elsize (dy));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
278 dy->base = new_base;
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 else
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
281 dy->base = xrealloc (dy->base, new_size * Dynarr_elsize (dy));
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
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 void *
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
285 Dynarr_newf (Bytecount elsize)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287 Dynarr *d = xnew_and_zero (Dynarr);
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
288 d->elsize_ = elsize;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 return d;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
292
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
293 #ifdef NEW_GC
5118
e0db3c197671 merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
294 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
295 0, 0,
e0db3c197671 merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
296 Dynarr);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
297
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
298 static void
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
299 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
300 {
5118
e0db3c197671 merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
301 void *new_base =
5126
Ben Wing <ben@xemacs.org>
parents: 5125 5038
diff changeset
302 XPNTR (alloc_sized_lrecord_array (Dynarr_elsize (dy), new_size,
Ben Wing <ben@xemacs.org>
parents: 5125 5038
diff changeset
303 dy->lisp_imp));
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
304 if (dy->base)
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
305 memcpy (new_base, dy->base,
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
306 (Dynarr_max (dy) < new_size ? Dynarr_max (dy) : new_size) *
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
307 Dynarr_elsize (dy));
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
308 dy->base = new_base;
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
309 }
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
310
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
311 void *
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
312 Dynarr_lisp_newf (Bytecount elsize,
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
313 const struct lrecord_implementation *dynarr_imp,
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
314 const struct lrecord_implementation *imp)
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
315 {
5120
d1247f3cc363 latest work on lisp-object workspace;
Ben Wing <ben@xemacs.org>
parents: 5118
diff changeset
316 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
317 dynarr_imp));
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
318 d->elsize_ = elsize;
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
319 d->lisp_imp = imp;
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
320
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
321 return d;
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
322 }
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
323 #endif /* not NEW_GC */
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
324
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325 void
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
326 Dynarr_resize (void *d, Elemcount size)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 {
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
328 Elemcount newsize;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 double multiplier;
1318
b531bf8658e9 [xemacs-hg @ 2003-02-21 06:56:46 by ben]
ben
parents: 793
diff changeset
330 Dynarr *dy = (Dynarr *) Dynarr_verify (d);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
332 if (Dynarr_max (dy) <= 8)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 multiplier = 2;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 multiplier = 1.5;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
337 for (newsize = Dynarr_max (dy); newsize < size;)
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
338 newsize = max (Dynarr_min_size, (Elemcount) (multiplier * newsize));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340 /* 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
341 if (newsize > Dynarr_max (dy))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342 {
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
343 #ifdef NEW_GC
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
344 if (dy->lisp_imp)
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
345 Dynarr_lisp_realloc (dy, newsize);
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
346 else
3210
72b7d685c194 [xemacs-hg @ 2006-01-20 17:59:48 by crestani]
crestani
parents: 3092
diff changeset
347 Dynarr_realloc (dy, newsize);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
348 #else /* not NEW_GC */
3210
72b7d685c194 [xemacs-hg @ 2006-01-20 17:59:48 by crestani]
crestani
parents: 3092
diff changeset
349 Dynarr_realloc (dy, newsize);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
350 #endif /* not NEW_GC */
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
351 dy->max_ = newsize;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
355 /* 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
356
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
357 void
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
358 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
359 {
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
360 Dynarr *dy = Dynarr_verify_mod (d);
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
361 Elemcount old_len = Dynarr_length (dy);
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
362
4844
91b3d00e717f Various cleanups for Dynarr code, from Unicode-internal ws
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
363 /* #### 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
364 between len and largest. */
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
365 dynarr_checking_assert (pos >= 0 && pos <= old_len);
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
366 dynarr_checking_assert (len >= 0);
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
367 Dynarr_increase_length (dy, old_len + len);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
368
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
369 if (pos != old_len)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370 {
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
371 memmove ((Rawbyte *) dy->base + (pos + len)*Dynarr_elsize (dy),
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
372 (Rawbyte *) dy->base + pos*Dynarr_elsize (dy),
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
373 (old_len - pos)*Dynarr_elsize (dy));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374 }
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
375 /* 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
376 don't write into it" */
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
377 if (base)
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
378 memcpy ((Rawbyte *) dy->base + pos*Dynarr_elsize (dy), base,
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
379 len*Dynarr_elsize (dy));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
381
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 void
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
383 Dynarr_delete_many (void *d, Elemcount pos, Elemcount len)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
384 {
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
385 Dynarr *dy = Dynarr_verify_mod (d);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
387 dynarr_checking_assert (pos >= 0 && len >= 0 &&
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
388 pos + len <= Dynarr_length (dy));
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
389
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
390 memmove ((Rawbyte *) dy->base + pos*Dynarr_elsize (dy),
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
391 (Rawbyte *) dy->base + (pos + len)*Dynarr_elsize (dy),
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
392 (Dynarr_length (dy) - pos - len)*Dynarr_elsize (dy));
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
393
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
394 Dynarr_set_length_1 (dy, Dynarr_length (dy) - len);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
395 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
396
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398 Dynarr_free (void *d)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
400 Dynarr *dy = (Dynarr *) d;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
401
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
402 #ifdef NEW_GC
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
403 if (dy->base && !DUMPEDP (dy->base))
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
404 {
4117
229bd619740a [xemacs-hg @ 2007-08-15 11:06:02 by crestani]
crestani
parents: 3293
diff changeset
405 if (!dy->lisp_imp)
5169
6c6d78781d59 cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents: 5168
diff changeset
406 {
6c6d78781d59 cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents: 5168
diff changeset
407 xfree (dy->base);
6c6d78781d59 cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents: 5168
diff changeset
408 dy->base = 0;
6c6d78781d59 cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents: 5168
diff changeset
409 }
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
410 }
5169
6c6d78781d59 cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents: 5168
diff changeset
411 if (!DUMPEDP (dy))
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
412 {
4117
229bd619740a [xemacs-hg @ 2007-08-15 11:06:02 by crestani]
crestani
parents: 3293
diff changeset
413 if (!dy->lisp_imp)
4976
16112448d484 Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents: 4967
diff changeset
414 xfree (dy);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
415 }
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
416 #else /* not NEW_GC */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
417 if (dy->base && !DUMPEDP (dy->base))
5169
6c6d78781d59 cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents: 5168
diff changeset
418 {
6c6d78781d59 cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents: 5168
diff changeset
419 xfree (dy->base);
6c6d78781d59 cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents: 5168
diff changeset
420 dy->base = 0;
6c6d78781d59 cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents: 5168
diff changeset
421 }
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422 if(!DUMPEDP (dy))
4976
16112448d484 Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents: 4967
diff changeset
423 xfree (dy);
3092
141c2920ea48 [xemacs-hg @ 2005-11-25 01:41:31 by crestani]
crestani
parents: 2500
diff changeset
424 #endif /* not NEW_GC */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427 #ifdef MEMORY_USAGE_STATS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
429 /* 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
430 amount of bytes actually being used for the dynarr, including all
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
431 overhead. The extra amount of space in the dynarr that is
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432 allocated beyond what was requested is returned in DYNARR_OVERHEAD
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433 in STATS. The extra amount of space that malloc() allocates beyond
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 what was requested of it is returned in MALLOC_OVERHEAD in STATS.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 See the comment above the definition of this structure. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
436
665
fdefd0186b75 [xemacs-hg @ 2001-09-20 06:28:42 by ben]
ben
parents: 647
diff changeset
437 Bytecount
5157
1fae11d56ad2 redo memory-usage mechanism, add way of dynamically initializing Lisp objects
Ben Wing <ben@xemacs.org>
parents: 5126
diff changeset
438 Dynarr_memory_usage (void *d, struct usage_stats *stats)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 {
665
fdefd0186b75 [xemacs-hg @ 2001-09-20 06:28:42 by ben]
ben
parents: 647
diff changeset
440 Bytecount total = 0;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441 Dynarr *dy = (Dynarr *) d;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 /* 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
444 memory that malloc() will claim as "requested" was actually
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445 requested. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 if (dy->base)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448 {
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
449 Bytecount malloc_used =
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
450 malloced_storage_size (dy->base, Dynarr_elsize (dy) * Dynarr_max (dy),
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
451 0);
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
452 /* #### 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
453 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
454 Bytecount was_requested = Dynarr_elsize (dy) * Dynarr_largest (dy);
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
455 Bytecount dynarr_overhead =
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
456 Dynarr_elsize (dy) * (Dynarr_max (dy) - Dynarr_largest (dy));
428
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 total += malloc_used;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459 stats->was_requested += was_requested;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460 stats->dynarr_overhead += dynarr_overhead;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461 /* And the remainder must be malloc overhead. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462 stats->malloc_overhead +=
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
463 malloc_used - was_requested - dynarr_overhead;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
464 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466 total += malloced_storage_size (d, sizeof (*dy), stats);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
467
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
468 return total;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
469 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471 #endif /* MEMORY_USAGE_STATS */
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
472
5168
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
473
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
474 /*****************************************************************************/
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
475 /* stack-like allocation */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
476 /*****************************************************************************/
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
477
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
478 /* 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
479 nearly always occurs in LIFO (stack) order.
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
480
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
481 #### 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
482
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
483 typedef struct
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 Dynarr_declare (char_dynarr *);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
486 } char_dynarr_dynarr;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
487
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
488 char_dynarr_dynarr *stack_like_free_list;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
489 char_dynarr_dynarr *stack_like_in_use_list;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
490
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
491 void *
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
492 stack_like_malloc (Bytecount size)
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 char_dynarr *this_one;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
495 if (!stack_like_free_list)
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
496 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
497 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
498 char_dynarr *);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
499 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
500 char_dynarr *);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
501 }
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
502
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
503 if (Dynarr_length (stack_like_free_list) > 0)
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
504 this_one = Dynarr_pop (stack_like_free_list);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
505 else
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
506 this_one = Dynarr_new (char);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
507 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
508 Dynarr_reset (this_one);
91b3d00e717f Various cleanups for Dynarr code, from Unicode-internal ws
Ben Wing <ben@xemacs.org>
parents: 4117
diff changeset
509 Dynarr_add_many (this_one, 0, size);
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
510 return Dynarr_begin (this_one);
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
511 }
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
512
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
513 void
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
514 stack_like_free (void *val)
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
515 {
5038
9410323e4b0d major dynarr fixes
Ben Wing <ben@xemacs.org>
parents: 5023
diff changeset
516 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
517 assert (len > 0);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
518 /* 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
519 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
520 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
521 calls. */
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
522 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
523 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
524 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
525 Dynarr_add (stack_like_free_list, this_one);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
526 }
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
527 else
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
528 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
529 /* Find the item and delete it. */
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
530 int i;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
531 assert (len >= 2);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
532 for (i = len - 2; i >= 0; i--)
4967
0d4c9d0f6a8d rewrite dynarr code
Ben Wing <ben@xemacs.org>
parents: 4952
diff changeset
533 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
534 val)
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
535 {
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
536 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
537 Dynarr_add (stack_like_free_list, this_one);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
538 Dynarr_delete (stack_like_in_use_list, i);
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
539 return;
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
540 }
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
541
2500
3d8143fc88e1 [xemacs-hg @ 2005-01-24 23:33:30 by ben]
ben
parents: 2367
diff changeset
542 ABORT ();
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
543 }
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 1726
diff changeset
544 }
5168
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
545
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
546
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
547 /*****************************************************************************/
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
548 /* Generalized gap array */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
549 /*****************************************************************************/
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
550
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
551 /* A "gap array" is an array that has a "gap" somewhere in the middle of it,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
552 so that insertions and deletions near the gap -- or in general, highly
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
553 localized insertions and deletions -- are very fast. Inserting or
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
554 deleting works by first moving the gap to the insertion or deletion
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
555 position and then shortening or lengthening the gap as necessary. The
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
556 idea comes from the gap used in storing text in a buffer.
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
557
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
558 The gap array interface differs in a number of ways from dynarrs (####
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
559 and should be changed so that it works the same as dynarrs):
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
560
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
561 (1) There aren't separate type-specific gap array types. As a result,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
562 operations like gap_array_at() require that the type be specified as
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
563 one of the arguments. It is often more convenient to use a macro
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
564 wrapper around this operation.
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
565
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
566 (2) The gap array type is itself a stretchy array rather than using a
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
567 separate block of memory to store the array. This means that certain
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
568 operations (especially insertions) may relocate the the gap array,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
569 and as a result return a pointer to the (possibly) moved gap array,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
570 which must be stored back into the location where the gap array
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
571 pointer resides. This also means that the caller must worry about
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
572 cloning the gap array in the case where it has been dumped, or you
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
573 will get an ABORT() inside of xrealloc().
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
574
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
575 (3) Fewer operations are available than for dynarrs, and may have
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
576 different names and/or different calling conventions.
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
577
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
578 (4) The mechanism for creating "Lisp-object gap arrays" isn't completely
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
579 developed. Currently it's only possible to create a gap-array Lisp
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
580 object that wraps Lisp_Object pointers (not Lisp object structures
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
581 directly), and only under NEW_GC.
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
582
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
583 (5) Gap arrays have a concept of a "gap array marker" that properly
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
584 tracks insertions and deletions; no such thing exists in dynarrs.
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
585 It exists in gap arrays because it's necessary for their use in
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
586 implementing extent lists.
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
587 */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
588
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
589 extern const struct sized_memory_description gap_array_marker_description;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
590
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
591 static const struct memory_description gap_array_marker_description_1[] = {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
592 #ifdef NEW_GC
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
593 { XD_LISP_OBJECT, offsetof (Gap_Array_Marker, next) },
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
594 #else /* not NEW_GC */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
595 { XD_BLOCK_PTR, offsetof (Gap_Array_Marker, next), 1,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
596 { &gap_array_marker_description } },
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
597 #endif /* not NEW_GC */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
598 { XD_END }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
599 };
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
600
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
601 #ifdef NEW_GC
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
602 DEFINE_NODUMP_INTERNAL_LISP_OBJECT ("gap-array-marker", gap_array_marker,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
603 0, gap_array_marker_description_1,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
604 struct gap_array_marker);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
605 #else /* not NEW_GC */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
606 const struct sized_memory_description gap_array_marker_description = {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
607 sizeof (Gap_Array_Marker),
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
608 gap_array_marker_description_1
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
609 };
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
610 #endif /* not NEW_GC */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
611
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
612 static const struct memory_description lispobj_gap_array_description_1[] = {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
613 XD_GAP_ARRAY_DESC (&lisp_object_description),
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
614 { XD_END }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
615 };
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
616
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
617 #ifdef NEW_GC
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
618
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
619 static Bytecount
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
620 size_gap_array (Lisp_Object obj)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
621 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
622 Gap_Array *ga = XGAP_ARRAY (obj);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
623 return gap_array_byte_size (ga);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
624 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
625
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
626 DEFINE_DUMPABLE_SIZABLE_INTERNAL_LISP_OBJECT ("gap-array", gap_array,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
627 0,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
628 lispobj_gap_array_description_1,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
629 size_gap_array,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
630 struct gap_array);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
631 #else /* not NEW_GC */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
632 const struct sized_memory_description lispobj_gap_array_description = {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
633 0, lispobj_gap_array_description_1
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
634 };
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
635 #endif /* (not) NEW_GC */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
636
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
637 #ifndef NEW_GC
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
638 static Gap_Array_Marker *gap_array_marker_freelist;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
639 #endif /* not NEW_GC */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
640
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
641 /* This generalizes the "array with a gap" model used to store buffer
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
642 characters. This is based on the stuff in insdel.c and should
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
643 probably be merged with it. This is not extent-specific and should
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
644 perhaps be moved into a separate file. */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
645
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
646 /* ------------------------------- */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
647 /* internal functions */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
648 /* ------------------------------- */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
649
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
650 /* Adjust the gap array markers in the range (FROM, TO]. Parallel to
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
651 adjust_markers() in insdel.c. */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
652
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
653 static void
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
654 gap_array_adjust_markers (Gap_Array *ga, Memxpos from,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
655 Memxpos to, Elemcount amount)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
656 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
657 Gap_Array_Marker *m;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
658
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
659 for (m = ga->markers; m; m = m->next)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
660 m->pos = do_marker_adjustment (m->pos, from, to, amount);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
661 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
662
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
663 static void
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
664 gap_array_recompute_derived_values (Gap_Array *ga)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
665 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
666 ga->offset_past_gap = ga->elsize * (ga->gap + ga->gapsize);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
667 ga->els_past_gap = ga->numels - ga->gap;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
668 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
669
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
670 /* Move the gap to array position POS. Parallel to move_gap() in
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
671 insdel.c but somewhat simplified. */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
672
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
673 static void
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
674 gap_array_move_gap (Gap_Array *ga, Elemcount pos)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
675 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
676 Elemcount gap = ga->gap;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
677 Elemcount gapsize = ga->gapsize;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
678
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
679 if (pos < gap)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
680 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
681 memmove (GAP_ARRAY_MEMEL_ADDR (ga, pos + gapsize),
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
682 GAP_ARRAY_MEMEL_ADDR (ga, pos),
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
683 (gap - pos)*ga->elsize);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
684 gap_array_adjust_markers (ga, (Memxpos) pos, (Memxpos) gap,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
685 gapsize);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
686 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
687 else if (pos > gap)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
688 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
689 memmove (GAP_ARRAY_MEMEL_ADDR (ga, gap),
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
690 GAP_ARRAY_MEMEL_ADDR (ga, gap + gapsize),
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
691 (pos - gap)*ga->elsize);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
692 gap_array_adjust_markers (ga, (Memxpos) (gap + gapsize),
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
693 (Memxpos) (pos + gapsize), - gapsize);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
694 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
695 ga->gap = pos;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
696
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
697 gap_array_recompute_derived_values (ga);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
698 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
699
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
700 /* Make the gap INCREMENT characters longer. Parallel to make_gap() in
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
701 insdel.c. The gap array may be moved, so assign the return value back
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
702 to the array pointer. */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
703
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
704 static Gap_Array *
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
705 gap_array_make_gap (Gap_Array *ga, Elemcount increment)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
706 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
707 Elemcount real_gap_loc;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
708 Elemcount old_gap_size;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
709
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
710 /* If we have to get more space, get enough to last a while. We use
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
711 a geometric progression that saves on realloc space. */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
712 increment += 100 + ga->numels / 8;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
713
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
714 #ifdef NEW_GC
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
715 if (ga->is_lisp)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
716 ga = (Gap_Array *) mc_realloc (ga,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
717 offsetof (Gap_Array, array) +
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
718 (ga->numels + ga->gapsize + increment) *
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
719 ga->elsize);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
720 else
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
721 #endif /* not NEW_GC */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
722 ga = (Gap_Array *) xrealloc (ga,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
723 offsetof (Gap_Array, array) +
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
724 (ga->numels + ga->gapsize + increment) *
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
725 ga->elsize);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
726 if (ga == 0)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
727 memory_full ();
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
728
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
729 real_gap_loc = ga->gap;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
730 old_gap_size = ga->gapsize;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
731
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
732 /* Call the newly allocated space a gap at the end of the whole space. */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
733 ga->gap = ga->numels + ga->gapsize;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
734 ga->gapsize = increment;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
735
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
736 /* Move the new gap down to be consecutive with the end of the old one.
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
737 This adjusts the markers properly too. */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
738 gap_array_move_gap (ga, real_gap_loc + old_gap_size);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
739
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
740 /* Now combine the two into one large gap. */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
741 ga->gapsize += old_gap_size;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
742 ga->gap = real_gap_loc;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
743
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
744 gap_array_recompute_derived_values (ga);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
745
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
746 return ga;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
747 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
748
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
749 /* ------------------------------- */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
750 /* external functions */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
751 /* ------------------------------- */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
752
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
753 Bytecount
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
754 gap_array_byte_size (Gap_Array *ga)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
755 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
756 return offsetof (Gap_Array, array) + (ga->numels + ga->gapsize) * ga->elsize;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
757 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
758
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
759 /* Insert NUMELS elements (pointed to by ELPTR) into the specified
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
760 gap array at POS. The gap array may be moved, so assign the
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
761 return value back to the array pointer. */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
762
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
763 Gap_Array *
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
764 gap_array_insert_els (Gap_Array *ga, Elemcount pos, void *elptr,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
765 Elemcount numels)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
766 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
767 assert (pos >= 0 && pos <= ga->numels);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
768 if (ga->gapsize < numels)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
769 ga = gap_array_make_gap (ga, numels - ga->gapsize);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
770 if (pos != ga->gap)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
771 gap_array_move_gap (ga, pos);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
772
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
773 memcpy (GAP_ARRAY_MEMEL_ADDR (ga, ga->gap), (char *) elptr,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
774 numels*ga->elsize);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
775 ga->gapsize -= numels;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
776 ga->gap += numels;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
777 ga->numels += numels;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
778 gap_array_recompute_derived_values (ga);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
779 /* This is the equivalent of insert-before-markers.
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
780
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
781 #### Should only happen if marker is "moves forward at insert" type.
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
782 */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
783
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
784 gap_array_adjust_markers (ga, pos - 1, pos, numels);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
785 return ga;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
786 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
787
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
788 /* Delete NUMELS elements from the specified gap array, starting at FROM. */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
789
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
790 void
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
791 gap_array_delete_els (Gap_Array *ga, Elemcount from, Elemcount numdel)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
792 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
793 Elemcount to = from + numdel;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
794 Elemcount gapsize = ga->gapsize;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
795
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
796 assert (from >= 0);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
797 assert (numdel >= 0);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
798 assert (to <= ga->numels);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
799
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
800 /* Make sure the gap is somewhere in or next to what we are deleting. */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
801 if (to < ga->gap)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
802 gap_array_move_gap (ga, to);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
803 if (from > ga->gap)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
804 gap_array_move_gap (ga, from);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
805
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
806 /* Relocate all markers pointing into the new, larger gap
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
807 to point at the end of the text before the gap. */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
808 gap_array_adjust_markers (ga, to + gapsize, to + gapsize,
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
809 - numdel - gapsize);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
810
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
811 ga->gapsize += numdel;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
812 ga->numels -= numdel;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
813 ga->gap = from;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
814 gap_array_recompute_derived_values (ga);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
815 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
816
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
817 Gap_Array_Marker *
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
818 gap_array_make_marker (Gap_Array *ga, Elemcount pos)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
819 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
820 Gap_Array_Marker *m;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
821
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
822 assert (pos >= 0 && pos <= ga->numels);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
823 #ifdef NEW_GC
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
824 m = XGAP_ARRAY_MARKER (ALLOC_NORMAL_LISP_OBJECT (gap_array_marker));
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
825 #else /* not NEW_GC */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
826 if (gap_array_marker_freelist)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
827 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
828 m = gap_array_marker_freelist;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
829 gap_array_marker_freelist = gap_array_marker_freelist->next;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
830 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
831 else
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
832 m = xnew (Gap_Array_Marker);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
833 #endif /* not NEW_GC */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
834
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
835 m->pos = GAP_ARRAY_ARRAY_TO_MEMORY_POS (ga, pos);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
836 m->next = ga->markers;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
837 ga->markers = m;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
838 return m;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
839 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
840
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
841 void
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
842 gap_array_delete_marker (Gap_Array *ga, Gap_Array_Marker *m)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
843 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
844 Gap_Array_Marker *p, *prev;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
845
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
846 for (prev = 0, p = ga->markers; p && p != m; prev = p, p = p->next)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
847 ;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
848 assert (p);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
849 if (prev)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
850 prev->next = p->next;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
851 else
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
852 ga->markers = p->next;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
853 #ifndef NEW_GC
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
854 m->next = gap_array_marker_freelist;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
855 m->pos = 0xDEADBEEF; /* -559038737 base 10 */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
856 gap_array_marker_freelist = m;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
857 #endif /* not NEW_GC */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
858 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
859
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
860 #ifndef NEW_GC
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
861 void
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
862 gap_array_delete_all_markers (Gap_Array *ga)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
863 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
864 Gap_Array_Marker *p, *next;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
865
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
866 for (p = ga->markers; p; p = next)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
867 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
868 next = p->next;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
869 p->next = gap_array_marker_freelist;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
870 p->pos = 0xDEADBEEF; /* -559038737 as an int */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
871 gap_array_marker_freelist = p;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
872 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
873 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
874 #endif /* not NEW_GC */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
875
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
876 void
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
877 gap_array_move_marker (Gap_Array *ga, Gap_Array_Marker *m, Elemcount pos)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
878 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
879 assert (pos >= 0 && pos <= ga->numels);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
880 m->pos = GAP_ARRAY_ARRAY_TO_MEMORY_POS (ga, pos);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
881 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
882
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
883 Gap_Array *
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
884 make_gap_array (Elemcount elsize, int USED_IF_NEW_GC (do_lisp))
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
885 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
886 Gap_Array *ga;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
887 #ifdef NEW_GC
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
888 /* #### I don't quite understand why it's necessary to make all these
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
889 internal objects into Lisp objects under NEW_GC. It's a pain in the
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
890 ass to code around this. I'm proceeding on the assumption that it's
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
891 not really necessary to do it after all, and so we only make a Lisp-
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
892 object gap array when the object being held is a Lisp_Object, i.e. a
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
893 pointer to a Lisp object. In the case where instead we hold a `struct
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
894 range_table_entry', just blow it off. Otherwise we either need to do
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
895 a bunch of painful and/or boring rewriting. --ben */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
896 if (do_lisp)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
897 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
898 ga = XGAP_ARRAY (ALLOC_SIZED_LISP_OBJECT (sizeof (Gap_Array),
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
899 gap_array));
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
900 ga->is_lisp = 1;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
901 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
902 else
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
903 #endif /* not NEW_GC */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
904 ga = xnew_and_zero (Gap_Array);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
905 ga->elsize = elsize;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
906 return ga;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
907 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
908
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
909 Gap_Array *
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
910 gap_array_clone (Gap_Array *ga)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
911 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
912 Bytecount size = gap_array_byte_size (ga);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
913 Gap_Array *ga2;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
914 Gap_Array_Marker *m;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
915
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
916 #ifdef NEW_GC
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
917 if (ga->is_lisp)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
918 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
919 ga2 = XGAP_ARRAY (ALLOC_SIZED_LISP_OBJECT (size, gap_array));
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
920 copy_lisp_object (wrap_gap_array (ga2), wrap_gap_array (ga));
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
921 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
922 else
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
923 #endif
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
924 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
925 ga2 = (Gap_Array *) xmalloc (size);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
926 memcpy (ga2, ga, size);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
927 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
928 ga2->markers = NULL;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
929 for (m = ga->markers; m; m = m->next)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
930 gap_array_make_marker (ga2, m->pos);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
931 return ga2;
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
932 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
933
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
934 #ifndef NEW_GC
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
935 void
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
936 free_gap_array (Gap_Array *ga)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
937 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
938 gap_array_delete_all_markers (ga);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
939 xfree (ga);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
940 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
941 #endif /* not NEW_GC */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
942
5170
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
943 #ifdef MEMORY_USAGE_STATS
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
944
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
945 /* Return memory usage for gap array GA. The returned value is the total
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
946 amount of bytes actually being used for the gap array, including all
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
947 overhead. The extra amount of space in the gap array that is used
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
948 for the gap is counted in GAP_OVERHEAD, not in WAS_REQUESTED.
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
949 If NEW_GC, space for gap-array markers is returned through MARKER_ANCILLARY;
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
950 otherwise it's added into the gap array usage. */
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
951
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
952 Bytecount
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
953 gap_array_memory_usage (Gap_Array *ga, struct usage_stats *stats,
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
954 Bytecount *marker_ancillary)
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
955 {
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
956 Bytecount total = 0;
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
957
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
958 /* We have to be a bit tricky here because not all of the
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
959 memory that malloc() will claim as "requested" was actually
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
960 requested -- some of it makes up the gap. */
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
961
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
962 Bytecount size = gap_array_byte_size (ga);
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
963 Bytecount gap_size = ga->gapsize * ga->elsize;
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
964 Bytecount malloc_used = malloced_storage_size (ga, size, 0);
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
965 total += malloc_used;
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
966 stats->was_requested += size - gap_size;
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
967 stats->gap_overhead += gap_size;
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
968 stats->malloc_overhead += malloc_used - size;
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
969
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
970 #ifdef NEW_GC
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
971 {
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
972 Bytecount marker_usage = 0;
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
973 Gap_Array_Marker *p;
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
974
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
975 for (p = ga->markers; p; p = p->next)
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
976 marker_usage += lisp_object_memory_usage (wrap_gap_array_marker (p));
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
977 if (marker_ancillary)
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
978 *marker_ancillary = marker_usage;
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
979 }
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
980 #else
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
981 {
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
982 Gap_Array_Marker *p;
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
983
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
984 for (p = ga->markers; p; p = p->next)
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
985 total += malloced_storage_size (p, sizeof (p), stats);
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
986 if (marker_ancillary)
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
987 *marker_ancillary = 0;
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
988 }
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
989 #endif /* (not) NEW_GC */
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
990
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
991 return total;
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
992 }
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
993
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
994 #endif /* MEMORY_USAGE_STATS */
5ddbab03b0e6 various fixes to memory-usage stats
Ben Wing <ben@xemacs.org>
parents: 5169
diff changeset
995
5168
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
996
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
997 /*****************************************************************************/
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
998 /* Initialization */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
999 /*****************************************************************************/
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
1000
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
1001 void
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
1002 syms_of_array (void)
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
1003 {
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
1004 #ifdef NEW_GC
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
1005 INIT_LISP_OBJECT (gap_array_marker);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
1006 INIT_LISP_OBJECT (gap_array);
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
1007 #endif /* NEW_GC */
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
1008 }
cf900a2f1fa3 extract gap array from extents.c, use in range tables
Ben Wing <ben@xemacs.org>
parents: 5157
diff changeset
1009