Fixed lots of compiler bugs. Added more primitives. Reworked interpreter
[panda.git] / goo / st-byte-array.c
blobb766756633dd69aaa700e3d285b45d9da5f93945
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-descriptor.h"
30 #include <string.h>
32 #define ST_BYTE_ARRAY(oop) ((STByteArray *) ST_POINTER (oop))
34 st_oop
35 st_byte_array_size (st_oop object)
37 return ST_BYTE_ARRAY (object)->size;
40 guchar *
41 st_byte_array_bytes (st_oop object)
43 return ST_BYTE_ARRAY (object)->bytes;
46 bool
47 st_byte_array_range_check (st_oop object, st_smi i)
49 return 1 <= i && i <= st_smi_value (st_byte_array_size (object));
52 guchar
53 st_byte_array_at (st_oop object, st_smi i)
55 g_assert (1 <= i && i <= st_smi_value (st_byte_array_size (object)));
57 return st_byte_array_bytes (object)[i - 1];
60 void
61 st_byte_array_at_put (st_oop object, st_smi i, guchar value)
63 g_assert (1 <= i && i <= st_smi_value (st_byte_array_size (object)));
65 st_byte_array_bytes (object)[i - 1] = value;
68 INLINE st_smi
69 round_size (st_smi size)
71 /* round off number of bytes to a multiple of st_oop 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);
84 if (r == 0)
85 return size;
86 else
87 return size - r + sizeof (st_oop);
90 static st_oop
91 allocate_arrayed (st_oop klass, st_smi size)
93 g_assert (size >= 0);
95 st_smi size_rounded = round_size (size);
96 st_oop array = st_allocate_object (ST_TYPE_SIZE (STByteArray) + (size_rounded / sizeof (st_oop)));
98 st_heap_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
107 allocate (st_oop klass)
109 return allocate_arrayed (klass, 0);
112 bool
113 st_byte_array_equal (st_oop object, st_oop other)
115 st_smi size, size_other;
117 if (st_object_class (other) != st_byte_array_class &&
118 st_object_class (other) != st_string_class &&
119 st_object_class (other) != st_symbol_class)
120 return false;
122 size = st_smi_value (st_byte_array_size (object));
123 size_other = st_smi_value (st_byte_array_size (other));
125 if (size != size_other)
126 return false;
128 return memcmp (st_byte_array_bytes (object), st_byte_array_bytes (other), size) == 0;
131 guint
132 st_byte_array_hash (st_oop object)
134 const signed char *p = (signed char *) st_byte_array_bytes (object);
135 guint32 h = *p;
137 long size = st_smi_value (st_byte_array_size (object));
139 if (size == 0 || !h)
140 return h;
142 for (st_smi i = 1; i < size; i++) {
143 h = (h << 5) - h + *(p + i);
146 return h;
150 const STDescriptor *
151 st_byte_array_descriptor (void)
153 static const STDescriptor __descriptor =
154 { .allocate = allocate,
155 .allocate_arrayed = allocate_arrayed,
158 return & __descriptor;