4 * Copyright (C) 2008 Vincent Geddes
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include <st-byte-array.h>
26 #include <st-object.h>
31 ST_DEFINE_VTABLE (st_byte_array
, st_heap_object_vtable ());
34 byte_array_verify (st_oop_t object
)
37 // printf ("%p %p %p %p\n", st_object_virtual (object), st_object_virtual (object)->parent_table,
38 // st_object_virtual (object)->verify, st_object_virtual (object)->parent_table->verify);
41 // printf ("%i ", st_object_virtual (object) == st_symbol_vtable ());
43 if (st_object_is_symbol (object
)) {
45 if (!st_object_virtual (object
)->parent_table
->parent_table
->verify (object
))
51 if (!st_object_virtual (object
)->parent_table
->verify (object
))
57 // if (!st_object_super_virtual (object)->verify (object))
61 st_oop_t size
= st_byte_array_size (object
);
62 if (!st_object_is_smi (size
) || !(st_smi_value (size
) > 0))
69 round_size (st_smi_t size
)
71 /* round off number of bytes to a multiple of st_oop_t size (i.e. a multiple of 4 or 8 bytes,
72 * depending on the platform).
74 * we also include space for a null terminator. By doing this, we don't
75 * have to allocate and initialize a new terminated string when interfacing with C string functions
77 * TODO: Only include null terminator for *String subclasses of ByteArray
82 int r
= size
% sizeof (st_oop_t
);
87 return size
- r
+ sizeof (st_oop_t
);
91 allocate_arrayed (st_oop_t klass
, st_smi_t size
)
95 st_smi_t size_rounded
= round_size (size
);
96 st_oop_t array
= st_allocate_object ((sizeof (st_byte_array_t
) / sizeof (st_oop_t
)) + size_rounded
/ sizeof (st_oop_t
));
98 st_object_initialize_header (array
, klass
);
99 _ST_BYTE_ARRAY (array
)->size
= st_smi_new (size
);
101 memset (st_byte_array_bytes (array
), 0, size_rounded
);
107 allocate (st_oop_t klass
)
109 return allocate_arrayed (klass
, 0);
125 byte_array_equal (st_oop_t object
, st_oop_t another
)
127 st_smi_t size1
, size2
;
129 if (!st_object_is_byte_array (another
))
132 size1
= st_smi_value (st_byte_array_size (object
));
133 size2
= st_smi_value (st_byte_array_size (another
));
138 return memcmp (st_byte_array_bytes (object
),
139 st_byte_array_bytes (another
),
144 byte_array_hash (st_oop_t object
)
146 const signed char *p
= (signed char *) st_byte_array_bytes (object
);
149 long size
= st_smi_value (st_byte_array_size (object
));
154 for (st_smi_t i
= 1; i
< size
; i
++) {
155 h
= (h
<< 5) - h
+ *(p
+ i
);
162 st_byte_array_vtable_init (st_vtable_t
*table
)
164 assert_static (sizeof (st_byte_array_t
) == (sizeof (st_header_t
) + sizeof (st_oop_t
)));
166 table
->allocate
= allocate
;
167 table
->allocate_arrayed
= allocate_arrayed
;
169 table
->verify
= byte_array_verify
;
171 table
->is_byte_array
= is_byte_array
;
172 table
->is_arrayed
= is_arrayed
;
174 table
->equal
= byte_array_equal
;
175 table
->hash
= byte_array_hash
;