Removed the notion of a "large" context. For simplicity, all contexts
[panda.git] / src / st-byte-array.c
blob9802becb55c75041b52231b597be6f990b6f4d82
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 static st_oop
33 allocate_arrayed (st_space *space,
34 st_oop class,
35 st_smi size)
37 st_uint size_oops;
38 st_oop array;
40 st_assert (size >= 0);
42 /* add 1 byte for NULL terminator. Allows toll-free bridging with C string function */
43 size_oops = ST_ROUNDED_UP_OOPS (size + 1);
45 array = st_space_allocate_object (space, class, ST_SIZE_OOPS (struct st_byte_array) + size_oops);
46 ST_ARRAYED_OBJECT (array)->size = st_smi_new (size);
48 memset (st_byte_array_bytes (array), 0, ST_OOPS_TO_BYTES (size_oops));
50 return array;
53 bool
54 st_byte_array_equal (st_oop object, st_oop other)
56 st_smi size, size_other;
58 if (st_object_class (other) != st_byte_array_class &&
59 st_object_class (other) != st_string_class &&
60 st_object_class (other) != st_symbol_class)
61 return false;
63 size = st_smi_value (ST_ARRAYED_OBJECT (object)->size);
64 size_other = st_smi_value (ST_ARRAYED_OBJECT (other)->size);
66 if (size != size_other)
67 return false;
69 return memcmp (st_byte_array_bytes (object), st_byte_array_bytes (other), size) == 0;
72 st_uint
73 st_byte_array_hash (st_oop object)
75 const signed char *p = (signed char *) st_byte_array_bytes (object);
76 st_uint h = *p;
78 long size = st_smi_value (ST_ARRAYED_OBJECT (object)->size);
80 if (size == 0 || !h)
81 return h;
83 for (st_smi i = 1; i < size; i++) {
84 h = (h << 5) - h + *(p + i);
87 return h;
90 static st_oop
91 byte_array_copy (st_oop object)
93 st_oop copy;
94 st_smi size;
96 size = st_smi_value (ST_ARRAYED_OBJECT (object)->size);
98 copy = allocate_arrayed (memory->moving_space, st_object_class (object), size);
100 memcpy (st_byte_array_bytes (copy),
101 st_byte_array_bytes (object),
102 size);
104 return copy;
107 static st_uint
108 byte_array_size (st_oop object)
110 st_uint size;
112 size = st_smi_value (st_arrayed_object_size (object));
113 return ST_SIZE_OOPS (struct st_arrayed_object) + ST_ROUNDED_UP_OOPS (size + 1);
116 static void
117 byte_array_contents (st_oop object, struct contents *contents)
119 contents->oops = NULL;
120 contents->size = 0;
123 st_descriptor *
124 st_byte_array_descriptor (void)
126 static st_descriptor __descriptor =
127 { .allocate = NULL,
128 .allocate_arrayed = allocate_arrayed,
129 .copy = byte_array_copy,
130 .size = byte_array_size,
131 .contents = byte_array_contents,
134 return & __descriptor;