Implemented a resizable heap abstraction over mmap(). Allows us to
[panda.git] / src / st-object.c
blob8c3103257ddc3631f752a40c406e060698a11d03
1 /*
2 * st-object.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-object.h"
26 #include "st-universe.h"
27 #include "st-behavior.h"
28 #include "st-small-integer.h"
29 #include "st-association.h"
30 #include "st-float.h"
31 #include "st-array.h"
32 #include "st-byte-array.h"
33 #include "st-symbol.h"
34 #include "st-object.h"
35 #include "st-character.h"
36 #include "st-unicode.h"
38 bool
39 st_object_equal (st_oop object, st_oop other)
41 if (st_object_class (object) == st_smi_class)
42 return st_smi_equal (object, other);
44 if (st_object_class (object) == st_float_class)
45 return st_float_equal (object, other);
47 if (st_object_class (object) == st_character_class)
48 return st_character_equal (object, other);
50 if (st_object_class (object) == st_association_class)
51 return st_association_equal (object, other);
53 if (st_object_class (object) == st_symbol_class)
54 return st_symbol_equal (object, other);
56 if (st_object_class (object) == st_byte_array_class ||
57 st_object_class (object) == st_string_class)
58 return st_byte_array_equal (object, other);
60 return object == other;
63 st_uint
64 st_object_hash (st_oop object)
66 if (st_object_class (object) == st_smi_class)
67 return st_smi_hash (object);
69 if (st_object_class (object) == st_byte_array_class ||
70 st_object_class (object) == st_string_class ||
71 st_object_class (object) == st_symbol_class)
72 return st_byte_array_hash (object);
74 if (st_object_class (object) == st_float_class)
75 return st_float_hash (object);
77 if (st_object_class (object) == st_character_class)
78 return st_character_hash (object);
80 if (st_object_class (object) == st_association_class)
81 return st_association_hash (object);
83 return st_smi_value (ST_HEADER (object)->hash);
86 char *
87 st_object_printString (st_oop object)
89 char *class_name;
90 char *string;
92 class_name = (char *) st_byte_array_bytes (ST_CLASS (st_object_class (object))->name);
94 // SmallInteger
95 if (st_object_is_smi (object))
96 string = st_strdup_printf ("%li", st_smi_value (object));
98 // Float
99 else if (st_object_class (object) == st_float_class)
100 string = st_strdup_printf ("%g", st_float_value (object));
102 // ByteString
103 else if (st_object_is_string (object))
104 string = st_strdup_printf ("'%s'", (char *) st_byte_array_bytes (object));
106 // ByteSymbol
107 else if (st_object_is_symbol (object))
108 string = st_strdup_printf ("#%s", (char *) st_byte_array_bytes (object));
110 // Character
111 else if (st_object_class (object) == st_character_class) {
112 char outbuf[6] = { 0 };
113 st_unichar_to_utf8 (st_character_value (object), outbuf);
114 string = st_strdup_printf ("$%s", outbuf);
116 // Other
117 } else
118 string = st_strdup_printf ("%s", class_name);
120 return string;
123 int st_current_hash = 1;
125 static void
126 st_object_initialize_header (st_oop object, st_oop class)
128 ST_HEADER (object)->mark = 0 | ST_MARK_TAG;
129 ST_HEADER (object)->hash = st_smi_new (st_current_hash++);
130 ST_HEADER (object)->class = class;
131 st_object_set_format (object, st_smi_value (ST_BEHAVIOR (class)->format));
133 st_assert (st_object_format (object) == (st_smi_value (ST_BEHAVIOR (class)->format)));
136 static void
137 st_object_initialize_body (st_oop object, st_smi instance_size)
139 st_oop *fields;
141 fields = ST_HEADER (object)->fields;
143 for (st_smi i = 0; i < instance_size; i++)
144 fields[i] = st_nil;
147 static st_oop
148 allocate (st_space *space, st_oop class)
150 st_smi instance_size;
151 st_oop object;
153 instance_size = st_smi_value (ST_BEHAVIOR (class)->instance_size);
154 object = st_space_allocate_object (space, class, ST_SIZE_OOPS (struct st_header) + instance_size);
156 st_object_initialize_body (object, instance_size);
160 return object;
163 static st_oop
164 object_copy (st_oop object)
166 st_oop class;
167 st_oop copy;
168 st_smi instance_size;
170 class = ST_HEADER (object)->class;
171 instance_size = st_smi_value (ST_BEHAVIOR (class)->instance_size);
172 copy = st_object_new (memory->moving_space, class);
174 st_oops_copy (ST_HEADER (copy)->fields,
175 ST_HEADER (object)->fields,
176 instance_size);
178 return copy;
181 static st_uint
182 object_size (st_oop object)
184 return (sizeof (struct st_header) / sizeof (st_oop)) + st_smi_value (ST_BEHAVIOR (ST_HEADER (object)->class)->instance_size);
187 static void
188 object_contents (st_oop object, struct contents *contents)
190 contents->oops = ST_HEADER (object)->fields;
191 contents->size = st_smi_value (ST_BEHAVIOR (ST_HEADER (object)->class)->instance_size);
194 st_descriptor *
195 st_object_descriptor (void)
197 static st_descriptor __descriptor =
198 { .allocate = allocate,
199 .allocate_arrayed = NULL,
200 .copy = object_copy,
201 .size = object_size,
202 .contents = object_contents,
205 return & __descriptor;