1 /* Mark objects as 'ancient' so they are taken out of the OCaml heap.
2 * $Id: ancient_c.c,v 1.3 2006-09-27 15:36:18 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;
35 extern asize_t caml_page_low, caml_page_high;
39 #define Page(p) ((uintnat) (p) >> Page_log)
40 #define Is_in_heap(p) \
41 (assert (Is_block ((value) (p))), \
42 (addr)(p) >= (addr)caml_heap_start && (addr)(p) < (addr)caml_heap_end \
43 && caml_page_table [Page (p)])
45 // Area is an expandable buffer, allocated on the C heap.
47 void *ptr; // Start of area.
48 size_t n; // Current position.
49 size_t size; // Allocated size.
51 // If this area requires custom realloc function, these will be non-null.
52 void *(*realloc)(void *data, void *ptr, size_t size);
53 void (*free)(void *data, void *ptr);
69 area_init_custom (area *a,
70 void *(*realloc)(void *data, void *ptr, size_t size),
71 void (*free)(void *data, void *ptr),
81 area_append (area *a, const void *obj, size_t size)
83 while (a->n + size > a->size) {
84 if (a->size == 0) a->size = 256; else a->size <<= 1;
87 ? a->realloc (a->data, a->ptr, a->size)
88 : realloc (a->ptr, a->size);
89 if (a->ptr == 0) return -1; // Out of memory.
91 memcpy (a->ptr + a->n, obj, size);
99 if (a->n != a->size) {
103 ? a->realloc (a->data, a->ptr, a->size)
104 : realloc (a->ptr, a->size);
105 assert (a->ptr); // Getting smaller, so shouldn't really fail.
112 if (a->free) a->free (a->data, a->ptr);
118 struct restore_item {
123 // When a block is visited, we overwrite the header with all 1's.
124 // This is not quite an impossible value - one could imagine an
125 // enormous custom block where the header could take on this
127 static header_t visited = (unsigned long) -1;
129 // The general plan here:
131 // 1. Starting at [obj], copy it to our out-of-heap memory area
133 // 2. Recursively visit subnodes of [obj] and do the same.
134 // 3. As we copy each object, we avoid circularity by setting that
135 // object's header to a special 'visited' value. However since these
136 // are objects in the Caml heap we have to restore the original
137 // headers at the end, which is the purpose of the [restore] area.
138 // 4. We use realloc to allocate the memory for the copy, but because
139 // the memory can move around, we cannot store absolute pointers.
140 // Instead we store offsets and fix them up later. This is the
141 // purpose of the [fixups] area.
143 // XXX Large, deeply recursive structures cause a stack overflow.
144 // Temporary solution: 'ulimit -s unlimited'. This function should
145 // be replaced with something iterative.
147 mark (value obj, area *ptr, area *restore, area *fixups)
149 char *header = Hp_val (obj);
150 assert (Wosize_hp (header) > 0); // Always true? (XXX)
152 // XXX This assertion might fail if someone tries to mark an object
153 // which is already ancient.
154 assert (Is_young (obj) || Is_in_heap (obj));
156 // If we've already visited this object, just return its offset
157 // in the out-of-heap memory.
158 if (memcmp (header, &visited, sizeof visited) == 0)
159 return (Long_val (Field (obj, 0)));
161 // Offset where we will store this object in the out-of-heap memory.
162 size_t offset = ptr->n;
164 // Copy the object out of the OCaml heap.
165 size_t bytes = Bhsize_hp (header);
166 if (area_append (ptr, header, bytes) == -1)
167 return -1; // Error out of memory.
169 // Scan the fields looking for pointers to blocks.
170 int can_scan = Tag_val (obj) < No_scan_tag;
172 mlsize_t nr_words = Wosize_hp (header);
175 for (i = 0; i < nr_words; ++i) {
176 value field = Field (obj, i);
178 if (Is_block (field) &&
179 (Is_young (field) || Is_in_heap (field))) {
180 size_t field_offset = mark (field, ptr, restore, fixups);
181 if (field_offset == -1) return -1; // Propagate out of memory errors.
183 // Since the recursive call to mark above can reallocate the
184 // area, we need to recompute these each time round the loop.
185 char *obj_copy_header = ptr->ptr + offset;
186 value obj_copy = Val_hp (obj_copy_header);
188 // Don't store absolute pointers yet because realloc will
189 // move the memory around. Store a fake pointer instead.
190 // We'll fix up these fake pointers afterwards.
191 Field (obj_copy, i) = field_offset + sizeof (header_t);
193 size_t fixup = (void *)&Field(obj_copy, i) - ptr->ptr;
194 area_append (fixups, &fixup, sizeof fixup);
199 // Mark this object as having been "visited", but keep track of
200 // what was there before so it can be restored. We also need to
201 // record the offset.
203 // (1) What was in the header before is kept in the out-of-heap
204 // copy, so we don't explicitly need to remember that.
205 // (2) We can keep the offset in the zeroth field, but since
206 // the code above might have modified the copy, we need to remember
207 // what was in that field before.
208 // (3) We can overwrite the header with all 1's to indicate that
209 // we've visited (but see notes on 'static header_t visited' above).
210 // (4) All objects in OCaml are at least one word long (we hope!).
211 struct restore_item restore_item;
212 restore_item.header = header;
213 restore_item.field_zero = Field (obj, 0);
214 area_append (restore, &restore_item, sizeof restore_item);
216 memcpy (header, (void *)&visited, sizeof visited);
217 Field (obj, 0) = Val_long (offset);
222 // See comments immediately above.
224 do_restore (area *ptr, area *restore)
227 for (i = 0; i < restore->n; i += sizeof (struct restore_item))
229 struct restore_item *restore_item =
230 (struct restore_item *)(restore->ptr + i);
231 assert (memcmp (restore_item->header, &visited, sizeof visited) == 0);
233 value obj = Val_hp (restore_item->header);
234 size_t offset = Long_val (Field (obj, 0));
236 char *obj_copy_header = ptr->ptr + offset;
237 //value obj_copy = Val_hp (obj_copy_header);
239 // Restore the original header.
240 memcpy (restore_item->header, obj_copy_header, sizeof visited);
242 // Restore the original zeroth field.
243 Field (obj, 0) = restore_item->field_zero;
247 // Fixup fake pointers.
249 do_fixups (area *ptr, area *fixups)
253 for (i = 0; i < fixups->n; i += sizeof (size_t))
255 size_t fixup = *(size_t *)(fixups->ptr + i);
256 size_t offset = *(size_t *)(ptr->ptr + fixup);
257 void *real_ptr = ptr->ptr + offset;
258 *(value *)(ptr->ptr + fixup) = (value) real_ptr;
262 static CAMLprim value
264 void *(*realloc)(void *data, void *ptr, size_t size),
265 void (*free)(void *data, void *ptr),
271 area ptr; // This will be the out of heap area.
272 area_init_custom (&ptr, realloc, free, data);
273 area restore; // Headers to be fixed up after.
274 area_init (&restore);
275 area fixups; // List of fake pointers to be fixed up.
278 if (mark (obj, &ptr, &restore, &fixups) == -1) {
279 // Ran out of memory. Recover and throw an exception.
281 do_restore (&ptr, &restore);
282 area_free (&restore);
284 caml_failwith ("out of memory");
288 // Restore Caml heap structures.
289 do_restore (&ptr, &restore);
290 area_free (&restore);
292 // Update all fake pointers in the out of heap area to make them real
294 do_fixups (&ptr, &fixups);
297 // Replace obj with a proxy.
298 proxy = caml_alloc (1, Abstract_tag);
299 Field (proxy, 0) = (value) ptr.ptr;
305 my_realloc (void *data __attribute__((unused)), void *ptr, size_t size)
307 return realloc (ptr, size);
311 my_free (void *data __attribute__((unused)), void *ptr)
317 ancient_mark (value obj)
322 proxy = do_mark (obj, my_realloc, my_free, 0);
328 ancient_follow (value obj)
334 if (Is_long (v)) caml_invalid_argument ("deleted");
335 v = Val_hp (v); // v points to the header; make it point to the object.
341 ancient_delete (value obj)
347 if (Is_long (v)) caml_invalid_argument ("deleted");
349 // Otherwise v is a pointer to the out of heap malloc'd object.
350 assert (!Is_young (v) && !Is_in_heap (v));
353 // Replace the proxy (a pointer) with an int 0 so we know it's
354 // been deleted in future.
355 Field (obj, 0) = Val_long (0);
357 CAMLreturn (Val_unit);
361 ancient_share (value fdv, value obj)
363 CAMLparam2 (fdv, obj);
366 int fd = Int_val (fd);
367 void *md = mmalloc_attach (fd, 0);
369 perror ("mmalloc_attach");
370 caml_failwith ("mmalloc_attach");
373 proxy = do_mark (obj, mrealloc, mfree, md);
375 // Save the address of the object within the mmalloc area. We need
376 // it when attaching.
377 mmalloc_setkey (md, 0, (void *) Field (proxy, 0));
383 ancient_attach (value fdv)
388 int fd = Int_val (fd);
389 void *md = mmalloc_attach (fd, 0);
391 perror ("mmalloc_attach");
392 caml_failwith ("mmalloc_attach");
395 proxy = caml_alloc (2, Abstract_tag);
396 Field (proxy, 0) = (value) mmalloc_getkey (md, 0);
397 Field (proxy, 1) = (value) md;
403 ancient_detach (value obj)
408 int size = Wosize_val (obj);
409 if (size < 2) caml_failwith ("Ancient.detach: not an attached object");
412 if (Is_long (v)) caml_invalid_argument ("detached");
414 void *md = (void *) Field (obj, 1);
415 if (mmalloc_detach (md) != 0) {
416 perror ("mmalloc_detach");
417 caml_failwith ("mmalloc_detach");
420 // Replace the proxy (a pointer) with an int 0 so we know it's
421 // been detached in future.
422 Field (obj, 0) = Val_long (0);
424 CAMLreturn (Val_unit);