Add several new record features
[ghc-hetmet.git] / rts / Stable.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2002
4  *
5  * Stable names and stable pointers.
6  *
7  * ---------------------------------------------------------------------------*/
8
9 // Make static versions of inline functions in Stable.h:
10 #define RTS_STABLE_C
11
12 #include "PosixSource.h"
13 #include "Rts.h"
14 #include "Hash.h"
15 #include "RtsUtils.h"
16 #include "OSThreads.h"
17 #include "Storage.h"
18 #include "RtsAPI.h"
19 #include "RtsFlags.h"
20 #include "OSThreads.h"
21 #include "Trace.h"
22 #include "Stable.h"
23
24 /* Comment from ADR's implementation in old RTS:
25
26   This files (together with @ghc/runtime/storage/PerformIO.lhc@ and a
27   small change in @HpOverflow.lc@) consists of the changes in the
28   runtime system required to implement "Stable Pointers". But we're
29   getting a bit ahead of ourselves --- what is a stable pointer and what
30   is it used for?
31
32   When Haskell calls C, it normally just passes over primitive integers,
33   floats, bools, strings, etc.  This doesn't cause any problems at all
34   for garbage collection because the act of passing them makes a copy
35   from the heap, stack or wherever they are onto the C-world stack.
36   However, if we were to pass a heap object such as a (Haskell) @String@
37   and a garbage collection occured before we finished using it, we'd run
38   into problems since the heap object might have been moved or even
39   deleted.
40
41   So, if a C call is able to cause a garbage collection or we want to
42   store a pointer to a heap object between C calls, we must be careful
43   when passing heap objects. Our solution is to keep a table of all
44   objects we've given to the C-world and to make sure that the garbage
45   collector collects these objects --- updating the table as required to
46   make sure we can still find the object.
47
48
49   Of course, all this rather begs the question: why would we want to
50   pass a boxed value?
51
52   One very good reason is to preserve laziness across the language
53   interface. Rather than evaluating an integer or a string because it
54   {\em might\/} be required by the C function, we can wait until the C
55   function actually wants the value and then force an evaluation.
56
57   Another very good reason (the motivating reason!) is that the C code
58   might want to execute an object of sort $IO ()$ for the side-effects
59   it will produce. For example, this is used when interfacing to an X
60   widgets library to allow a direct implementation of callbacks.
61
62
63   The @makeStablePointer :: a -> IO (StablePtr a)@ function
64   converts a value into a stable pointer.  It is part of the @PrimIO@
65   monad, because we want to be sure we don't allocate one twice by
66   accident, and then only free one of the copies.
67
68   \begin{verbatim}
69   makeStablePtr#  :: a -> State# RealWorld -> (# RealWorld, a #)
70   freeStablePtr#  :: StablePtr# a -> State# RealWorld -> State# RealWorld
71   deRefStablePtr# :: StablePtr# a -> State# RealWorld -> 
72         (# State# RealWorld, a #)
73   \end{verbatim}
74
75   There may be additional functions on the C side to allow evaluation,
76   application, etc of a stable pointer.
77
78 */
79
80 snEntry *stable_ptr_table = NULL;
81 static snEntry *stable_ptr_free = NULL;
82
83 static unsigned int SPT_size = 0;
84
85 #ifdef THREADED_RTS
86 static Mutex stable_mutex;
87 #endif
88
89 /* This hash table maps Haskell objects to stable names, so that every
90  * call to lookupStableName on a given object will return the same
91  * stable name.
92  *
93  * OLD COMMENTS about reference counting follow.  The reference count
94  * in a stable name entry is now just a counter.
95  *
96  * Reference counting
97  * ------------------
98  * A plain stable name entry has a zero reference count, which means
99  * the entry will dissappear when the object it points to is
100  * unreachable.  For stable pointers, we need an entry that sticks
101  * around and keeps the object it points to alive, so each stable name
102  * entry has an associated reference count.
103  *
104  * A stable pointer has a weighted reference count N attached to it
105  * (actually in its upper 5 bits), which represents the weight
106  * 2^(N-1).  The stable name entry keeps a 32-bit reference count, which
107  * represents any weight between 1 and 2^32 (represented as zero).
108  * When the weight is 2^32, the stable name table owns "all" of the
109  * stable pointers to this object, and the entry can be garbage
110  * collected if the object isn't reachable.
111  *
112  * A new stable pointer is given the weight log2(W/2), where W is the
113  * weight stored in the table entry.  The new weight in the table is W
114  * - 2^log2(W/2).
115  *
116  * A stable pointer can be "split" into two stable pointers, by
117  * dividing the weight by 2 and giving each pointer half.
118  * When freeing a stable pointer, the weight of the pointer is added
119  * to the weight stored in the table entry.
120  * */
121
122 static HashTable *addrToStableHash = NULL;
123
124 #define INIT_SPT_SIZE 64
125
126 STATIC_INLINE void
127 initFreeList(snEntry *table, nat n, snEntry *free)
128 {
129   snEntry *p;
130
131   for (p = table + n - 1; p >= table; p--) {
132     p->addr   = (P_)free;
133     p->old    = NULL;
134     p->ref    = 0;
135     p->sn_obj = NULL;
136     free = p;
137   }
138   stable_ptr_free = table;
139 }
140
141 void
142 initStablePtrTable(void)
143 {
144         if (SPT_size > 0)
145                 return;
146
147     SPT_size = INIT_SPT_SIZE;
148     stable_ptr_table = stgMallocBytes(SPT_size * sizeof(snEntry),
149                                       "initStablePtrTable");
150
151     /* we don't use index 0 in the stable name table, because that
152      * would conflict with the hash table lookup operations which
153      * return NULL if an entry isn't found in the hash table.
154      */
155     initFreeList(stable_ptr_table+1,INIT_SPT_SIZE-1,NULL);
156     addrToStableHash = allocHashTable();
157
158 #ifdef THREADED_RTS
159     initMutex(&stable_mutex);
160 #endif
161 }
162
163 void
164 exitStablePtrTable(void)
165 {
166   if (addrToStableHash)
167     freeHashTable(addrToStableHash, NULL);
168   addrToStableHash = NULL;
169   if (stable_ptr_table)
170     stgFree(stable_ptr_table);
171   stable_ptr_table = NULL;
172   SPT_size = 0;
173 #ifdef THREADED_RTS
174   closeMutex(&stable_mutex);
175 #endif
176 }
177
178 /*
179  * get at the real stuff...remove indirections.
180  *
181  * ToDo: move to a better home.
182  */
183 static
184 StgClosure*
185 removeIndirections(StgClosure* p)
186 {
187   StgClosure* q = p;
188
189   while (get_itbl(q)->type == IND ||
190          get_itbl(q)->type == IND_STATIC ||
191          get_itbl(q)->type == IND_OLDGEN ||
192          get_itbl(q)->type == IND_PERM ||
193          get_itbl(q)->type == IND_OLDGEN_PERM ) {
194       q = ((StgInd *)q)->indirectee;
195   }
196   return q;
197 }
198
199 static StgWord
200 lookupStableName_(StgPtr p)
201 {
202   StgWord sn;
203   void* sn_tmp;
204
205   if (stable_ptr_free == NULL) {
206     enlargeStablePtrTable();
207   }
208
209   /* removing indirections increases the likelihood
210    * of finding a match in the stable name hash table.
211    */
212   p = (StgPtr)removeIndirections((StgClosure*)p);
213
214   sn_tmp = lookupHashTable(addrToStableHash,(W_)p);
215   sn = (StgWord)sn_tmp;
216   
217   if (sn != 0) {
218     ASSERT(stable_ptr_table[sn].addr == p);
219     debugTrace(DEBUG_stable, "cached stable name %ld at %p",sn,p);
220     return sn;
221   } else {
222     sn = stable_ptr_free - stable_ptr_table;
223     stable_ptr_free  = (snEntry*)(stable_ptr_free->addr);
224     stable_ptr_table[sn].ref = 0;
225     stable_ptr_table[sn].addr = p;
226     stable_ptr_table[sn].sn_obj = NULL;
227     /* debugTrace(DEBUG_stable, "new stable name %d at %p\n",sn,p); */
228     
229     /* add the new stable name to the hash table */
230     insertHashTable(addrToStableHash, (W_)p, (void *)sn);
231
232     return sn;
233   }
234 }
235
236 StgWord
237 lookupStableName(StgPtr p)
238 {
239     StgWord res;
240
241     initStablePtrTable();
242     ACQUIRE_LOCK(&stable_mutex);
243     res = lookupStableName_(p);
244     RELEASE_LOCK(&stable_mutex);
245     return res;
246 }
247
248 STATIC_INLINE void
249 freeStableName(snEntry *sn)
250 {
251   ASSERT(sn->sn_obj == NULL);
252   if (sn->addr != NULL) {
253       removeHashTable(addrToStableHash, (W_)sn->addr, NULL);
254   }
255   sn->addr = (P_)stable_ptr_free;
256   stable_ptr_free = sn;
257 }
258
259 StgStablePtr
260 getStablePtr(StgPtr p)
261 {
262   StgWord sn;
263
264   initStablePtrTable();
265   ACQUIRE_LOCK(&stable_mutex);
266   sn = lookupStableName_(p);
267   stable_ptr_table[sn].ref++;
268   RELEASE_LOCK(&stable_mutex);
269   return (StgStablePtr)(sn);
270 }
271
272 void
273 freeStablePtr(StgStablePtr sp)
274 {
275     snEntry *sn;
276
277         initStablePtrTable();
278     ACQUIRE_LOCK(&stable_mutex);
279
280     sn = &stable_ptr_table[(StgWord)sp];
281     
282     ASSERT((StgWord)sp < SPT_size  &&  sn->addr != NULL  &&  sn->ref > 0);
283
284     sn->ref--;
285
286     // If this entry has no StableName attached, then just free it
287     // immediately.  This is important; it might be a while before the
288     // next major GC which actually collects the entry.
289     if (sn->sn_obj == NULL && sn->ref == 0) {
290         freeStableName(sn);
291     }
292
293     RELEASE_LOCK(&stable_mutex);
294 }
295
296 void
297 enlargeStablePtrTable(void)
298 {
299   nat old_SPT_size = SPT_size;
300
301     // 2nd and subsequent times
302   SPT_size *= 2;
303   stable_ptr_table =
304     stgReallocBytes(stable_ptr_table,
305                       SPT_size * sizeof(snEntry),
306                       "enlargeStablePtrTable");
307
308   initFreeList(stable_ptr_table + old_SPT_size, old_SPT_size, NULL);
309 }
310
311 /* -----------------------------------------------------------------------------
312  * Treat stable pointers as roots for the garbage collector.
313  *
314  * A stable pointer is any stable name entry with a ref > 0.  We'll
315  * take the opportunity to zero the "keep" flags at the same time.
316  * -------------------------------------------------------------------------- */
317
318 void
319 markStablePtrTable(evac_fn evac)
320 {
321     snEntry *p, *end_stable_ptr_table;
322     StgPtr q;
323     
324     end_stable_ptr_table = &stable_ptr_table[SPT_size];
325     
326     // Mark all the stable *pointers* (not stable names).
327     // _starting_ at index 1; index 0 is unused.
328     for (p = stable_ptr_table+1; p < end_stable_ptr_table; p++) {
329         q = p->addr;
330
331         // Internal pointers are free slots.  If q == NULL, it's a
332         // stable name where the object has been GC'd, but the
333         // StableName object (sn_obj) is still alive.
334         if (q && (q < (P_)stable_ptr_table || q >= (P_)end_stable_ptr_table)) {
335
336             // save the current addr away: we need to be able to tell
337             // whether the objects moved in order to be able to update
338             // the hash table later.
339             p->old = p->addr;
340
341             // if the ref is non-zero, treat addr as a root
342             if (p->ref != 0) {
343                 evac((StgClosure **)&p->addr);
344             }
345         }
346     }
347 }
348
349 /* -----------------------------------------------------------------------------
350  * Thread the stable pointer table for compacting GC.
351  * 
352  * Here we must call the supplied evac function for each pointer into
353  * the heap from the stable pointer table, because the compacting
354  * collector may move the object it points to.
355  * -------------------------------------------------------------------------- */
356
357 void
358 threadStablePtrTable( evac_fn evac )
359 {
360     snEntry *p, *end_stable_ptr_table;
361     StgPtr q;
362     
363     end_stable_ptr_table = &stable_ptr_table[SPT_size];
364     
365     for (p = stable_ptr_table+1; p < end_stable_ptr_table; p++) {
366         
367         if (p->sn_obj != NULL) {
368             evac((StgClosure **)&p->sn_obj);
369         }
370
371         q = p->addr;
372         if (q && (q < (P_)stable_ptr_table || q >= (P_)end_stable_ptr_table)) {
373             evac((StgClosure **)&p->addr);
374         }
375     }
376 }
377
378 /* -----------------------------------------------------------------------------
379  * Garbage collect any dead entries in the stable pointer table.
380  *
381  * A dead entry has:
382  *
383  *          - a zero reference count
384  *          - a dead sn_obj
385  *
386  * Both of these conditions must be true in order to re-use the stable
387  * name table entry.  We can re-use stable name table entries for live
388  * heap objects, as long as the program has no StableName objects that
389  * refer to the entry.
390  * -------------------------------------------------------------------------- */
391
392 void
393 gcStablePtrTable( void )
394 {
395     snEntry *p, *end_stable_ptr_table;
396     StgPtr q;
397     
398     end_stable_ptr_table = &stable_ptr_table[SPT_size];
399     
400     // NOTE: _starting_ at index 1; index 0 is unused.
401     for (p = stable_ptr_table + 1; p < end_stable_ptr_table; p++) {
402         
403         // Update the pointer to the StableName object, if there is one
404         if (p->sn_obj != NULL) {
405             p->sn_obj = isAlive(p->sn_obj);
406         }
407         
408         // Internal pointers are free slots.  If q == NULL, it's a
409         // stable name where the object has been GC'd, but the
410         // StableName object (sn_obj) is still alive.
411         q = p->addr;
412         if (q && (q < (P_)stable_ptr_table || q >= (P_)end_stable_ptr_table)) {
413
414             // StableNames only:
415             if (p->ref == 0) {
416                 if (p->sn_obj == NULL) {
417                     // StableName object is dead
418                     freeStableName(p);
419                     debugTrace(DEBUG_stable, "GC'd Stable name %ld",
420                                (long)(p - stable_ptr_table));
421                     continue;
422                     
423                 } else {
424                   p->addr = (StgPtr)isAlive((StgClosure *)p->addr);
425                   debugTrace(DEBUG_stable, 
426                              "stable name %ld still alive at %p, ref %ld\n",
427                              (long)(p - stable_ptr_table), p->addr, p->ref);
428                 }
429             }
430         }
431     }
432 }
433
434 /* -----------------------------------------------------------------------------
435  * Update the StablePtr/StableName hash table
436  *
437  * The boolean argument 'full' indicates that a major collection is
438  * being done, so we might as well throw away the hash table and build
439  * a new one.  For a minor collection, we just re-hash the elements
440  * that changed.
441  * -------------------------------------------------------------------------- */
442
443 void
444 updateStablePtrTable(rtsBool full)
445 {
446     snEntry *p, *end_stable_ptr_table;
447     
448     if (full && addrToStableHash != NULL) {
449         freeHashTable(addrToStableHash,NULL);
450         addrToStableHash = allocHashTable();
451     }
452     
453     end_stable_ptr_table = &stable_ptr_table[SPT_size];
454     
455     // NOTE: _starting_ at index 1; index 0 is unused.
456     for (p = stable_ptr_table + 1; p < end_stable_ptr_table; p++) {
457         
458         if (p->addr == NULL) {
459             if (p->old != NULL) {
460                 // The target has been garbage collected.  Remove its
461                 // entry from the hash table.
462                 removeHashTable(addrToStableHash, (W_)p->old, NULL);
463                 p->old = NULL;
464             }
465         }
466         else if (p->addr < (P_)stable_ptr_table 
467                  || p->addr >= (P_)end_stable_ptr_table) {
468             // Target still alive, Re-hash this stable name 
469             if (full) {
470                 insertHashTable(addrToStableHash, (W_)p->addr, 
471                                 (void *)(p - stable_ptr_table));
472             } else if (p->addr != p->old) {
473                 removeHashTable(addrToStableHash, (W_)p->old, NULL);
474                 insertHashTable(addrToStableHash, (W_)p->addr, 
475                                 (void *)(p - stable_ptr_table));
476             }
477         }
478     }
479 }