Characters are now implemented as tagged pointers
[panda.git] / goo / st-heap-object.c
blobf0a9001edd30c4ed0c83a340771556b0185975a2
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"
32 #define HEADER(object) (ST_POINTER (object)->header)
34 int st_current_hash = 0;
36 guint
37 st_heap_object_hash (st_oop object)
39 return (guint) ST_POINTER (object)->hash;
42 void
43 st_heap_object_set_hash (st_oop object, int value)
45 ST_POINTER (object)->hash = (st_smi) value;
48 void
49 st_heap_object_set_format (st_oop object, guint format)
51 HEADER (object) = (HEADER (object) & ~st_format_mask_in_place) | (format << st_format_shift);
54 bool
55 st_heap_object_readonly (st_oop object)
57 return (HEADER (object) >> st_readonly_shift) & st_readonly_mask;
60 void
61 st_heap_object_set_readonly (st_oop object, bool readonly)
63 HEADER (object) = (HEADER (object) & ~st_readonly_mask_in_place) | ((readonly ? 1 : 0) << st_readonly_shift);
66 bool
67 st_heap_object_nonpointer (st_oop object)
69 return (HEADER (object) >> st_nonpointer_shift) & st_nonpointer_mask;
72 void
73 st_heap_object_set_nonpointer (st_oop object, bool nonpointer)
75 HEADER (object) = (HEADER (object) & ~st_nonpointer_mask_in_place) | ((nonpointer ? 1 : 0) << st_nonpointer_shift);
78 void
79 st_heap_object_initialize_header (st_oop object, st_oop klass)
81 /* header */
82 st_heap_object_set_format (object, st_smi_value (st_behavior_format (klass)));
83 st_heap_object_set_readonly (object, false);
84 st_heap_object_set_nonpointer (object, false);
86 /* hash */
87 st_heap_object_set_hash (object, st_current_hash++);
89 /* klass */
90 st_heap_object_class (object) = klass;
93 void
94 st_heap_object_initialize_body (st_oop object, st_smi instance_size)
96 st_oop *instvars = st_heap_object_body (object);
98 for (st_smi i = 0; i < instance_size; i++)
99 instvars[i] = st_nil;
103 static st_oop
104 allocate (st_oop klass)
106 st_smi instance_size = st_smi_value (st_behavior_instance_size (klass));
108 st_oop object = st_allocate_object (ST_TYPE_SIZE (STHeader) + instance_size);
110 st_heap_object_initialize_header (object, klass);
111 st_heap_object_initialize_body (object, instance_size);
113 return object;
116 const STDescriptor *
117 st_heap_object_descriptor (void)
119 static const STDescriptor __descriptor =
120 { .allocate = allocate,
121 .allocate_arrayed = NULL,
124 return & __descriptor;