1 /* Mark objects as 'ancient' so they are taken out of the OCaml heap.
2 * $Id: ancient_c.c,v 1.2 2006-09-27 14:05:07 rich Exp $
8 #include <caml/config.h>
9 #include <caml/memory.h>
10 #include <caml/alloc.h>
11 #include <caml/mlvalues.h>
12 #include <caml/fail.h>
14 // From byterun/misc.h:
17 // From byterun/minor_gc.c:
18 CAMLextern char *caml_young_start;
19 CAMLextern char *caml_young_end;
20 #define Is_young(val) \
21 (assert (Is_block (val)), \
22 (addr)(val) < (addr)caml_young_end && (addr)(val) > (addr)caml_young_start)
24 // From byterun/major_gc.h:
26 typedef int page_table_entry;
28 typedef char page_table_entry;
30 CAMLextern char *caml_heap_start;
31 CAMLextern char *caml_heap_end;
32 CAMLextern page_table_entry *caml_page_table;
33 extern asize_t caml_page_low, caml_page_high;
37 #define Page(p) ((uintnat) (p) >> Page_log)
38 #define Is_in_heap(p) \
39 (assert (Is_block ((value) (p))), \
40 (addr)(p) >= (addr)caml_heap_start && (addr)(p) < (addr)caml_heap_end \
41 && caml_page_table [Page (p)])
43 // Area is an expandable buffer, allocated on the C heap.
45 void *ptr; // Start of area.
46 size_t n; // Current position.
47 size_t size; // Allocated size.
59 area_append (area *a, const void *obj, size_t size)
61 while (a->n + size > a->size) {
62 if (a->size == 0) a->size = 256; else a->size <<= 1;
63 a->ptr = realloc (a->ptr, a->size);
64 if (a->ptr == 0) return -1; // Out of memory.
66 memcpy (a->ptr + a->n, obj, size);
74 if (a->n != a->size) {
76 a->ptr = realloc (a->ptr, a->size);
77 assert (a->ptr); // Getting smaller, so shouldn't really fail.
94 // When a block is visited, we overwrite the header with all 1's.
95 // This is not quite an impossible value - one could imagine an
96 // enormous custom block where the header could take on this
98 static header_t visited = (unsigned long) -1;
100 // The general plan here:
102 // 1. Starting at [obj], copy it to our out-of-heap memory area
104 // 2. Recursively visit subnodes of [obj] and do the same.
105 // 3. As we copy each object, we avoid circularity by setting that
106 // object's header to a special 'visited' value. However since these
107 // are objects in the Caml heap we have to restore the original
108 // headers at the end, which is the purpose of the [restore] area.
109 // 4. We use realloc to allocate the memory for the copy, but because
110 // the memory can move around, we cannot store absolute pointers.
111 // Instead we store offsets and fix them up later. This is the
112 // purpose of the [fixups] area.
114 // XXX Large, deeply recursive structures cause a stack overflow.
115 // Temporary solution: 'ulimit -s unlimited'. This function should
116 // be replaced with something iterative.
118 mark (value obj, area *ptr, area *restore, area *fixups)
120 char *header = Hp_val (obj);
121 assert (Wosize_hp (header) > 0); // Always true? (XXX)
123 // We can't handle out-of-heap objects.
124 // XXX Since someone might try to mark an ancient object, they
125 // might get this error, so we should try to do better here.
126 assert (Is_young (obj) || Is_in_heap (obj));
128 // If we've already visited this object, just return its offset
129 // in the out-of-heap memory.
130 if (memcmp (header, &visited, sizeof visited) == 0)
131 return (Long_val (Field (obj, 0)));
133 // Offset where we will store this object in the out-of-heap memory.
134 size_t offset = ptr->n;
136 // Copy the object out of the OCaml heap.
137 size_t bytes = Bhsize_hp (header);
138 if (area_append (ptr, header, bytes) == -1)
139 return -1; // Error out of memory.
141 // Scan the fields looking for pointers to blocks.
142 int can_scan = Tag_val (obj) < No_scan_tag;
144 mlsize_t nr_words = Wosize_hp (header);
147 for (i = 0; i < nr_words; ++i) {
148 value field = Field (obj, i);
150 if (Is_block (field) &&
151 (Is_young (field) || Is_in_heap (field))) {
152 size_t field_offset = mark (field, ptr, restore, fixups);
153 if (field_offset == -1) return -1; // Propagate out of memory errors.
155 // Since the recursive call to mark above can reallocate the
156 // area, we need to recompute these each time round the loop.
157 char *obj_copy_header = ptr->ptr + offset;
158 value obj_copy = Val_hp (obj_copy_header);
160 // Don't store absolute pointers yet because realloc will
161 // move the memory around. Store a fake pointer instead.
162 // We'll fix up these fake pointers afterwards.
163 Field (obj_copy, i) = field_offset + sizeof (header_t);
165 size_t fixup = (void *)&Field(obj_copy, i) - ptr->ptr;
166 area_append (fixups, &fixup, sizeof fixup);
171 // Mark this object as having been "visited", but keep track of
172 // what was there before so it can be restored. We also need to
173 // record the offset.
175 // (1) What was in the header before is kept in the out-of-heap
176 // copy, so we don't explicitly need to remember that.
177 // (2) We can keep the offset in the zeroth field, but since
178 // the code above might have modified the copy, we need to remember
179 // what was in that field before.
180 // (3) We can overwrite the header with all 1's to indicate that
181 // we've visited (but see notes on 'static header_t visited' above).
182 // (4) All objects in OCaml are at least one word long (we hope!).
183 struct restore_item restore_item;
184 restore_item.header = header;
185 restore_item.field_zero = Field (obj, 0);
186 area_append (restore, &restore_item, sizeof restore_item);
188 memcpy (header, (void *)&visited, sizeof visited);
189 Field (obj, 0) = Val_long (offset);
194 // See comments immediately above.
196 do_restore (area *ptr, area *restore)
199 for (i = 0; i < restore->n; i += sizeof (struct restore_item))
201 struct restore_item *restore_item =
202 (struct restore_item *)(restore->ptr + i);
203 assert (memcmp (restore_item->header, &visited, sizeof visited) == 0);
205 value obj = Val_hp (restore_item->header);
206 size_t offset = Long_val (Field (obj, 0));
208 char *obj_copy_header = ptr->ptr + offset;
209 //value obj_copy = Val_hp (obj_copy_header);
211 // Restore the original header.
212 memcpy (restore_item->header, obj_copy_header, sizeof visited);
214 // Restore the original zeroth field.
215 Field (obj, 0) = restore_item->field_zero;
219 // Fixup fake pointers.
221 do_fixups (area *ptr, area *fixups)
225 for (i = 0; i < fixups->n; i += sizeof (size_t))
227 size_t fixup = *(size_t *)(fixups->ptr + i);
228 size_t offset = *(size_t *)(ptr->ptr + fixup);
229 void *real_ptr = ptr->ptr + offset;
230 *(value *)(ptr->ptr + fixup) = (value) real_ptr;
235 ancient_mark (value obj)
240 area ptr; // This will be the out of heap area.
242 area restore; // Headers to be fixed up after.
243 area_init (&restore);
244 area fixups; // List of fake pointers to be fixed up.
247 if (mark (obj, &ptr, &restore, &fixups) == -1) {
248 // Ran out of memory. Recover and throw an exception.
249 do_restore (&ptr, &restore);
251 area_free (&restore);
253 caml_failwith ("out of memory");
257 // Restore Caml heap structures.
258 do_restore (&ptr, &restore);
259 area_free (&restore);
261 // Update all fake pointers in the out of heap area to make them real
263 do_fixups (&ptr, &fixups);
266 // Replace obj with a proxy.
267 proxy = caml_alloc (1, Abstract_tag);
268 Field (proxy, 0) = (value) ptr.ptr;
274 ancient_follow (value obj)
280 if (Is_long (v)) caml_invalid_argument ("deleted");
281 v = Val_hp (v); // v points to the header; make it point to the object.
287 ancient_delete (value obj)
293 if (Is_long (v)) caml_invalid_argument ("deleted");
295 // Otherwise v is a pointer to the out of heap malloc'd object.
296 assert (!Is_young (v) && !Is_in_heap (v));
299 // Replace the proxy (a pointer) with an int 0 so we know it's
300 // been deleted in future.
301 Field (obj, 0) = Val_long (0);
303 CAMLreturn (Val_unit);