d85d4a09ba638e79b75d8a75276d03843166de7d
[ghc-hetmet.git] / ghc / rts / Storage.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Storage.h,v 1.23 2001/01/26 14:17:01 simonpj Exp $
3  *
4  * (c) The GHC Team, 1998-1999
5  *
6  * External Storage Manger Interface
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #ifndef STORAGE_H
11 #define STORAGE_H
12
13 #include "Block.h"
14 #include "BlockAlloc.h"
15 #include "StoragePriv.h"
16
17 /* -----------------------------------------------------------------------------
18    Initialisation / De-initialisation
19    -------------------------------------------------------------------------- */
20
21 extern void initStorage(void);
22 extern void exitStorage(void);
23
24 /* -----------------------------------------------------------------------------
25    Generic allocation
26
27    StgPtr allocate(int n)       Allocates a chunk of contiguous store
28                                 n words long, returning a pointer to
29                                 the first word.  Always succeeds.
30                                 
31                                 Don't forget to TICK_ALLOC_XXX(...)
32                                 after calling allocate, for the
33                                 benefit of the ticky-ticky profiler.
34
35    rtsBool doYouWantToGC(void)  Returns True if the storage manager is
36                                 ready to perform a GC, False otherwise.
37
38    lnat  allocated_bytes(void)  Returns the number of bytes allocated
39                                 via allocate() since the last GC.
40                                 Used in the reoprting of statistics.
41
42    SMP: allocate and doYouWantToGC can be used from STG code, they are
43    surrounded by a mutex.
44    -------------------------------------------------------------------------- */
45
46 extern StgPtr  allocate(nat n);
47 static inline rtsBool doYouWantToGC(void)
48 {
49   return (alloc_blocks >= alloc_blocks_lim);
50 }
51 extern lnat allocated_bytes(void);
52
53 /* -----------------------------------------------------------------------------
54    ExtendNursery(hp,hplim)      When hplim is reached, try to grab
55                                 some more allocation space.  Returns
56                                 False if the allocation space is
57                                 exhausted, and the application should
58                                 call GarbageCollect().
59   -------------------------------------------------------------------------- */
60
61 #define ExtendNursery(hp,hplim)                 \
62   (CurrentNursery->free = (P_)(hp)+1,           \
63    CurrentNursery->link == NULL ? rtsFalse :    \
64    (CurrentNursery = CurrentNursery->link,      \
65     OpenNursery(hp,hplim),                      \
66     rtsTrue))
67
68 extern void PleaseStopAllocating(void);
69
70 /* -----------------------------------------------------------------------------
71    Performing Garbage Collection
72
73    GarbageCollect(get_roots)    Performs a garbage collection.  
74                                 'get_roots' is called to find all the 
75                                 roots that the system knows about.
76
77    StgClosure                   Called by get_roots on each root.       
78    MarkRoot(StgClosure *p)      Returns the new location of the root.
79    -------------------------------------------------------------------------- */
80
81 extern void   GarbageCollect(void (*get_roots)(void),rtsBool force_major_gc);
82 extern StgClosure *MarkRoot(StgClosure *p);
83
84 /* Temporary measure to ensure we retain all the dynamically-loaded CAFs */
85 #ifdef GHCI
86 extern void markCafs( void );
87 #endif
88
89 /* -----------------------------------------------------------------------------
90    Generational garbage collection support
91
92    recordMutable(StgPtr p)       Informs the garbage collector that a
93                                  previously immutable object has
94                                  become (permanently) mutable.  Used
95                                  by thawArray and similar.
96
97    updateWithIndirection(p1,p2)  Updates the object at p1 with an
98                                  indirection pointing to p2.  This is
99                                  normally called for objects in an old
100                                  generation (>0) when they are updated.
101
102    updateWithPermIndirection(p1,p2)  As above but uses a permanent indir.
103
104    -------------------------------------------------------------------------- */
105
106 /* ToDo: shouldn't recordMutable and recordOldToNewPtrs acquire some
107  * kind of lock in the SMP case?
108  */
109 static inline void
110 recordMutable(StgMutClosure *p)
111 {
112   bdescr *bd;
113
114 #ifdef SMP
115   ASSERT(p->header.info == &stg_WHITEHOLE_info || closure_MUTABLE(p));
116 #else
117   ASSERT(closure_MUTABLE(p));
118 #endif
119
120   bd = Bdescr((P_)p);
121   if (bd->gen->no > 0) {
122     p->mut_link = bd->gen->mut_list;
123     bd->gen->mut_list = p;
124   }
125 }
126
127 static inline void
128 recordOldToNewPtrs(StgMutClosure *p)
129 {
130   bdescr *bd;
131   
132   bd = Bdescr((P_)p);
133   if (bd->gen->no > 0) {
134     p->mut_link = bd->gen->mut_once_list;
135     bd->gen->mut_once_list = p;
136   }
137 }
138
139 #ifndef DEBUG
140 #define updateWithIndirection(info, p1, p2)                             \
141   {                                                                     \
142     bdescr *bd;                                                         \
143                                                                         \
144     bd = Bdescr((P_)p1);                                                \
145     if (bd->gen->no == 0) {                                             \
146       ((StgInd *)p1)->indirectee = p2;                                  \
147       SET_INFO(p1,&stg_IND_info);                                       \
148       TICK_UPD_NEW_IND();                                               \
149     } else {                                                            \
150       ((StgIndOldGen *)p1)->indirectee = p2;                            \
151       if (info != &stg_BLACKHOLE_BQ_info) {                             \
152         ACQUIRE_LOCK(&sm_mutex);                                        \
153         ((StgIndOldGen *)p1)->mut_link = bd->gen->mut_once_list;        \
154         bd->gen->mut_once_list = (StgMutClosure *)p1;                   \
155         RELEASE_LOCK(&sm_mutex);                                        \
156       }                                                                 \
157       SET_INFO(p1,&stg_IND_OLDGEN_info);                                \
158       TICK_UPD_OLD_IND();                                               \
159     }                                                                   \
160   }
161 #else
162
163 /* In the DEBUG case, we also zero out the slop of the old closure,
164  * so that the sanity checker can tell where the next closure is.
165  */
166 #define updateWithIndirection(info, p1, p2)                             \
167   {                                                                     \
168     bdescr *bd;                                                         \
169                                                                         \
170     bd = Bdescr((P_)p1);                                                \
171     if (bd->gen->no == 0) {                                             \
172       ((StgInd *)p1)->indirectee = p2;                                  \
173       SET_INFO(p1,&stg_IND_info);                                       \
174       TICK_UPD_NEW_IND();                                               \
175     } else {                                                            \
176       if (info != &stg_BLACKHOLE_BQ_info) {                             \
177         {                                                               \
178           StgInfoTable *inf = get_itbl(p1);                             \
179           nat np = inf->layout.payload.ptrs,                            \
180               nw = inf->layout.payload.nptrs, i;                        \
181           for (i = np; i < np + nw; i++) {                              \
182              ((StgClosure *)p1)->payload[i] = 0;                        \
183           }                                                             \
184         }                                                               \
185         ACQUIRE_LOCK(&sm_mutex);                                        \
186         ((StgIndOldGen *)p1)->mut_link = bd->gen->mut_once_list;        \
187         bd->gen->mut_once_list = (StgMutClosure *)p1;                   \
188         RELEASE_LOCK(&sm_mutex);                                        \
189       }                                                                 \
190       ((StgIndOldGen *)p1)->indirectee = p2;                            \
191       SET_INFO(p1,&stg_IND_OLDGEN_info);                                \
192       TICK_UPD_OLD_IND();                                               \
193     }                                                                   \
194   }
195 #endif
196
197 /* Static objects all live in the oldest generation
198  */
199 #define updateWithStaticIndirection(info, p1, p2)                       \
200   {                                                                     \
201     ASSERT( ((StgMutClosure*)p1)->mut_link == NULL );                   \
202                                                                         \
203     ACQUIRE_LOCK(&sm_mutex);                                            \
204     ((StgMutClosure *)p1)->mut_link = oldest_gen->mut_once_list;        \
205     oldest_gen->mut_once_list = (StgMutClosure *)p1;                    \
206     RELEASE_LOCK(&sm_mutex);                                            \
207                                                                         \
208     ((StgInd *)p1)->indirectee = p2;                                    \
209     SET_INFO((StgInd *)p1, &stg_IND_STATIC_info);                       \
210     TICK_UPD_STATIC_IND();                                              \
211   }
212
213 #if defined(TICKY_TICKY) || defined(PROFILING)
214 static inline void
215 updateWithPermIndirection(const StgInfoTable *info, StgClosure *p1, StgClosure *p2) 
216 {
217   bdescr *bd;
218
219   bd = Bdescr((P_)p1);
220   if (bd->gen->no == 0) {
221     ((StgInd *)p1)->indirectee = p2;
222     SET_INFO(p1,&stg_IND_PERM_info);
223     TICK_UPD_NEW_PERM_IND(p1);
224   } else {
225     ((StgIndOldGen *)p1)->indirectee = p2;
226     if (info != &stg_BLACKHOLE_BQ_info) {
227       ACQUIRE_LOCK(&sm_mutex);
228       ((StgIndOldGen *)p1)->mut_link = bd->gen->mut_once_list;
229       bd->gen->mut_once_list = (StgMutClosure *)p1;
230       RELEASE_LOCK(&sm_mutex);
231     }
232     SET_INFO(p1,&stg_IND_OLDGEN_PERM_info);
233     TICK_UPD_OLD_PERM_IND();
234   }
235 }
236 #endif
237
238 /* -----------------------------------------------------------------------------
239    The CAF table - used to let us revert CAFs
240    -------------------------------------------------------------------------- */
241
242 #if defined(INTERPRETER)
243 typedef struct StgCAFTabEntry_ {
244     StgClosure*   closure;
245     StgInfoTable* origItbl;
246 } StgCAFTabEntry;
247
248 extern void addToECafTable ( StgClosure* closure, StgInfoTable* origItbl );
249 extern void clearECafTable ( void );
250
251 extern StgCAF*         ecafList;
252 extern StgCAFTabEntry* ecafTable;
253 extern StgInt          usedECafTable;
254 extern StgInt          sizeECafTable;
255 #endif
256
257 #if defined(DEBUG)
258 void printMutOnceList(generation *gen);
259 void printMutableList(generation *gen);
260 #endif DEBUG
261
262 /* --------------------------------------------------------------------------
263                       Address space layout macros
264    --------------------------------------------------------------------------
265
266    Here are the assumptions GHC makes about address space layout.
267    Broadly, it thinks there are three sections:
268
269      CODE    Read-only.  Contains code and read-only data (such as
270                 info tables)
271              Also called "text"
272
273      DATA    Read-write data.  Contains static closures (and on some
274                 architectures, info tables too)
275
276      HEAP    Dynamically-allocated closures
277
278    Three macros identify these three areas:
279      IS_CODE(p), IS_DATA(p), HEAP_ALLOCED(p)
280
281    HEAP_ALLOCED is called FOR EVERY SINGLE CLOSURE during GC.
282    It needs to be FAST.
283
284    Implementation of HEAP_ALLOCED
285    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
286    Concerning HEAP, most of the time (certainly under [Static] and [GHCi],
287    we ensure that the heap is allocated above some fixed address HEAP_BASE
288    (defined in MBlock.h).  In this case we set TEXT_BEFORE_HEAP, and we
289    get a nice fast test.
290
291    Sometimes we can't be quite sure.  For example in Windows, we can't 
292    fix where our heap address space comes from.  In this case we un-set 
293    TEXT_BEFORE_HEAP. That makes it more expensive to test whether a pointer
294    comes from the HEAP section, because we need to look at the allocator's
295    address maps (see HEAP_ALLOCED macro)
296
297    Implementation of CODE and DATA
298    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
299    Concerning CODE and DATA, there are three main regimes:
300
301      [Static] Totally      The segments are contiguous, and laid out 
302      statically linked     exactly as above
303
304      [GHCi] Static,        GHCi may load new modules, but it knows the
305      except for GHCi       address map, so for any given address it can
306                            still tell which section it belongs to
307
308      [DLL] OS-supported    Chunks of CODE and DATA may be mixed in 
309      dynamic loading       the address space, and we can't tell how
310
311
312    For the [Static] case, we assume memory is laid out like this
313    (in order of increasing addresses)
314
315        Start of memory
316            CODE section
317        TEXT_SECTION_END_MARKER   (usually _etext)
318            DATA section
319        DATA_SECTION_END_MARKER   (usually _end)
320            ???
321        HEAP_BASE
322            HEAP section
323
324    For the [GHCi] case, we have to consult GHCi's dynamic linker's
325    address maps, which is done by macros
326          is_dynamically_loaded_code_or_rodata_ptr
327          is_dynamically_loaded_code_or_rwdata_ptr
328
329    For the [DLL] case, IS_CODE and IS_DATA are really not usable at all.
330  */
331
332
333 #undef TEXT_BEFORE_HEAP
334 #ifndef mingw32_TARGET_OS
335 #define TEXT_BEFORE_HEAP 1
336 #endif
337
338 extern void* TEXT_SECTION_END_MARKER_DECL;
339 extern void* DATA_SECTION_END_MARKER_DECL;
340
341 #if defined(INTERPRETER) || defined(GHCI)
342 /* Take into account code sections in dynamically loaded object files. */
343 #define IS_CODE_PTR(p) (  ((P_)(p) < (P_)&TEXT_SECTION_END_MARKER) \
344                        || is_dynamically_loaded_code_or_rodata_ptr((char *)p) )
345 #define IS_DATA_PTR(p) ( ((P_)(p) >= (P_)&TEXT_SECTION_END_MARKER && \
346                           (P_)(p) < (P_)&DATA_SECTION_END_MARKER) \
347                        || is_dynamically_loaded_rwdata_ptr((char *)p) )
348 #define IS_USER_PTR(p) ( ((P_)(p) >= (P_)&DATA_SECTION_END_MARKER) \
349                        && is_not_dynamically_loaded_ptr((char *)p) )
350 #else
351 #define IS_CODE_PTR(p) ((P_)(p) < (P_)&TEXT_SECTION_END_MARKER)
352 #define IS_DATA_PTR(p) ((P_)(p) >= (P_)&TEXT_SECTION_END_MARKER && (P_)(p) < (P_)&DATA_SECTION_END_MARKER)
353 #define IS_USER_PTR(p) ((P_)(p) >= (P_)&DATA_SECTION_END_MARKER)
354 #endif
355
356 /* The HEAP_ALLOCED test below is called FOR EVERY SINGLE CLOSURE
357  * during GC.  It needs to be FAST.
358  *
359  * BEWARE: when we're dynamically loading code (for GHCi), make sure
360  * that we don't load any code above HEAP_BASE, or this test won't work.
361  */
362 #ifdef TEXT_BEFORE_HEAP
363 # define HEAP_ALLOCED(x)  ((StgPtr)(x) >= (StgPtr)(HEAP_BASE))
364 #else
365 extern int is_heap_alloced(const void* x);
366 # define HEAP_ALLOCED(x)  (is_heap_alloced(x))
367 #endif
368
369
370 /* --------------------------------------------------------------------------
371    Macros for distinguishing data pointers from code pointers
372    --------------------------------------------------------------------------
373
374   Specification
375   ~~~~~~~~~~~~~
376   The garbage collector needs to make some critical distinctions between pointers.
377   In particular we need
378  
379      LOOKS_LIKE_GHC_INFO(p)          p points to an info table
380
381   For both of these macros, p is
382       *either* a pointer to a closure (static or heap allocated)
383       *or* a return address on the (Haskell) stack
384
385   (Return addresses are in fact info-pointers, so that the Haskell stack
386   looks very like a chunk of heap.)
387
388   The garbage collector uses LOOKS_LIKE_GHC_INFO when walking the stack, as it
389   walks over the "pending arguments" on its way to the next return address.
390   It is called moderately often, but not as often as HEAP_ALLOCED
391
392
393   Implementation
394   ~~~~~~~~~~~~~~
395   LOOKS_LIKE_GHC_INFO is more complicated because of the need to distinguish 
396   between static closures and info tables.  It's a known portability problem.
397   We have three approaches:
398
399   Plan A: Address-space partitioning.  
400     Keep info tables in the (single, contiguous) text segment:    IS_CODE_PTR(p)
401     and static closures in the (single, contiguous) data segment: IS_DATA_PTR(p)
402
403   Plan A can fail for two reasons:
404     * In many environments (eg. dynamic loading),
405       text and data aren't in a single contiguous range.  
406     * When we compile through vanilla C (no mangling) we sometimes
407       can't guaranteee to put info tables in the text section.  This
408       happens eg. on MacOS where the C compiler refuses to put const
409       data in the text section if it has any code pointers in it
410       (which info tables do *only* when we're compiling without
411       TABLES_NEXT_TO_CODE).
412     
413   Hence, Plan B: (compile-via-C-with-mangling, or native code generation)
414     Put a zero word before each static closure.
415     When compiling to native code, or via C-with-mangling, info tables
416     are laid out "backwards" from the address specified in the info pointer
417     (the entry code goes forward from the info pointer).  Hence, the word
418     before the one referenced the info pointer is part of the info table,
419     and is guaranteed non-zero.
420
421     For reasons nobody seems to fully understand, the statically-allocated tables
422     of INTLIKE and CHARLIKE closures can't have this zero word, so we
423     have to test separately for them.
424
425     Plan B fails altogether for the compile-through-vanilla-C route, because
426     info tables aren't laid out backwards.
427
428
429   Hence, Plan C: (unregisterised, compile-through-vanilla-C route only)
430     If we didn't manage to get info tables into the text section, then
431     we can distinguish between a static closure pointer and an info
432     pointer as follows:  the first word of an info table is a code pointer,
433     and therefore in text space, whereas the first word of a closure pointer
434     is an info pointer, and therefore not.  Shazam!
435 */
436
437
438 /* When working with Win32 DLLs, static closures are identified by
439    being prefixed with a zero word. This is needed so that we can
440    distinguish between pointers to static closures and (reversed!)
441    info tables.
442
443    This 'scheme' breaks down for closure tables such as CHARLIKE,
444    so we catch these separately.
445   
446    LOOKS_LIKE_STATIC_CLOSURE() 
447        - discriminates between static closures and info tbls
448          (needed by LOOKS_LIKE_GHC_INFO() below - [Win32 DLLs only.])
449    LOOKS_LIKE_STATIC() 
450        - distinguishes between static and heap allocated data.
451  */
452 #if defined(ENABLE_WIN32_DLL_SUPPORT) && !defined(INTERPRETER)
453             /* definitely do not enable for mingw DietHEP */
454 #define LOOKS_LIKE_STATIC(r) (!(HEAP_ALLOCED(r)))
455
456 /* Tiresome predicates needed to check for pointers into the closure tables */
457 #define IS_CHARLIKE_CLOSURE(p) \
458     ( (P_)(p) >= (P_)stg_CHARLIKE_closure && \
459       (char*)(p) <= ((char*)stg_CHARLIKE_closure + \
460                      (MAX_CHARLIKE-MIN_CHARLIKE) * sizeof(StgIntCharlikeClosure)) )
461 #define IS_INTLIKE_CLOSURE(p) \
462     ( (P_)(p) >= (P_)stg_INTLIKE_closure && \
463       (char*)(p) <= ((char*)stg_INTLIKE_closure + \
464                      (MAX_INTLIKE-MIN_INTLIKE) * sizeof(StgIntCharlikeClosure)) )
465
466 #define LOOKS_LIKE_STATIC_CLOSURE(r) (((*(((unsigned long *)(r))-1)) == 0) || IS_CHARLIKE_CLOSURE(r) || IS_INTLIKE_CLOSURE(r))
467 #else
468 #define LOOKS_LIKE_STATIC(r) IS_DATA_PTR(r)
469 #define LOOKS_LIKE_STATIC_CLOSURE(r) IS_DATA_PTR(r)
470 #endif
471
472
473 /* -----------------------------------------------------------------------------
474    Macros for distinguishing infotables from closures.
475    
476    You'd think it'd be easy to tell an info pointer from a closure pointer:
477    closures live on the heap and infotables are in read only memory.  Right?
478    Wrong!  Static closures live in read only memory and Hugs allocates
479    infotables for constructors on the (writable) C heap.
480    -------------------------------------------------------------------------- */
481
482 #ifdef INTERPRETER
483 #  ifdef USE_MINIINTERPRETER
484      /* yoiks: one of the dreaded pointer equality tests */
485 #    define IS_HUGS_CONSTR_INFO(info) \
486             (((StgInfoTable *)(info))->entry == (StgFunPtr)&Hugs_CONSTR_entry)
487 #  else
488 #    define IS_HUGS_CONSTR_INFO(info) 0 /* ToDo: more than mildly bogus */
489 #  endif
490 #elif GHCI
491    /* not accurate by any means, but stops the assertions failing... */
492 #  define IS_HUGS_CONSTR_INFO(info)  IS_USER_PTR(info)
493 #else
494 #  define IS_HUGS_CONSTR_INFO(info) 0 /* ToDo: more than mildly bogus */
495 #endif
496
497 /* LOOKS_LIKE_GHC_INFO is called moderately often during GC, but
498  * Certainly not as often as HEAP_ALLOCED.
499  */
500 #ifdef TEXT_BEFORE_HEAP /* needed for mingw DietHEP */
501 # define LOOKS_LIKE_GHC_INFO(info) IS_CODE_PTR(info)
502 #else
503 # define LOOKS_LIKE_GHC_INFO(info) (!HEAP_ALLOCED(info) \
504                                     && !LOOKS_LIKE_STATIC_CLOSURE(info))
505 #endif
506
507
508 /* -----------------------------------------------------------------------------
509    Macros for calculating how big a closure will be (used during allocation)
510    -------------------------------------------------------------------------- */
511
512 /* ToDo: replace unsigned int by nat.  The only fly in the ointment is that
513  * nat comes from Rts.h which many folk dont include.  Sigh!
514  */
515 static __inline__ StgOffset AP_sizeW    ( unsigned int n_args )              
516 { return sizeofW(StgAP_UPD) + n_args; }
517
518 static __inline__ StgOffset PAP_sizeW   ( unsigned int n_args )              
519 { return sizeofW(StgPAP)    + n_args; }
520
521 static __inline__ StgOffset CONSTR_sizeW( unsigned int p, unsigned int np )  
522 { return sizeofW(StgHeader) + p + np; }
523
524 static __inline__ StgOffset THUNK_SELECTOR_sizeW ( void )                    
525 { return sizeofW(StgHeader) + MIN_UPD_SIZE; }
526
527 static __inline__ StgOffset BLACKHOLE_sizeW ( void )                    
528 { return sizeofW(StgHeader) + MIN_UPD_SIZE; }
529
530 static __inline__ StgOffset CAF_sizeW ( void )                    
531 { return sizeofW(StgCAF); }
532
533 /* --------------------------------------------------------------------------
534  * Sizes of closures
535  * ------------------------------------------------------------------------*/
536
537 static __inline__ StgOffset sizeW_fromITBL( const StgInfoTable* itbl ) 
538 { return sizeofW(StgClosure) 
539        + sizeofW(StgPtr)  * itbl->layout.payload.ptrs 
540        + sizeofW(StgWord) * itbl->layout.payload.nptrs; }
541
542 static __inline__ StgOffset pap_sizeW( StgPAP* x )
543 { return PAP_sizeW(x->n_args); }
544
545 static __inline__ StgOffset arr_words_sizeW( StgArrWords* x )
546 { return sizeofW(StgArrWords) + x->words; }
547
548 static __inline__ StgOffset mut_arr_ptrs_sizeW( StgMutArrPtrs* x )
549 { return sizeofW(StgMutArrPtrs) + x->ptrs; }
550
551 static __inline__ StgWord tso_sizeW ( StgTSO *tso )
552 { return TSO_STRUCT_SIZEW + tso->stack_size; }
553
554 #endif STORAGE_H
555