Release notes and some test programs.
[ocaml-ancient.git] / ancient.mli
1 (** Mark objects as 'ancient' so they are taken out of the OCaml heap.
2   * $Id: ancient.mli,v 1.5 2006-10-06 15:03:47 rich Exp $
3   *)
4
5 type 'a ancient
6
7 val mark : 'a -> 'a ancient
8   (** [mark obj] copies [obj] and all objects referenced
9     * by [obj] out of the OCaml heap.  It returns the proxy
10     * for [obj].
11     *
12     * The copy of [obj] accessed through the proxy MUST NOT be mutated.
13     *
14     * If [obj] represents a large object, then it is a good
15     * idea to call {!Gc.compact} after marking to recover the
16     * OCaml heap memory.
17     *)
18
19 val follow : 'a ancient -> 'a
20   (** Follow proxy link to out of heap object.
21     *
22     * @raise [Invalid_argument "deleted"] if the object has been deleted.
23     *)
24
25 val delete : 'a ancient -> unit
26   (** [delete obj] deletes ancient object [obj].
27     *
28     * @raise [Invalid_argument "deleted"] if the object has been deleted.
29     *
30     * Forgetting to delete an ancient object results in a memory leak.
31     *)
32
33 (** {6 Shared memory mappings} *)
34
35 type md
36   (** Memory descriptor handle. *)
37
38 val attach : Unix.file_descr -> nativeint -> md
39   (** [attach fd baseaddr] attaches to a new or existing file
40     * which may contain shared objects.
41     *
42     * Initially [fd] should be a read/writable, zero-length file
43     * (for example you could create this using {!Unix.openfile} and
44     * passing the flags [O_RDWR], [O_TRUNC], [O_CREAT]).
45     * One or more objects can then be shared in this file
46     * using {!Unix.share}.
47     *
48     * For new files, [baseaddr] specifies the virtual address to
49     * map the file.  Specifying [Nativeint.zero] ([0n]) here lets [mmap(2)]
50     * choose this, but on some platforms (notably Linux/AMD64)
51     * [mmap] chooses very unwisely, tending to map the memory
52     * just before [libc] with hardly any headroom to grow.  If
53     * you encounter this sort of problem (usually a segfault or
54     * illegal instruction inside libc), then look at [/proc/PID/maps]
55     * and choose a more suitable address.
56     *
57     * If the file was created previously, then the [baseaddr] is
58     * ignored.  The underlying [mmalloc] library will map the
59     * file in at the same place as before.
60     *)
61
62 val detach : md -> unit
63   (** [detach md] detaches from an existing file, and closes it.
64     *)
65
66 val share : md -> int -> 'a -> 'a ancient
67   (** [share md key obj] does the same as {!Ancient.mark} except
68     * that instead of copying the object into local memory, it
69     * writes it into memory which is backed by the attached file.
70     *
71     * Shared mappings created this way may be shared between
72     * other OCaml processes which can access the underlying
73     * file.  See {!Ancient.attach}, {!Ancient.detach}.
74     *
75     * More than one object can be stored in a file.  They are
76     * indexed using integers in the range [0..1023] (the limit
77     * is hard-coded in [mmalloc/mmprivate.h]).  The [key] parameter
78     * controls which object is written/overwritten by [share].
79     * If you do not wish to use this feature, just pass [0]
80     * as the key.
81     *
82     * Do not call {!Ancient.delete} on a mapping created like this.
83     * Instead, call {!Ancient.detach} and, if necessary, delete the
84     * underlying file.
85     *
86     * Caution when sharing files/objects between processes:
87     * The underlying [mmalloc] library does not do any sort of
88     * locking, so all calls to [share] must ensure that they have
89     * exclusive access to the underlying file while in progress.
90     * (Other processes should not even call {!Ancient.get} while
91     * this is happening, but it seems safe to be just reading an
92     * ancient object from the file).
93     *)
94
95 val get : md -> int -> 'a ancient
96   (** [get md key] returns the object indexed by [key] in the
97     * attached file.
98     *
99     * The key is in the range [0..1023] (the limit is hard-coded in
100     * [mmalloc/mmprivate.h]).  If you do not wish to use this feature,
101     * just pass [0] as the key when sharing / getting.
102     *
103     * You need to annotate the returned object with the correct
104     * type.  As with the Marshal module, there is no type checking,
105     * and setting the wrong type will likely cause a segfault
106     * or undefined behaviour.  Note that the returned object has
107     * type [sometype ancient], not just [sometype].
108     *
109     * @raises [Not_found] if no object is associated with the key.
110     *)