Debian Codegraph @

terps/tads/tads3/vmbytarr.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2001, 2002 Michael J. Roberts. All Rights Reserved.
3 *
4 * Please see the accompanying license file, LICENSE.TXT, for information
5 * on using and copying this software.
6 */
7 /*
8 Name
9 vmbytarr.h - T3 ByteArray metaclass
10 Function
11
12 Notes
13
14 Modified
15 06/05/01 MJRoberts - Creation
16 */
17
18 #ifndef VMBYTARR_H
19 #define VMBYTARR_H
20
21 #include <stdlib.h>
22 #include "os.h"
23 #include "vmtype.h"
24 #include "vmobj.h"
25 #include "vmglob.h"
26
27 /* ------------------------------------------------------------------------ */
28 /*
29 * A ByteArray is simply an array of byte values. This class provides a
30 * simple, fast mechanism to store blocks of binary data, so it is not a
31 * subclass of Array and is not a Collection.
32 *
33 * The image file data for a byte array is simple:
34 *
35 * UINT4 number of bytes
36 *. BYTE bytes[1..number_of_bytes]
37 *
38 * Internally, we store the array data in chunks of 32k each. Our
39 * extension is a first-level page table, pointing to the chunks:
40 *
41 * UINT4 number of elements
42 *. unsigned char **page0
43 *. unsigned char **page1
44 *. ...
45 *
46 * Each pageN pointer points to a second-level page table, which consists
47 * of (up to) 8192 pointers to the actual pages. Since a page is 32k, and
48 * we can store 8k pointers per second-level table, each second-level
49 * table is capable of referencing 256MB. By design, we can store up to
50 * 4GB, so we need at most 16 second-level tables.
51 *
52 * The extension is allocated according to the actual number of
53 * second-level tables we require for the element count. Each
54 * second-level page is allocated to 8192*sizeof(char *), except the last
55 * second-level page, which is allocated to N*sizeof(char *) where N is
56 * the number of elements required in the last second-level table. Each
57 * page is allocated to 32K bytes, except the last, which is allocated to
58 * the actual size needed.
59 *
60 * To access an element at index i, we calculate s1 (the page table
61 * selector) as i/(32k*8k) == i/256M; s2 (the page selector within the
62 * selected page table) as (i%256M)/32k; and s3 (the byte selector within
63 * the page) as i%32k. The byte is then accessed as
64 *
65 * page[s1][s2][s3]
66 */
67 class CVmObjByteArray: public CVmObject
68 {
69 friend class CVmMetaclassByteArray;
70 friend class bytearray_undo_rec;
71
72 public:
73 /* metaclass registration object */
74 static class CVmMetaclass *metaclass_reg_;
75 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
76
77 /* am I of the given metaclass? */
78 virtual int is_of_metaclass(class CVmMetaclass *meta) const
79 {
80 /* try my own metaclass and my base class */
81 return (meta == metaclass_reg_
82 || CVmObject::is_of_metaclass(meta));
83 }
84
85 /* create dynamically using stack arguments */
86 static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr,
87 uint argc);
88
89 /*
90 * call a static property - we don't have any of our own, so simply
91 * "inherit" the base class handling
92 */
93 static int call_stat_prop(VMG_ vm_val_t *result,
94 const uchar **pc_ptr, uint *argc,
95 vm_prop_id_t prop);
96
97 /* reserve constant data */
98 virtual void reserve_const_data(VMG_ class CVmConstMapper *mapper,
99 vm_obj_id_t self)
100 {
101 /*
102 * we reference no other objects and cannot ourselves be converted
103 * to constant data, so there's nothing to do here
104 */
105 }
106
107 /* convert to constant data */
108 virtual void convert_to_const_data(VMG_ class CVmConstMapper *mapper,
109 vm_obj_id_t self)
110 {
111 /*
112 * we reference no data and cannot be converted to constant data,
113 * so there's nothing to do
114 */
115 }
116
117 /* cast to string - assumes the bytes as Latin-1 characters */
118 virtual const char *cast_to_string(VMG_ vm_obj_id_t self,
119 vm_val_t *new_str) const;
120
121 /* create an array with no initial contents */
122 static vm_obj_id_t create(VMG_ int in_root_set);
123
124 /* create an array with a given number of elements */
125 static vm_obj_id_t create(VMG_ int in_root_set,
126 unsigned long element_count);
127
128 /* create an array by mapping a string through a character set */
129 static vm_obj_id_t create_from_string(
130 VMG_ const vm_val_t *strval, const char *str, const vm_val_t *mapval);
131
132 /* create from binary data */
133 static vm_obj_id_t create_from_bytes(
134 VMG_ int in_root_set, const char *buf, size_t len);
135
136 /*
137 * determine if an object is a byte array - it is if the object's
138 * virtual metaclass registration index matches the class's static
139 * index
140 */
141 static int is_byte_array(VMG_ vm_obj_id_t obj)
142 { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); }
143
144 /* notify of deletion */
145 void notify_delete(VMG_ int in_root_set);
146
147 /* set a property */
148 void set_prop(VMG_ class CVmUndo *undo,
149 vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val);
150
151 /* get a property */
152 int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
153 vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc);
154
155 /* undo operations */
156 void notify_new_savept() { }
157 void apply_undo(VMG_ struct CVmUndoRecord *rec);
158 void discard_undo(VMG_ struct CVmUndoRecord *);
159
160 /* our data are just bytes - we reference nothing */
161 void mark_undo_ref(VMG_ struct CVmUndoRecord *) { }
162 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
163 void mark_refs(VMG_ uint /*state*/) { }
164 void remove_stale_weak_refs(VMG0_) { }
165
166 /* load from an image file */
167 void load_from_image(VMG_ vm_obj_id_t self, const char *ptr, size_t siz);
168
169 /* rebuild for image file */
170 virtual ulong rebuild_image(VMG_ char *buf, ulong buflen);
171
172 /* reload from the image file */
173 void reload_from_image(VMG_ vm_obj_id_t self,
174 const char *ptr, size_t siz);
175
176 /* save to a file */
177 void save_to_file(VMG_ class CVmFile *fp);
178
179 /* restore from a file */
180 void restore_from_file(VMG_ vm_obj_id_t self,
181 class CVmFile *fp, class CVmObjFixup *fixup);
182
183 /* index the array */
184 int index_val_q(VMG_ vm_val_t *result, vm_obj_id_t self,
185 const vm_val_t *index_val);
186
187 /* set an indexed element of the array */
188 int set_index_val_q(VMG_ vm_val_t *new_container, vm_obj_id_t self,
189 const vm_val_t *index_val, const vm_val_t *new_val);
190
191 /*
192 * Check a value for equality. We will match another byte array with
193 * the same number of elements and the same value for each element.
194 */
195 int equals(VMG_ vm_obj_id_t self, const vm_val_t *val, int depth) const;
196
197 /* calculate a hash value for the array */
198 uint calc_hash(VMG_ vm_obj_id_t self, int depth) const;
199
200 /*
201 * assume that we've been changed since loading, if we came from the
202 * image file
203 */
204 int is_changed_since_load() const { return TRUE; }
205
206 /* get the number of elements in the array */
207 unsigned long get_element_count() const
208 { return t3rp4u(get_ext_ptr()); }
209
210 /* get the byte at a given 1-based index */
211 unsigned char get_element(unsigned long idx) const
212 {
213 size_t avail;
214 return *get_ele_ptr(idx, &avail);
215 }
216
217 /*
218 * construction: copy (without undo) bytes from a buffer into the byte
219 * array
220 */
221 void cons_copy_from_buf(const unsigned char *buf,
222 unsigned long idx, size_t cnt)
223 {
224 /* copy the bytes into our array */
225 copy_from_buf(buf, idx, cnt);
226 }
227
228 /*
229 * Write the specified region of the array to a file. Returns zero on
230 * success, non-zero on failure.
231 */
232 int write_to_file(class CVmDataSource *fp, unsigned long start_idx,
233 unsigned long len) const;
234
235 /*
236 * Read bytes from a file into a region of the array, replacing
237 * existing bytes in the array; saves undo for the change. Returns
238 * the number of bytes actually read from the file, which will be less
239 * than the number of bytes requested if we reach the end of the file
240 * before satisfying the request.
241 */
242 unsigned long read_from_file(VMG_ vm_obj_id_t self,
243 class CVmDataSource *fp,
244 unsigned long start_idx, unsigned long len,
245 int save_undo);
246
247 /*
248 * write to a 'data' mode file - returns zero on success, non-zero on
249 * failure
250 */
251 int write_to_data_file(class CVmDataSource *fp);
252
253 /*
254 * read from a 'data' mode file, creating a new ByteArray object to
255 * hold the bytes from the file
256 */
257 static int read_from_data_file(VMG_ vm_val_t *retval,
258 class CVmDataSource *fp);
259
260 /*
261 * Copy bytes into a buffer, starting at the given 1-based index.
262 * Returns the number of bytes actually copied (this might be less than
263 * the requested size, because the ByteArray might not have enough
264 * contents to fill the request).
265 */
266 size_t copy_to_buf(unsigned char *buf, unsigned long idx, size_t cnt)
267 const;
268
269 /*
270 * Copy bytes from a buffer into the array at the given 1-based index,
271 * saving undo. Returns the actual number of bytes copied, which might
272 * be less than the requested size, since the ByteArray might not be
273 * big enough to accommodate the write.
274 */
275 size_t copy_from_buf_undo(VMG_ vm_obj_id_t self, const unsigned char *buf,
276 unsigned long idx, size_t cnt);
277
278 protected:
279 /* load image data */
280 virtual void load_image_data(VMG_ const char *ptr, size_t siz);
281
282 /* create a list with no initial contents */
283 CVmObjByteArray() { ext_ = 0; }
284
285 /*
286 * create a list with a given number of elements, for construction
287 * of the list element-by-element
288 */
289 CVmObjByteArray(VMG_ unsigned long byte_count);
290
291 /* get a pointer to my extension */
292 const char *get_ext_ptr() const { return ext_; }
293
294 /*
295 * get my extension data pointer for construction purposes - this is
296 * a writable pointer, so that a caller can fill our data buffer
297 * during construction
298 */
299 char *cons_get_ext_ptr() const { return ext_; }
300
301 /* allocate space for the array, given the number of elements */
302 void alloc_array(VMG_ unsigned long element_count);
303
304 /* property evaluator - undefined function */
305 int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; }
306
307 /* property evaluator - length */
308 int getp_length(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
309
310 /* property evaluator - subarray */
311 int getp_subarray(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
312
313 /* property evaluator - copy from another byte array */
314 int getp_copy_from(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
315
316 /* property evaluator - fill with a value */
317 int getp_fill_val(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
318
319 /* property evaluator - convert to string */
320 int getp_to_string(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
321
322 /* property evaluator - read integer */
323 int getp_read_int(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
324
325 /* property evaluator - write integer */
326 int getp_write_int(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
327
328 /* property evaluator - packBytes */
329 int getp_packBytes(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
330
331 /* property evaluator - unpackBytes */
332 int getp_unpackBytes(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
333
334 /* property evaluator - static ByteArray.unpackBytes */
335 static int static_packBytes(VMG_ vm_val_t *retval, uint *argc);
336
337 /* property evaluator - sha256 */
338 int getp_sha256(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
339
340 /* property evaluator - digestMD5 */
341 int getp_digestMD5(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
342
343 /*
344 * Given a 1-based index, get a pointer to the byte at the index, and
345 * the number of contiguous bytes available starting with that byte.
346 * The available byte count doesn't take into account a short last
347 * page, but simply returns the maximum number of bytes that would be
348 * available on the page if it were allocated to full size; the caller
349 * is responsible for ensuring that there is no reading or writing
350 * past the end of the array.
351 */
352 unsigned char *get_ele_ptr(unsigned long idx, size_t *bytes_avail) const
353 {
354 size_t s1;
355 size_t s2;
356 size_t s3;
357
358 /* convert to a zero-based index */
359 --idx;
360
361 /*
362 * calculate the page table index - since each page holds 32k
363 * bytes and each page table points to 8k pages, divide by 32k*8k
364 * == 2^15*2^13 == 2^28
365 */
366 s1 = idx >> 28;
367
368 /*
369 * calculate the page index within the page table - each page
370 * holds 32k, so calculate the excess from the page table selector
371 * (i.e, idx % 32k*8k) and then divide by 32k == 2^15
372 */
373 s2 = (idx & 0x0FFFFFFF) >> 15;
374
375 /*
376 * calculate the page offset - this is simply the excess from the
377 * page index
378 */
379 s3 = idx & 0x7FFF;
380
381 /*
382 * Each page holds 32k, so the number of contiguous bytes starting
383 * at this byte is 32k less the index.
384 */
385 *bytes_avail = (32*1024) - s3;
386
387 /*
388 * dereference the extension to get the page table, deference the
389 * page table to get the page, and index the page by the offset
390 */
391 return get_page_table_ptr(s1)[s2] + s3;
392 }
393
394 /*
395 * Given a page table selector, return a pointer to the selected page
396 * table.
397 */
398 unsigned char **get_page_table_ptr(size_t s) const
399 {
400 return get_page_table_array()[s];
401 }
402
403 /*
404 * Get a pointer to the page table array
405 */
406 unsigned char ***get_page_table_array() const
407 {
408 /* the page table array starts after the element count */
409 return (unsigned char ***)(ext_ + 4);
410 }
411
412 /* fill the given (1-based index) range with the given byte value */
413 void fill_with(unsigned char val, unsigned long start_idx,
414 unsigned long cnt);
415
416 /* copy bytes from another byte array into this one */
417 void copy_from(unsigned long dst_idx,
418 CVmObjByteArray *src_arr,
419 unsigned long src_start_idx, unsigned long cnt);
420
421 /* move bytes within this array */
422 void move_bytes(unsigned long dst_idx, unsigned long src_idx,
423 unsigned long cnt);
424
425 /* copy bytes from a buffer into the array */
426 void copy_from_buf(const unsigned char *buf,
427 unsigned long idx, size_t cnt);
428
429 /* map to a string */
430 size_t map_to_string(unsigned long idx, unsigned long len,
431 class CVmObjString *str, size_t str_len,
432 class CCharmapToUni *mapper);
433
434 /* save undo for a change to a range of the array */
435 void save_undo(VMG_ vm_obj_id_t self, unsigned long start_idx,
436 unsigned long cnt);
437
438 /* set the number of bytes in the array */
439 void set_element_count(unsigned long cnt)
440 { oswp4(cons_get_ext_ptr(), cnt); }
441
442 /* property evaluation function table */
443 static int (CVmObjByteArray::*func_table_[])(
444 VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
445 };
446
447 /* ------------------------------------------------------------------------ */
448 /*
449 * Registration table object
450 */
451 class CVmMetaclassByteArray: public CVmMetaclass
452 {
453 public:
454 /* get the global name */
455 const char *get_meta_name() const { return "bytearray/030002"; }
456
457 /* create from image file */
458 void create_for_image_load(VMG_ vm_obj_id_t id)
459 {
460 new (vmg_ id) CVmObjByteArray();
461 G_obj_table->set_obj_gc_characteristics(id, FALSE, FALSE);
462 }
463
464 /* create from restoring from saved state */
465 void create_for_restore(VMG_ vm_obj_id_t id)
466 {
467 new (vmg_ id) CVmObjByteArray();
468 G_obj_table->set_obj_gc_characteristics(id, FALSE, FALSE);
469 }
470
471 /* create dynamically using stack arguments */
472 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
473 { return CVmObjByteArray::create_from_stack(vmg_ pc_ptr, argc); }
474
475 /* call a static property */
476 int call_stat_prop(VMG_ vm_val_t *result,
477 const uchar **pc_ptr, uint *argc,
478 vm_prop_id_t prop)
479 {
480 return CVmObjByteArray::
481 call_stat_prop(vmg_ result, pc_ptr, argc, prop);
482 }
483 };
484
485 #endif /* VMBYTARR_H */
486
487
488 /*
489 * Register the class
490 */
491 VM_REGISTER_METACLASS(CVmObjByteArray)