Added spec:commit task to commit changes to spec/ruby sources.
[rbx.git] / shotgun / lib / bytearray.c
blob6aed97fef4ed3e530aee6263eebd169e28eaed09
1 #include <stdlib.h>
2 #include <string.h>
4 #include "shotgun/lib/shotgun.h"
5 #include "shotgun/lib/object.h"
6 #include "shotgun/lib/bytearray.h"
8 OBJECT bytearray_new(STATE, unsigned int size) {
9 unsigned int words;
10 OBJECT obj;
12 words = size / SIZE_OF_OBJECT;
13 if(size % SIZE_OF_OBJECT != 0) {
14 words += 1;
17 obj = bytearray_allocate_with_extra(state, words);
18 object_make_byte_storage(state, obj);
19 fast_memfill(BYTES_OF(obj), 0, words);
20 object_initialize_bytes(state, obj);
21 return obj;
24 OBJECT bytearray_new_dirty(STATE, unsigned int size) {
25 unsigned int words;
26 OBJECT obj;
28 words = size / SIZE_OF_OBJECT;
29 if(size % SIZE_OF_OBJECT != 0) {
30 words += 1;
33 obj = object_memory_new_dirty_object(state->om, BASIC_CLASS(bytearray), words);
34 object_make_byte_storage(state, obj);
36 return obj;
39 OBJECT bytearray_dup(STATE, OBJECT self) {
40 OBJECT obj;
41 int words = NUM_FIELDS(self);
43 obj = object_memory_new_dirty_object(state->om, BASIC_CLASS(bytearray), words);
44 object_make_byte_storage(state, obj);
46 object_copy_body(state, self, obj);
48 return obj;
51 char *bytearray_as_string(STATE, OBJECT self) {
52 char *str;
53 char *out;
54 unsigned int sz;
56 str = (char*)bytearray_byte_address(state, self);
58 sz = object_size(state, self);
59 out = ALLOC_N(char, sz);
60 memcpy(out, str, sz);
62 return out;
65 OBJECT iseq_new(STATE, unsigned int sz) {
66 OBJECT obj;
67 int fields;
69 fields = sz / SIZE_OF_OBJECT;
70 if(sz % SIZE_OF_OBJECT != 0) {
71 fields += 1;
73 obj = NEW_OBJECT(state->global->iseq, fields);
74 object_make_byte_storage(state, obj);
76 return obj;
79 static inline uint32_t read_int_from_be(uint8_t *str) {
80 return (uint32_t)((str[0] << 24)
81 | (str[1] << 16)
82 | (str[2] << 8 )
83 | str[3] );
86 void iseq_flip(STATE, OBJECT self, OBJECT output) {
87 uint8_t *buf;
88 uint32_t *ibuf;
89 uint32_t val;
90 int i, f;
92 f = object_size(state, self);
93 buf = (uint8_t*)bytearray_byte_address(state, self);
94 ibuf = (uint32_t*)bytearray_byte_address(state, output);
96 /* A sanity check. The first thing is always an instruction,
97 * and we've got less that 1024 instructions, so if it's less
98 * it's already been flipped. */
99 if(*(uint32_t*)buf < 1024) return;
101 for(i = 0; i < f; i += 4, ibuf++) {
102 val = read_int_from_be(buf + i);
103 *ibuf = val;