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"
32 #define ST_BYTE_ARRAY(oop) ((STByteArray *) ST_POINTER (oop))
35 st_byte_array_size (st_oop object
)
37 return ST_BYTE_ARRAY (object
)->size
;
41 st_byte_array_bytes (st_oop object
)
43 return ST_BYTE_ARRAY (object
)->bytes
;
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
));
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];
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
;
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
82 int r
= size
% sizeof (st_oop
);
87 return size
- r
+ sizeof (st_oop
);
91 allocate_arrayed (st_oop klass
, st_smi size
)
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
);
107 allocate (st_oop klass
)
109 return allocate_arrayed (klass
, 0);
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
)
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
)
128 return memcmp (st_byte_array_bytes (object
), st_byte_array_bytes (other
), size
) == 0;
132 st_byte_array_hash (st_oop object
)
134 const signed char *p
= (signed char *) st_byte_array_bytes (object
);
137 long size
= st_smi_value (st_byte_array_size (object
));
142 for (st_smi i
= 1; i
< size
; i
++) {
143 h
= (h
<< 5) - h
+ *(p
+ i
);
151 st_byte_array_descriptor (void)
153 static const STDescriptor __descriptor
=
154 { .allocate
= allocate
,
155 .allocate_arrayed
= allocate_arrayed
,
158 return & __descriptor
;