initial import
[panda.git] / goo / st-byte-array.c
blobe2a261d4e083f7f8e64d43dc569fbcdc7d0d0d58
1 /*
2 * st-byte-array.c
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
22 * THE SOFTWARE.
25 #include <st-byte-array.h>
26 #include <st-object.h>
27 #include <st-utils.h>
28 #include <st-mini.h>
29 #include <string.h>
31 ST_DEFINE_VTABLE (st_byte_array, st_heap_object_vtable ());
33 static bool
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))
46 return false;
48 } else {
51 if (!st_object_virtual (object)->parent_table->verify (object))
52 return false;
57 // if (!st_object_super_virtual (object)->verify (object))
58 // return false;
60 // variable size
61 st_oop_t size = st_byte_array_size (object);
62 if (!st_object_is_smi (size) || !(st_smi_value (size) > 0))
63 return false;
65 return true;
68 INLINE st_smi_t
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
80 size += 1;
82 int r = size % sizeof (st_oop_t);
84 if (r == 0)
85 return size;
86 else
87 return size - r + sizeof (st_oop_t);
90 static st_oop_t
91 allocate_arrayed (st_oop_t klass, st_smi_t size)
93 g_assert (size > 0);
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);
103 return array;
106 static st_oop_t
107 allocate (st_oop_t klass)
109 return allocate_arrayed (klass, 0);
112 static bool
113 is_byte_array (void)
115 return true;
118 static bool
119 is_arrayed (void)
121 return true;
124 static bool
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))
130 return false;
132 size1 = st_smi_value (st_byte_array_size (object));
133 size2 = st_smi_value (st_byte_array_size (another));
135 if (size1 != size2)
136 return false;
138 return memcmp (st_byte_array_bytes (object),
139 st_byte_array_bytes (another),
140 size1) == 0;
143 static guint
144 byte_array_hash (st_oop_t object)
146 const signed char *p = (signed char *) st_byte_array_bytes (object);
147 guint32 h = *p;
149 long size = st_smi_value (st_byte_array_size (object));
151 if (size == 0 || !h)
152 return h;
154 for (st_smi_t i = 1; i < size; i++) {
155 h = (h << 5) - h + *(p + i);
158 return h;
161 static void
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;