-
Notifications
You must be signed in to change notification settings - Fork 138
/
resizablebooleanarray.pmc
499 lines (342 loc) · 13.6 KB
/
resizablebooleanarray.pmc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
Copyright (C) 2001-2009, Parrot Foundation.
$Id$
=head1 NAME
src/pmc/resizablebooleanarray.pmc - resizable array for booleans only
=head1 DESCRIPTION
The C<ResizableBooleanArray PMC> implements an array of resizable size, which
stores booleans. It uses the C<Boolean PMC> for all conversions. The
C<ResizableBooleanArray PMC> extends the C<FixedBooleanArray PMC>.
=head2 Functions
=over 4
=cut
*/
#define BITS_PER_CHAR 8
/* MIN_ALLOC is 8 * BITS_PER_CHAR */
#define MIN_ALLOC 64
/* Round a given size in bits to the nearest allocation unit, then convert it
* to bytes. */
#define ROUND_BYTES(size) (((size) / MIN_ALLOC + 1) * MIN_ALLOC / BITS_PER_CHAR)
/* Convert a size in bits to a size in bytes */
#define BITS_TO_BYTES(size) ((size) / BITS_PER_CHAR)
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */
pmclass ResizableBooleanArray extends FixedBooleanArray auto_attrs provides array {
/* RBA uses the same attributes as FBA, but in RBA they're used as follows:
size: position of the last element (a.k.a tail_pos)
resize_threshold: position of the first element (a.k.a. head_pos) */
/*
=back
=head2 Methods
=over 4
=item C<INTVAL get_integer_keyed_int(INTVAL key)>
Returns the integer value of the element at index C<key>.
=cut
*/
VTABLE INTVAL get_integer_keyed_int(INTVAL key) {
UINTVAL offsetkey, tail_pos, head_pos;
/* Try to make negative index into a real index */
if (key < 0) {
key = SELF.elements() + key;
/* If it's still negative, we have a problem */
if (key < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ResizableBooleanArray: index out of bounds!");
}
/* Check if key is greater than allocated size */
GET_ATTR_size(INTERP, SELF, tail_pos);
GET_ATTR_resize_threshold(INTERP, SELF, head_pos);
offsetkey = key + head_pos;
if (offsetkey >= tail_pos)
return 0;
return SUPER(offsetkey);
}
/*
=item C<void set_integer_keyed_int(INTVAL key, INTVAL value)>
Sets the integer value of the element at index C<key> to C<value>.
=cut
*/
VTABLE void set_integer_keyed_int(INTVAL key, INTVAL value) {
UINTVAL offsetkey, tail_pos, head_pos;
/* Try to make negative index into a real index */
if (key < 0) {
key = SELF.elements() + key;
/* If it's still negative, we have a problem */
if (key < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ResizableBooleanArray: index out of bounds!");
}
/* Check if key is greater than allocated size */
GET_ATTR_size(INTERP, SELF, tail_pos);
GET_ATTR_resize_threshold(INTERP, SELF, head_pos);
offsetkey = key + head_pos;
if (offsetkey >= tail_pos)
SELF.set_integer_native(key+1);
SUPER(offsetkey, value);
}
/*
=item C<void set_integer_native(INTVAL size)>
Resizes the array to C<size> elements.
=cut
*/
VTABLE void set_integer_native(INTVAL size) {
unsigned char * bit_array;
size_t old_tail_pos, new_tail_pos, new_size_in_bytes, old_size_in_bytes;
/* Size respects any existing head position offset from unshift */
GET_ATTR_resize_threshold(INTERP, SELF, new_tail_pos);
GET_ATTR_size(INTERP, SELF, old_tail_pos);
new_tail_pos += size;
/* We are already at the requested size. Yay */
if (new_tail_pos == old_tail_pos)
return;
if (size < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ResizableBooleanArray: Can't resize!");
/* now set the new size, in bits */
SET_ATTR_size(INTERP, SELF, new_tail_pos);
/* convert sizes to bytes */
new_size_in_bytes = ROUND_BYTES(new_tail_pos);
old_size_in_bytes = ROUND_BYTES(old_tail_pos);
/* Nothing allocated yet */
GET_ATTR_bit_array(INTERP, SELF, bit_array);
if (!bit_array) {
void *new_bit_array = Parrot_gc_allocate_memory_chunk(INTERP, new_size_in_bytes);
memset(new_bit_array, 0, new_size_in_bytes);
SET_ATTR_bit_array(INTERP, SELF, (unsigned char *)new_bit_array);
/* The size is different, and doesn't fit within the current
* allocation */
}
else if (new_size_in_bytes != old_size_in_bytes) {
unsigned char * old_store = bit_array;
unsigned char * new_store =
(unsigned char *)Parrot_gc_allocate_memory_chunk(INTERP, new_tail_pos);
size_t copy_size =
new_size_in_bytes < old_size_in_bytes ? new_size_in_bytes : old_size_in_bytes;
memset(new_store, 0, new_tail_pos);
/* Replace old array with new array, and free old array */
SET_ATTR_bit_array(INTERP, SELF,
(unsigned char *)mem_sys_memmove(
new_store, old_store, copy_size));
mem_gc_free(INTERP, old_store);
}
}
/*
=item C<void push_integer(INTVAL value)>
Extends the array by adding an element of value C<value> to the end.
=cut
*/
VTABLE void push_integer(INTVAL value) {
UINTVAL tail_pos, head_pos;
INTVAL new_size;
GET_ATTR_size(INTERP, SELF, tail_pos);
GET_ATTR_resize_threshold(INTERP, SELF, head_pos);
new_size = tail_pos - head_pos;
SELF.set_integer_native(new_size + 1);
SELF.set_integer_keyed_int(new_size, value);
}
/*
=item C<void pop_integer(INTVAL value)>
Removes and returns the last element.
=cut
*/
VTABLE INTVAL pop_integer() {
UINTVAL new_size, tail_pos, head_pos;
INTVAL value;
if (SELF.elements() < 1)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ResizableBooleanArray: Can't pop from an empty array!");
GET_ATTR_size(INTERP, SELF, tail_pos);
GET_ATTR_resize_threshold(INTERP, SELF, head_pos);
new_size = tail_pos - head_pos;
value = SELF.get_integer_keyed_int(new_size - 1);
SELF.set_integer_native(new_size - 1);
return value;
}
/*
=item C<void unshift_integer(INTVAL value)>
Extends the array by adding an element of value C<value> to the
beginning.
=cut
*/
VTABLE void unshift_integer(INTVAL value) {
UINTVAL head_pos;
GET_ATTR_resize_threshold(INTERP, SELF, head_pos);
/* If the current head position offset is 0, size this thing up by one
* allocation unit */
if (head_pos<= 0) {
UINTVAL tail_pos;
unsigned char * old_bit_array;
size_t old_mem_size, new_mem_size;
unsigned char * new_bit_array;
GET_ATTR_size(INTERP, SELF, tail_pos);
GET_ATTR_resize_threshold(INTERP, SELF, head_pos);
GET_ATTR_bit_array(INTERP, SELF, old_bit_array);
/* Allocate an extra allocation unit of space in new array */
new_mem_size = ROUND_BYTES(tail_pos+ MIN_ALLOC);
new_bit_array = (unsigned char *)Parrot_gc_allocate_memory_chunk(INTERP, new_mem_size);
memset(new_bit_array, 0, new_mem_size);
/* Copy contents of old array to new array, moving the head
* position forward by one allocation unit (in bytes). */
old_mem_size = ROUND_BYTES(tail_pos);
mem_sys_memmove(new_bit_array + (BITS_TO_BYTES(MIN_ALLOC)),
old_bit_array, old_mem_size);
/* Replace old array with new array, and free old array */
SET_ATTR_bit_array(INTERP, SELF, new_bit_array);
mem_gc_free(INTERP, old_bit_array);
/* Added one allocation unit to the head position offset */
SET_ATTR_size(INTERP, SELF, tail_pos + MIN_ALLOC);
SET_ATTR_resize_threshold(INTERP, SELF, head_pos + MIN_ALLOC);
}
/* Move the head position */
GET_ATTR_resize_threshold(INTERP, SELF, head_pos);
SET_ATTR_resize_threshold(INTERP, SELF, --head_pos);
/* Assign the new value as the first item */
SELF.set_integer_keyed_int(0, value);
}
/*
=item C<void shift_integer(INTVAL value)>
Removes and returns the first element.
=cut
*/
VTABLE INTVAL shift_integer() {
INTVAL value;
UINTVAL tail_pos, head_pos;
if (SELF.elements() < 1)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"ResizableBooleanArray: Can't shift from an empty array!");
/* Get head value */
value = SELF.get_integer_keyed_int(0);
/* Move the head position */
GET_ATTR_resize_threshold(INTERP, SELF, head_pos);
SET_ATTR_resize_threshold(INTERP, SELF, ++head_pos);
/* If the head position offset is greater than our allocation unit
* size, size this thing down */
if (head_pos >= MIN_ALLOC) {
/* Allocate one allocation unit less of space in new array */
unsigned char *new_bit_array, *old_bit_array;
size_t new_mem_size;
GET_ATTR_size(INTERP, SELF, tail_pos);
new_mem_size = ROUND_BYTES(tail_pos - MIN_ALLOC);
new_bit_array =
(unsigned char *)Parrot_gc_allocate_memory_chunk(INTERP, new_mem_size);
memset(new_bit_array, 0, new_mem_size);
/* Copy contents of old array to new array, move the head position
* offset back by one allocation unit (in bytes) */
GET_ATTR_bit_array(INTERP, SELF, old_bit_array);
mem_sys_memmove(new_bit_array, old_bit_array + (BITS_TO_BYTES(MIN_ALLOC)),
new_mem_size);
/* Replace old array with new array, and free old array */
SET_ATTR_bit_array(INTERP, SELF, new_bit_array);
mem_gc_free(INTERP, old_bit_array);
/* Removed one allocation unit from the head position offset */
SET_ATTR_size(INTERP, SELF, tail_pos - MIN_ALLOC);
GET_ATTR_resize_threshold(INTERP, SELF, head_pos);
SET_ATTR_resize_threshold(INTERP, SELF, head_pos- MIN_ALLOC);
}
return value;
}
/*
=item C<INTVAL elements()>
=cut
*/
VTABLE INTVAL elements() {
UINTVAL tail_pos, head_pos;
GET_ATTR_size(INTERP, SELF, tail_pos);
GET_ATTR_resize_threshold(INTERP, SELF, head_pos);
return tail_pos - head_pos;
}
/*
=item C<INTVAL get_integer()>
Returns the number of elements in the array.
=cut
*/
VTABLE INTVAL get_integer() {
return SELF.elements();
}
/*
=item C<PMC *clone()>
Returns a copy of the array.
=cut
*/
VTABLE PMC *clone() {
UINTVAL tail_pos, head_pos;
unsigned char * my_bit_array, * dest_bit_array;
PMC * const dest = Parrot_pmc_new(INTERP, SELF->vtable->base_type);
GET_ATTR_bit_array(INTERP, SELF, my_bit_array);
GET_ATTR_size(INTERP, SELF, tail_pos);
GET_ATTR_resize_threshold(INTERP, SELF, head_pos);
SET_ATTR_size(INTERP, dest, tail_pos);
SET_ATTR_resize_threshold(INTERP, dest, head_pos);
if (my_bit_array) {
size_t size_in_bits = tail_pos / BITS_PER_CHAR + 1;
dest_bit_array = (unsigned char *)Parrot_gc_allocate_memory_chunk(INTERP, size_in_bits);
mem_sys_memcopy(dest_bit_array, my_bit_array, size_in_bits);
}
else
dest_bit_array = NULL;
SET_ATTR_bit_array(INTERP, dest, dest_bit_array);
PObj_custom_destroy_SET(dest);
return dest;
}
/*
=back
=head2 Freeze/thaw Interface
=over 4
=item C<void freeze(PMC *info)>
Used to archive the string.
=cut
*/
VTABLE void freeze(PMC *info) {
/* XXX Dino - I'm concerned about freezing the entire
allocated block of memory, it's dependent on the
BITS_PER_CHAR value.
Maybe we need to store that during the freeze as well
and use it during thaw?
*/
STRING *s;
UINTVAL tail_pos, rounded_size, head_pos;
unsigned char *bit_array;
GET_ATTR_size(INTERP, SELF, tail_pos);
GET_ATTR_resize_threshold(INTERP, SELF, head_pos);
GET_ATTR_bit_array(INTERP, SELF, bit_array);
rounded_size = ROUND_BYTES(tail_pos);
VTABLE_push_integer(INTERP, info, head_pos);
VTABLE_push_integer(INTERP, info, tail_pos);
s = Parrot_str_new(INTERP, (char*)bit_array, rounded_size);
VTABLE_push_string(INTERP, info, s);
}
/*
=item C<void thaw(PMC *info)>
Used to unarchive the string.
=cut
*/
VTABLE void thaw(PMC *info) {
const UINTVAL head_pos = VTABLE_shift_integer(INTERP, info);
const UINTVAL tail_pos = VTABLE_shift_integer(INTERP, info);
STRING * const s = VTABLE_shift_string(INTERP, info);
unsigned char * const bit_array = (unsigned char*)Parrot_str_to_cstring(INTERP, s);
SET_ATTR_size(INTERP, SELF, tail_pos);
SET_ATTR_resize_threshold(INTERP, SELF, head_pos);
SET_ATTR_bit_array(INTERP, SELF, bit_array);
}
} /* pmclass */
/*
=back
=head1 SEE ALSO
F<docs/pdds/pdd17_basic_types.pod>.
=head1 HISTORY
Initial version - Matt Fowles 2004-06-11
Changed allocator to double size - Matt Fowles 2004-06-15
Added push_integer - Bernhard Schmalhofer 2004-10-17
Changed allocation code, added - Dino Morelli 2005-06-10
push_, pop_, shift_,
unshift_integer, freeze, thaw
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/