* st/CompiledMethod.st: Added methods for querying method
[panda.git] / src / st-heap-object.c
blob6bdf4b455536b9c7c6fd5c775d77e488fbc0c860
1 /*
2 * st-heap-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-heap-object.h"
26 #include "st-small-integer.h"
27 #include "st-byte-array.h"
28 #include "st-object.h"
29 #include "st-descriptor.h"
30 #include "st-object-memory.h"
33 #define HEADER(object) (ST_POINTER (object)->header)
35 int st_current_hash = 1;
37 st_uint
38 st_heap_object_hash (st_oop object)
40 return (st_uint) ST_POINTER (object)->hash;
43 void
44 st_heap_object_set_hash (st_oop object, int value)
46 ST_POINTER (object)->hash = (st_smi) value;
49 st_uint
50 st_heap_object_format (st_oop object)
52 return (HEADER (object) >> st_format_shift) & st_format_mask;
55 void
56 st_heap_object_set_format (st_oop object, st_uint format)
58 HEADER (object) = (HEADER (object) & ~st_format_mask_in_place) | (format << st_format_shift);
61 bool
62 st_heap_object_marked (st_oop object)
64 return (HEADER (object) >> st_mark_shift) & st_mark_mask;
67 void
68 st_heap_object_set_marked (st_oop object, bool mark)
70 HEADER (object) = (HEADER (object) & ~st_mark_mask_in_place) | ((mark ? 1 : 0) << st_mark_shift);
73 void
74 st_heap_object_initialize_header (st_oop object, st_oop class)
76 /* header */
77 HEADER (object) = 0 | ST_MARK_TAG;
78 st_heap_object_set_format (object, st_smi_value (ST_BEHAVIOR (class)->format));
79 st_heap_object_set_marked (object, false);
80 st_heap_object_set_hash (object, st_current_hash++);
81 st_heap_object_class (object) = class;
83 st_assert (st_heap_object_format (object) == (st_smi_value (ST_BEHAVIOR (class)->format)));
86 void
87 st_heap_object_initialize_body (st_oop object, st_smi instance_size)
89 st_oop *instvars = st_heap_object_body (object);
91 for (st_smi i = 0; i < instance_size; i++)
92 instvars[i] = st_nil;
95 void
96 st_object_verify (st_oop object)
98 st_assert (st_heap_object_class (object) != st_nil);
99 st_assert (st_heap_object_hash (object) != 0);
102 void
103 st_heap_object_install_forwarding_pointer (st_oop object, st_oop pointer)
105 ST_POINTER (object)->hash = pointer;
108 st_oop
109 st_heap_object_forwarding_pointer (st_oop object)
111 return ST_POINTER (object)->hash;
114 static st_oop
115 allocate (st_space *space, st_oop class)
117 st_smi instance_size;
118 st_oop object;
120 instance_size = st_smi_value (ST_BEHAVIOR (class)->instance_size);
121 object = st_space_allocate_object (space, class, ST_TYPE_SIZE (struct st_header) + instance_size);
123 st_heap_object_initialize_body (object, instance_size);
125 return object;
128 static st_oop
129 object_copy (st_oop object)
131 st_oop class;
132 st_oop copy;
133 st_smi instance_size;
135 class = st_heap_object_class (object);
136 instance_size = st_smi_value (ST_BEHAVIOR (class)->instance_size);
137 copy = st_object_new (om->moving_space, class);
139 st_oops_copy (st_heap_object_body (copy),
140 st_heap_object_body (object),
141 instance_size);
143 return copy;
146 static st_uint
147 object_size (st_oop object)
149 return (sizeof (struct st_header) / sizeof (st_oop)) + st_smi_value (ST_BEHAVIOR (st_heap_object_class (object))->instance_size);
152 static void
153 object_contents (st_oop object, struct contents *contents)
155 contents->oops = st_heap_object_body (object);
156 contents->size = st_smi_value (ST_BEHAVIOR (st_heap_object_class (object))->instance_size);
159 st_descriptor *
160 st_heap_object_descriptor (void)
162 static st_descriptor __descriptor =
163 { .allocate = allocate,
164 .allocate_arrayed = NULL,
165 .copy = object_copy,
166 .size = object_size,
167 .contents = object_contents,
170 return & __descriptor;