Removed the notion of a "large" context. For simplicity, all contexts
[panda.git] / src / st-object.c
blobd5ba5fd55d375500797a6e1ba2c639feb93df31a
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-array.h"
33 #include "st-symbol.h"
34 #include "st-object.h"
35 #include "st-character.h"
36 #include "st-handle.h"
37 #include "st-unicode.h"
39 void
40 st_object_initialize_header (st_oop object, st_oop class)
42 ST_OBJECT_MARK (object) = 0 | ST_MARK_TAG;
43 ST_OBJECT_CLASS (object) = class;
44 st_object_set_format (object, st_smi_value (ST_BEHAVIOR_FORMAT (class)));
45 st_object_set_instance_size (object, st_smi_value (ST_BEHAVIOR_INSTANCE_SIZE (class)));
48 bool
49 st_object_equal (st_oop object, st_oop other)
51 if (st_object_class (object) == ST_SMI_CLASS)
52 return st_smi_equal (object, other);
54 if (st_object_class (object) == ST_CHARACTER_CLASS)
55 return st_character_equal (object, other);
57 if (ST_OBJECT_CLASS (object) == ST_FLOAT_CLASS)
58 return st_float_equal (object, other);
60 if (ST_OBJECT_CLASS (object) == ST_ASSOCIATION_CLASS)
61 return st_association_equal (object, other);
63 if (ST_OBJECT_CLASS (object) == ST_SYMBOL_CLASS)
64 return st_symbol_equal (object, other);
66 if (ST_OBJECT_CLASS (object) == ST_BYTE_ARRAY_CLASS ||
67 ST_OBJECT_CLASS (object) == ST_STRING_CLASS)
68 return st_byte_array_equal (object, other);
70 return object == other;
73 st_uint
74 st_object_hash (st_oop object)
76 if (st_object_class (object) == ST_SMI_CLASS)
77 return st_smi_hash (object);
79 if (st_object_class (object) == ST_BYTE_ARRAY_CLASS ||
80 st_object_class (object) == ST_STRING_CLASS ||
81 st_object_class (object) == ST_SYMBOL_CLASS)
82 return st_byte_array_hash (object);
84 if (st_object_class (object) == ST_FLOAT_CLASS)
85 return st_float_hash (object);
87 if (st_object_class (object) == ST_CHARACTER_CLASS)
88 return st_character_hash (object);
90 if (st_object_class (object) == ST_ASSOCIATION_CLASS)
91 return st_association_hash (object);
93 return object >> 2;
96 st_oop
97 st_object_allocate (st_oop class)
99 st_oop *fields;
100 st_uint size, instance_size;
101 st_oop object;
103 instance_size = st_smi_value (ST_BEHAVIOR_INSTANCE_SIZE (class));
104 size = ST_SIZE_OOPS (struct st_header) + instance_size;
105 object = st_memory_allocate (size);
106 if (object == 0) {
107 st_memory_perform_gc ();
108 class = st_memory_remap_reference (class);
109 object = st_memory_allocate (size);
110 st_assert (object != 0);
113 st_object_initialize_header (object, class);
115 fields = ST_OBJECT_FIELDS (object);
116 for (st_uint i = 0; i < instance_size; i++)
117 fields[i] = ST_NIL;
119 return object;
122 st_oop
123 st_handle_allocate (st_oop class)
125 st_oop *fields;
126 st_oop object;
127 st_uint size;
129 size = ST_SIZE_OOPS (struct st_handle);
130 object = st_memory_allocate (size);
131 if (object == 0) {
132 st_memory_perform_gc ();
133 class = st_memory_remap_reference (class);
134 object = st_memory_allocate (size);
135 st_assert (object != 0);
137 st_object_initialize_header (object, class);
139 return object;