Mercurial > ~dholland > hg > ag > index.cgi
comparison anagram/agcore/arrays.cpp @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 /* | |
2 * AnaGram, A System for Syntax Directed Programming | |
3 * Copyright 1993-1999 Parsifal Software. All Rights Reserved. | |
4 * See the file COPYING for license and usage terms. | |
5 * | |
6 * arrays.cpp - old array control module | |
7 */ | |
8 | |
9 #include <string.h> | |
10 #include "port.h" | |
11 | |
12 #include "arrays.h" | |
13 #include "assert.h" | |
14 #include "myalloc.h" | |
15 #include "stacks.h" | |
16 #include "tsd.h" | |
17 | |
18 //#define INCLUDE_LOGGING | |
19 #include "log.h" | |
20 | |
21 | |
22 #define mkstrg(n,size) \ | |
23 (array_control_block *) \ | |
24 (ALLOCATE(n*size + sizeof(array_control_block), char)) | |
25 | |
26 #define remkstrg(p,n) \ | |
27 (array_control_block *) \ | |
28 (reallocate(p,n + sizeof(array_control_block), char)) | |
29 | |
30 typedef struct { | |
31 unsigned elements_allocated; | |
32 unsigned element_size; | |
33 } array_control_block; | |
34 | |
35 | |
36 void *init_array(unsigned n, unsigned size) { | |
37 LOGSECTION("init_array"); | |
38 LOGV(n) LCV(size); | |
39 | |
40 array_control_block *p; | |
41 p = mkstrg(n,size); | |
42 p->elements_allocated = n; | |
43 p->element_size = size; | |
44 memset(++p,0,n*size); | |
45 return p; | |
46 } | |
47 | |
48 void *reset_array_size(void *ptr, unsigned n, unsigned size) { | |
49 LOGSECTION("reset_array_size"); | |
50 LOGV((int) ptr) LCV(n) LCV(size); | |
51 array_control_block *p; | |
52 if (ptr) { | |
53 delete_array(ptr); | |
54 } | |
55 p = mkstrg(n, size); | |
56 p->elements_allocated = n; | |
57 p->element_size = size; | |
58 memset(++p, 0, n*size); | |
59 LOGV((int) p) LCV(n) LCV(size); | |
60 return p; | |
61 } | |
62 | |
63 void *reset_array(void *a) { | |
64 array_control_block *p; | |
65 unsigned n, size; | |
66 unsigned k; | |
67 | |
68 p = (array_control_block *) a; | |
69 p--; | |
70 ok_ptr(p); | |
71 n = p->elements_allocated; | |
72 size = p->element_size; | |
73 k = n*size; | |
74 size_ok(p, k+sizeof(array_control_block), __FILE__, __LINE__); | |
75 memset(++p,0,k); | |
76 return p; | |
77 } | |
78 | |
79 void *delete_array(void *a) { | |
80 array_control_block *p; | |
81 | |
82 p = (array_control_block *) a; | |
83 p--; | |
84 DEALLOCATE(p); | |
85 return NULL; | |
86 } | |
87 | |
88 static int array_ok(void *ptr) { | |
89 array_control_block *p; | |
90 | |
91 p = (array_control_block *) ptr; | |
92 p--; | |
93 return ptr_ok(p); | |
94 } | |
95 | |
96 #if 0 /* unused */ | |
97 void *slide_array(void *ptr) { | |
98 array_control_block *p = (array_control_block *) ptr, *q; | |
99 unsigned size; | |
100 | |
101 p--; | |
102 ok_ptr(p); | |
103 size = p->elements_allocated * p->element_size + sizeof(array_control_block); | |
104 q = (array_control_block *) reallocate(p,size, char); | |
105 q++; | |
106 return (void *) q; | |
107 } | |
108 #endif | |
109 | |
110 int ok_index(void *ptr, unsigned x) { | |
111 LOGSECTION("ok_index"); | |
112 array_control_block *p = (array_control_block *) ptr; | |
113 p--; | |
114 ok_ptr(p); | |
115 LOGV(x) LCV(p->elements_allocated); | |
116 return x < p->elements_allocated; | |
117 } | |
118 | |
119 void *check_array_size(void *ptr, unsigned n1, unsigned n) { | |
120 LOGSECTION("check_array_size"); | |
121 LOGV((int) ptr) LCV(n1) LCV(n); | |
122 array_control_block *p = (array_control_block *) ptr; | |
123 unsigned kp,kn; | |
124 // unsigned long lim; | |
125 // unsigned min; | |
126 | |
127 p--; | |
128 ok_ptr(p); | |
129 LOGV(p->elements_allocated); | |
130 if (n1 + 1 < (kp = p->elements_allocated)) { | |
131 return ptr; | |
132 } | |
133 n1 += 2; | |
134 if (n < n1) { | |
135 n = n1; | |
136 } | |
137 kn = p->element_size; | |
138 //min = n1*kn; | |
139 kp *= kn; | |
140 | |
141 //lim = (MAX_BYTES - sizeof(array_control_block))/kn; | |
142 //if (lim < n1) { | |
143 // no_mem(kp); | |
144 //} | |
145 | |
146 LOGV(n) LCV(n1) LCV(p->element_size); // LCV(lim); | |
147 | |
148 //if (n > lim) { | |
149 // n = (unsigned) lim; | |
150 //} | |
151 | |
152 assert(n >= n1); | |
153 kn *= n; | |
154 /* | |
155 p = (array_control_block *) vrealloc(p, | |
156 min + sizeof(array_control_block), | |
157 kn + sizeof(array_control_block), &n); | |
158 */ | |
159 p = remkstrg(p, kn); | |
160 LOGV(kn) LCV(p->element_size); | |
161 //n -= sizeof(array_control_block); | |
162 p->elements_allocated = kn/p->element_size; | |
163 LOGV(p->elements_allocated); | |
164 assert(p->elements_allocated >= n1); | |
165 p++; | |
166 if (kn > kp) { | |
167 memset((char *)p + kp, 0, kn - kp); | |
168 } | |
169 LOGV(kn) LCV(kp); | |
170 LOGV((int) ((char *)p + kp)) LCV(kn-kp); | |
171 return p; | |
172 } | |
173 | |
174 void *set_array_size(void *ptr, unsigned n) { | |
175 array_control_block *p = (array_control_block *) ptr; | |
176 unsigned kp,kn; | |
177 | |
178 p--; | |
179 ok_ptr(p); | |
180 kp = p->elements_allocated; | |
181 if (kp == n) { | |
182 return ptr; | |
183 } | |
184 kn = p->element_size; | |
185 kp *= kn; | |
186 kn *= n; | |
187 p = remkstrg(p, kn); | |
188 p->elements_allocated = n; | |
189 p++; | |
190 if (kn > kp) { | |
191 memset((char *)p + kp, 0, kn - kp); | |
192 } | |
193 return p; | |
194 } | |
195 | |
196 static unsigned *init_list(int n) { | |
197 LOGSECTION("init_list"); | |
198 LOGV(n); | |
199 unsigned *list = (unsigned *) init_array(n+1, sizeof(*list)); | |
200 list[0] = 1; | |
201 return list; | |
202 } | |
203 | |
204 unsigned *reset_list(unsigned *list, unsigned n) { | |
205 LOGSECTION("reset_list"); | |
206 LOGV(list ? list[0] : 0) LCV(n); | |
207 unsigned *new_array = init_list(n); | |
208 new_array[0] = 1; | |
209 if (list) { | |
210 delete_array(list); | |
211 } | |
212 return new_array; | |
213 } | |
214 | |
215 void *check_list_size(unsigned *list, unsigned n) { | |
216 LOGSECTION("check_list_size"); | |
217 LOGV(list[0]) LCV(n); | |
218 n += list[0]; | |
219 return check_array_size(list,n,n+n/2); | |
220 } | |
221 | |
222 static unsigned add_data_list(unsigned *list, unsigned *data, unsigned n) { | |
223 unsigned k = list[0]; | |
224 memmove(list+k, data, n*sizeof(*list)); | |
225 list[0] += n; | |
226 array_ok(list); | |
227 return k; | |
228 } | |
229 | |
230 int store_list_data(unsigned *list) { | |
231 LOGSECTION("store_list_data"); | |
232 unsigned n = tis(); | |
233 LOGV(list[0]) LCV(n); | |
234 if (n == 0) { | |
235 return 0; | |
236 } | |
237 return add_data_list(list, (unsigned *) list_base, n); | |
238 } | |
239 | |
240 int store_tuple_data(tsd *t, unsigned *list) { | |
241 unsigned n = t->nt * t->tw; | |
242 if (n == 0) { | |
243 return 0; | |
244 } | |
245 return add_data_list(list, (unsigned *)t->sb, n); | |
246 } | |
247 /* | |
248 void *shrink_fit(unsigned *list) { | |
249 return set_array_size(list, list[0]); | |
250 } | |
251 */ | |
252 | |
253 void *check_local_array(void *p) { | |
254 assert(p != NULL); | |
255 return p; | |
256 } | |
257 | |
258 /* End ARRAYS.C */ |