1 /* Mark objects as 'ancient' so they are taken out of the OCaml heap.
2 * $Id: ancient_c.c,v 1.10 2006-10-13 12:28:20 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 #include "mmalloc/mmalloc.h"
16 // From byterun/misc.h:
19 // From byterun/minor_gc.c:
20 CAMLextern char *caml_young_start;
21 CAMLextern char *caml_young_end;
22 #define Is_young(val) \
23 (assert (Is_block (val)), \
24 (addr)(val) < (addr)caml_young_end && (addr)(val) > (addr)caml_young_start)
26 // From byterun/major_gc.h:
28 typedef int page_table_entry;
30 typedef char page_table_entry;
32 CAMLextern char *caml_heap_start;
33 CAMLextern char *caml_heap_end;
34 CAMLextern page_table_entry *caml_page_table;
38 #define Page(p) ((uintnat) (p) >> Page_log)
39 #define Is_in_heap(p) \
40 (assert (Is_block ((value) (p))), \
41 (addr)(p) >= (addr)caml_heap_start && (addr)(p) < (addr)caml_heap_end \
42 && caml_page_table [Page (p)])
44 // Area is an expandable buffer, allocated on the C heap.
46 void *ptr; // Start of area.
47 size_t n; // Current position.
48 size_t size; // Allocated size.
50 // If this area requires custom realloc function, these will be non-null.
51 void *(*realloc)(void *data, void *ptr, size_t size);
52 void (*free)(void *data, void *ptr);
68 area_init_custom (area *a,
69 void *(*realloc)(void *data, void *ptr, size_t size),
70 void (*free)(void *data, void *ptr),
80 area_append (area *a, const void *obj, size_t size)
82 while (a->n + size > a->size) {
83 if (a->size == 0) a->size = 256; else a->size <<= 1;
86 ? a->realloc (a->data, a->ptr, a->size)
87 : realloc (a->ptr, a->size);
88 if (a->ptr == 0) return -1; // Out of memory.
90 memcpy (a->ptr + a->n, obj, size);
98 if (a->n != a->size) {
102 ? a->realloc (a->data, a->ptr, a->size)
103 : realloc (a->ptr, a->size);
104 assert (a->ptr); // Getting smaller, so shouldn't really fail.
111 if (a->free) a->free (a->data, a->ptr);
117 struct restore_item {
122 // When a block is visited, we overwrite the header with all 1's.
123 // This is not quite an impossible value - one could imagine an
124 // enormous custom block where the header could take on this
126 static header_t visited = (unsigned long) -1;
128 // The general plan here:
130 // 1. Starting at [obj], copy it to our out-of-heap memory area
132 // 2. Recursively visit subnodes of [obj] and do the same.
133 // 3. As we copy each object, we avoid circularity by setting that
134 // object's header to a special 'visited' value. However since these
135 // are objects in the Caml heap we have to restore the original
136 // headers at the end, which is the purpose of the [restore] area.
137 // 4. We use realloc to allocate the memory for the copy, but because
138 // the memory can move around, we cannot store absolute pointers.
139 // Instead we store offsets and fix them up later. This is the
140 // purpose of the [fixups] area.
142 // XXX Large, deeply recursive structures cause a stack overflow.
143 // Temporary solution: 'ulimit -s unlimited'. This function should
144 // be replaced with something iterative.
146 _mark (value obj, area *ptr, area *restore, area *fixups)
148 // XXX This assertion might fail if someone tries to mark an object
149 // which is already ancient.
150 assert (Is_young (obj) || Is_in_heap (obj));
152 char *header = Hp_val (obj);
154 // If we've already visited this object, just return its offset
155 // in the out-of-heap memory.
156 if (memcmp (header, &visited, sizeof visited) == 0)
157 return (Long_val (Field (obj, 0)));
159 // XXX Actually this fails if you try to persist a zero-length
160 // array. Needs to be fixed, but it breaks some rather important
162 assert (Wosize_hp (header) > 0);
164 // Offset where we will store this object in the out-of-heap memory.
165 size_t offset = ptr->n;
167 // Copy the object out of the OCaml heap.
168 size_t bytes = Bhsize_hp (header);
169 if (area_append (ptr, header, bytes) == -1)
170 return -1; // Error out of memory.
172 // Scan the fields looking for pointers to blocks.
173 int can_scan = Tag_val (obj) < No_scan_tag;
175 mlsize_t nr_words = Wosize_hp (header);
178 for (i = 0; i < nr_words; ++i) {
179 value field = Field (obj, i);
181 if (Is_block (field) &&
182 (Is_young (field) || Is_in_heap (field))) {
183 size_t field_offset = _mark (field, ptr, restore, fixups);
184 if (field_offset == -1) return -1; // Propagate out of memory errors.
186 // Since the recursive call to mark above can reallocate the
187 // area, we need to recompute these each time round the loop.
188 char *obj_copy_header = ptr->ptr + offset;
189 value obj_copy = Val_hp (obj_copy_header);
191 // Don't store absolute pointers yet because realloc will
192 // move the memory around. Store a fake pointer instead.
193 // We'll fix up these fake pointers afterwards in do_fixups.
194 Field (obj_copy, i) = field_offset + sizeof (header_t);
196 size_t fixup = (void *)&Field(obj_copy, i) - ptr->ptr;
197 area_append (fixups, &fixup, sizeof fixup);
202 // Mark this object as having been "visited", but keep track of
203 // what was there before so it can be restored. We also need to
204 // record the offset.
206 // (1) What was in the header before is kept in the out-of-heap
207 // copy, so we don't explicitly need to remember that.
208 // (2) We can keep the offset in the zeroth field, but since
209 // the code above might have modified the copy, we need to remember
210 // what was in that field before.
211 // (3) We can overwrite the header with all 1's to indicate that
212 // we've visited (but see notes on 'static header_t visited' above).
213 // (4) All objects in OCaml are at least one word long (XXX - actually
214 // this is not true).
215 struct restore_item restore_item;
216 restore_item.header = header;
217 restore_item.field_zero = Field (obj, 0);
218 area_append (restore, &restore_item, sizeof restore_item);
220 memcpy (header, (void *)&visited, sizeof visited);
221 Field (obj, 0) = Val_long (offset);
226 // See comments immediately above.
228 do_restore (area *ptr, area *restore)
231 for (i = 0; i < restore->n; i += sizeof (struct restore_item))
233 struct restore_item *restore_item =
234 (struct restore_item *)(restore->ptr + i);
235 assert (memcmp (restore_item->header, &visited, sizeof visited) == 0);
237 value obj = Val_hp (restore_item->header);
238 size_t offset = Long_val (Field (obj, 0));
240 char *obj_copy_header = ptr->ptr + offset;
241 //value obj_copy = Val_hp (obj_copy_header);
243 // Restore the original header.
244 memcpy (restore_item->header, obj_copy_header, sizeof visited);
246 // Restore the original zeroth field.
247 Field (obj, 0) = restore_item->field_zero;
251 // Fixup fake pointers.
253 do_fixups (area *ptr, area *fixups)
257 for (i = 0; i < fixups->n; i += sizeof (size_t))
259 size_t fixup = *(size_t *)(fixups->ptr + i);
260 size_t offset = *(size_t *)(ptr->ptr + fixup);
261 void *real_ptr = ptr->ptr + offset;
262 *(value *)(ptr->ptr + fixup) = (value) real_ptr;
268 void *(*realloc)(void *data, void *ptr, size_t size),
269 void (*free)(void *data, void *ptr),
273 area ptr; // This will be the out of heap area.
274 area_init_custom (&ptr, realloc, free, data);
275 area restore; // Headers to be fixed up after.
276 area_init (&restore);
277 area fixups; // List of fake pointers to be fixed up.
280 if (_mark (obj, &ptr, &restore, &fixups) == -1) {
281 // Ran out of memory. Recover and throw an exception.
283 do_restore (&ptr, &restore);
284 area_free (&restore);
286 caml_failwith ("out of memory");
290 // Restore Caml heap structures.
291 do_restore (&ptr, &restore);
292 area_free (&restore);
294 // Update all fake pointers in the out of heap area to make them real
296 do_fixups (&ptr, &fixups);
299 if (r_size) *r_size = ptr.size;
304 my_realloc (void *data __attribute__((unused)), void *ptr, size_t size)
306 return realloc (ptr, size);
310 my_free (void *data __attribute__((unused)), void *ptr)
316 ancient_mark_info (value obj)
319 CAMLlocal3 (proxy, info, rv);
322 void *ptr = mark (obj, my_realloc, my_free, 0, &size);
325 proxy = caml_alloc (1, Abstract_tag);
326 Field (proxy, 0) = (value) ptr;
328 // Make the info struct.
329 info = caml_alloc (1, 0);
330 Field (info, 0) = Val_long (size);
332 rv = caml_alloc (2, 0);
333 Field (rv, 0) = proxy;
334 Field (rv, 1) = info;
340 ancient_follow (value obj)
346 if (Is_long (v)) caml_invalid_argument ("deleted");
347 v = Val_hp (v); // v points to the header; make it point to the object.
353 ancient_delete (value obj)
359 if (Is_long (v)) caml_invalid_argument ("deleted");
361 // Otherwise v is a pointer to the out of heap malloc'd object.
362 assert (!Is_young (v) && !Is_in_heap (v));
365 // Replace the proxy (a pointer) with an int 0 so we know it's
366 // been deleted in future.
367 Field (obj, 0) = Val_long (0);
369 CAMLreturn (Val_unit);
373 ancient_is_ancient (value obj)
378 v = Is_young (obj) || Is_in_heap (obj) ? Val_false : Val_true;
384 ancient_address_of (value obj)
389 if (Is_block (obj)) v = caml_copy_nativeint ((intnat) obj);
390 else v = caml_copy_nativeint (0);
396 ancient_attach (value fdv, value baseaddrv)
398 CAMLparam2 (fdv, baseaddrv);
401 int fd = Int_val (fdv);
402 void *baseaddr = (void *) Nativeint_val (baseaddrv);
403 void *md = mmalloc_attach (fd, baseaddr);
405 perror ("mmalloc_attach");
406 caml_failwith ("mmalloc_attach");
409 mdv = caml_alloc (1, Abstract_tag);
410 Field (mdv, 0) = (value) md;
416 ancient_detach (value mdv)
420 void *md = (void *) Field (mdv, 0);
422 if (mmalloc_detach (md) != 0) {
423 perror ("mmalloc_detach");
424 caml_failwith ("mmalloc_detach");
427 CAMLreturn (Val_unit);
436 ancient_share_info (value mdv, value keyv, value obj)
438 CAMLparam3 (mdv, keyv, obj);
439 CAMLlocal3 (proxy, info, rv);
441 void *md = (void *) Field (mdv, 0);
442 int key = Int_val (keyv);
444 // Get the key table.
445 struct keytable *keytable = mmalloc_getkey (md, 0);
447 keytable = mmalloc (md, sizeof (struct keytable));
448 if (keytable == 0) caml_failwith ("out of memory");
450 keytable->allocated = 0;
451 mmalloc_setkey (md, 0, keytable);
454 // Existing key exists? Free it.
455 if (key < keytable->allocated && keytable->keys[key] != 0) {
456 mfree (md, keytable->keys[key]);
457 keytable->keys[key] = 0;
460 // Keytable large enough? If not, realloc it.
461 if (key >= keytable->allocated) {
462 int allocated = keytable->allocated == 0 ? 32 : keytable->allocated * 2;
463 void **keys = mrealloc (md, keytable->keys, allocated * sizeof (void *));
464 if (keys == 0) caml_failwith ("out of memory");
466 for (i = keytable->allocated; i < allocated; ++i) keys[i] = 0;
467 keytable->keys = keys;
468 keytable->allocated = allocated;
473 void *ptr = mark (obj, mrealloc, mfree, md, &size);
475 // Add the key to the keytable.
476 keytable->keys[key] = ptr;
479 proxy = caml_alloc (1, Abstract_tag);
480 Field (proxy, 0) = (value) ptr;
482 // Make the info struct.
483 info = caml_alloc (1, 0);
484 Field (info, 0) = Val_long (size);
486 rv = caml_alloc (2, 0);
487 Field (rv, 0) = proxy;
488 Field (rv, 1) = info;
494 ancient_get (value mdv, value keyv)
496 CAMLparam2 (mdv, keyv);
499 void *md = (void *) Field (mdv, 0);
500 int key = Int_val (keyv);
503 struct keytable *keytable = mmalloc_getkey (md, 0);
504 if (keytable == 0 || key >= keytable->allocated || keytable->keys[key] == 0)
505 caml_raise_not_found ();
506 void *ptr = keytable->keys[key];
509 proxy = caml_alloc (1, Abstract_tag);
510 Field (proxy, 0) = (value) ptr;