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"
28 #include "st-descriptor.h"
33 allocate_arrayed (st_space
*space
,
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
));
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
)
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
)
69 return memcmp (st_byte_array_bytes (object
), st_byte_array_bytes (other
), size
) == 0;
73 st_byte_array_hash (st_oop object
)
75 const signed char *p
= (signed char *) st_byte_array_bytes (object
);
78 long size
= st_smi_value (ST_ARRAYED_OBJECT (object
)->size
);
83 for (st_smi i
= 1; i
< size
; i
++) {
84 h
= (h
<< 5) - h
+ *(p
+ i
);
91 byte_array_copy (st_oop object
)
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
),
108 byte_array_size (st_oop object
)
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);
117 byte_array_contents (st_oop object
, struct contents
*contents
)
119 contents
->oops
= NULL
;
124 st_byte_array_descriptor (void)
126 static st_descriptor __descriptor
=
128 .allocate_arrayed
= allocate_arrayed
,
129 .copy
= byte_array_copy
,
130 .size
= byte_array_size
,
131 .contents
= byte_array_contents
,
134 return & __descriptor
;