1 /* -----------------------------------------------------------------------------
2 * $Id: Storage.h,v 1.37 2001/11/22 14:25:12 simonmar Exp $
4 * (c) The GHC Team, 1998-1999
6 * External Storage Manger Interface
8 * ---------------------------------------------------------------------------*/
14 #include "BlockAlloc.h"
15 #include "StoragePriv.h"
17 #include "LdvProfile.h"
20 /* -----------------------------------------------------------------------------
21 Initialisation / De-initialisation
22 -------------------------------------------------------------------------- */
24 extern void initStorage(void);
25 extern void exitStorage(void);
27 /* -----------------------------------------------------------------------------
30 StgPtr allocate(nat n) Allocates a chunk of contiguous store
31 n words long, returning a pointer to
32 the first word. Always succeeds.
34 StgPtr allocatePinned(nat n) Allocates a chunk of contiguous store
35 n words long, which is at a fixed
36 address (won't be moved by GC).
37 Returns a pointer to the first word.
40 NOTE: the GC can't in general handle
41 pinned objects, so allocatePinned()
42 can only be used for ByteArrays at the
45 Don't forget to TICK_ALLOC_XXX(...)
46 after calling allocate or
47 allocatePinned, for the
48 benefit of the ticky-ticky profiler.
50 rtsBool doYouWantToGC(void) Returns True if the storage manager is
51 ready to perform a GC, False otherwise.
53 lnat allocated_bytes(void) Returns the number of bytes allocated
54 via allocate() since the last GC.
55 Used in the reoprting of statistics.
57 SMP: allocate and doYouWantToGC can be used from STG code, they are
58 surrounded by a mutex.
59 -------------------------------------------------------------------------- */
61 extern StgPtr allocate ( nat n );
62 extern StgPtr allocatePinned ( nat n );
63 extern lnat allocated_bytes ( void );
68 return (alloc_blocks >= alloc_blocks_lim);
71 /* -----------------------------------------------------------------------------
72 ExtendNursery(hp,hplim) When hplim is reached, try to grab
73 some more allocation space. Returns
74 False if the allocation space is
75 exhausted, and the application should
76 call GarbageCollect().
77 -------------------------------------------------------------------------- */
79 #define ExtendNursery(hp,hplim) \
80 (CurrentNursery->free = (P_)(hp)+1, \
81 CurrentNursery->link == NULL ? rtsFalse : \
82 (CurrentNursery = CurrentNursery->link, \
83 OpenNursery(hp,hplim), \
86 extern void PleaseStopAllocating(void);
88 /* -----------------------------------------------------------------------------
89 Performing Garbage Collection
91 GarbageCollect(get_roots) Performs a garbage collection.
92 'get_roots' is called to find all the
93 roots that the system knows about.
95 StgClosure Called by get_roots on each root.
96 MarkRoot(StgClosure *p) Returns the new location of the root.
97 -------------------------------------------------------------------------- */
99 extern void GarbageCollect(void (*get_roots)(evac_fn),rtsBool force_major_gc);
101 /* -----------------------------------------------------------------------------
102 Generational garbage collection support
104 recordMutable(StgPtr p) Informs the garbage collector that a
105 previously immutable object has
106 become (permanently) mutable. Used
107 by thawArray and similar.
109 updateWithIndirection(p1,p2) Updates the object at p1 with an
110 indirection pointing to p2. This is
111 normally called for objects in an old
112 generation (>0) when they are updated.
114 updateWithPermIndirection(p1,p2) As above but uses a permanent indir.
116 -------------------------------------------------------------------------- */
119 * Storage manager mutex
122 extern pthread_mutex_t sm_mutex;
125 /* ToDo: shouldn't recordMutable and recordOldToNewPtrs acquire some
126 * kind of lock in the SMP case?
129 recordMutable(StgMutClosure *p)
134 ASSERT(p->header.info == &stg_WHITEHOLE_info || closure_MUTABLE(p));
136 ASSERT(closure_MUTABLE(p));
140 if (bd->gen_no > 0) {
141 p->mut_link = generations[bd->gen_no].mut_list;
142 generations[bd->gen_no].mut_list = p;
147 recordOldToNewPtrs(StgMutClosure *p)
152 if (bd->gen_no > 0) {
153 p->mut_link = generations[bd->gen_no].mut_once_list;
154 generations[bd->gen_no].mut_once_list = p;
159 // We zero out the slop when PROFILING is on.
161 #if !defined(DEBUG) && !defined(PROFILING)
162 #define updateWithIndirection(info, p1, p2) \
166 bd = Bdescr((P_)p1); \
167 if (bd->gen_no == 0) { \
168 ((StgInd *)p1)->indirectee = p2; \
169 SET_INFO(p1,&stg_IND_info); \
170 TICK_UPD_NEW_IND(); \
172 ((StgIndOldGen *)p1)->indirectee = p2; \
173 if (info != &stg_BLACKHOLE_BQ_info) { \
174 ACQUIRE_LOCK(&sm_mutex); \
175 ((StgIndOldGen *)p1)->mut_link = generations[bd->gen_no].mut_once_list; \
176 generations[bd->gen_no].mut_once_list = (StgMutClosure *)p1; \
177 RELEASE_LOCK(&sm_mutex); \
179 SET_INFO(p1,&stg_IND_OLDGEN_info); \
180 TICK_UPD_OLD_IND(); \
183 #elif defined(PROFILING)
185 // We call LDV_recordDead_FILL_SLOP_DYNAMIC(p1) regardless of the generation in
189 // After all, we do *NOT* need to call LDV_recordCreate() for both IND and
190 // IND_OLDGEN closures because they are inherently used. But, it corrupts
191 // the invariants that every closure keeps its creation time in the profiling
192 // field. So, we call LDV_recordCreate().
194 #define updateWithIndirection(info, p1, p2) \
198 LDV_recordDead_FILL_SLOP_DYNAMIC((p1)); \
199 bd = Bdescr((P_)p1); \
200 if (bd->gen_no == 0) { \
201 ((StgInd *)p1)->indirectee = p2; \
202 SET_INFO(p1,&stg_IND_info); \
203 LDV_recordCreate((p1)); \
204 TICK_UPD_NEW_IND(); \
206 ((StgIndOldGen *)p1)->indirectee = p2; \
207 if (info != &stg_BLACKHOLE_BQ_info) { \
208 ACQUIRE_LOCK(&sm_mutex); \
209 ((StgIndOldGen *)p1)->mut_link = generations[bd->gen_no].mut_once_list; \
210 generations[bd->gen_no].mut_once_list = (StgMutClosure *)p1; \
211 RELEASE_LOCK(&sm_mutex); \
213 SET_INFO(p1,&stg_IND_OLDGEN_info); \
214 LDV_recordCreate((p1)); \
220 /* In the DEBUG case, we also zero out the slop of the old closure,
221 * so that the sanity checker can tell where the next closure is.
223 * Two important invariants: we should never try to update a closure
224 * to point to itself, and the closure being updated should not
225 * already have been updated (the mutable list will get messed up
228 #define updateWithIndirection(info, p1, p2) \
232 ASSERT( p1 != p2 && !closure_IND(p1) ); \
233 bd = Bdescr((P_)p1); \
234 if (bd->gen_no == 0) { \
235 ((StgInd *)p1)->indirectee = p2; \
236 SET_INFO(p1,&stg_IND_info); \
237 TICK_UPD_NEW_IND(); \
239 if (info != &stg_BLACKHOLE_BQ_info) { \
241 StgInfoTable *inf = get_itbl(p1); \
242 nat np = inf->layout.payload.ptrs, \
243 nw = inf->layout.payload.nptrs, i; \
244 if (inf->type != THUNK_SELECTOR) { \
245 for (i = np; i < np + nw; i++) { \
246 ((StgClosure *)p1)->payload[i] = 0; \
250 ACQUIRE_LOCK(&sm_mutex); \
251 ((StgIndOldGen *)p1)->mut_link = generations[bd->gen_no].mut_once_list; \
252 generations[bd->gen_no].mut_once_list = (StgMutClosure *)p1; \
253 RELEASE_LOCK(&sm_mutex); \
255 ((StgIndOldGen *)p1)->indirectee = p2; \
256 SET_INFO(p1,&stg_IND_OLDGEN_info); \
257 TICK_UPD_OLD_IND(); \
262 /* Static objects all live in the oldest generation
264 #define updateWithStaticIndirection(info, p1, p2) \
266 ASSERT( p1 != p2 && !closure_IND(p1) ); \
267 ASSERT( ((StgMutClosure*)p1)->mut_link == NULL ); \
269 ACQUIRE_LOCK(&sm_mutex); \
270 ((StgMutClosure *)p1)->mut_link = oldest_gen->mut_once_list; \
271 oldest_gen->mut_once_list = (StgMutClosure *)p1; \
272 RELEASE_LOCK(&sm_mutex); \
274 ((StgInd *)p1)->indirectee = p2; \
275 SET_INFO((StgInd *)p1, &stg_IND_STATIC_info); \
276 TICK_UPD_STATIC_IND(); \
279 #if defined(TICKY_TICKY) || defined(PROFILING)
281 updateWithPermIndirection(const StgInfoTable *info, StgClosure *p1, StgClosure *p2)
285 ASSERT( p1 != p2 && !closure_IND(p1) );
288 // Destroy the old closure.
289 LDV_recordDead_FILL_SLOP_DYNAMIC(p1);
291 if (bd->gen_no == 0) {
292 ((StgInd *)p1)->indirectee = p2;
293 SET_INFO(p1,&stg_IND_PERM_info);
295 // We have just created a new closure.
296 LDV_recordCreate(p1);
297 TICK_UPD_NEW_PERM_IND(p1);
299 ((StgIndOldGen *)p1)->indirectee = p2;
300 if (info != &stg_BLACKHOLE_BQ_info) {
301 ACQUIRE_LOCK(&sm_mutex);
302 ((StgIndOldGen *)p1)->mut_link = generations[bd->gen_no].mut_once_list;
303 generations[bd->gen_no].mut_once_list = (StgMutClosure *)p1;
304 RELEASE_LOCK(&sm_mutex);
306 SET_INFO(p1,&stg_IND_OLDGEN_PERM_info);
308 // We have just created a new closure.
309 LDV_recordCreate(p1);
310 TICK_UPD_OLD_PERM_IND();
315 /* -----------------------------------------------------------------------------
316 The CAF table - used to let us revert CAFs
317 -------------------------------------------------------------------------- */
319 void revertCAFs( void );
322 void printMutOnceList(generation *gen);
323 void printMutableList(generation *gen);
326 /* --------------------------------------------------------------------------
327 Address space layout macros
328 --------------------------------------------------------------------------
330 Here are the assumptions GHC makes about address space layout.
331 Broadly, it thinks there are three sections:
333 CODE Read-only. Contains code and read-only data (such as
337 DATA Read-write data. Contains static closures (and on some
338 architectures, info tables too)
340 HEAP Dynamically-allocated closures
342 USER None of the above. The only way USER things arise right
343 now is when GHCi allocates a constructor info table, which
344 it does by mallocing them.
346 Three macros identify these three areas:
347 IS_CODE(p), IS_DATA(p), HEAP_ALLOCED(p)
349 HEAP_ALLOCED is called FOR EVERY SINGLE CLOSURE during GC.
352 Implementation of HEAP_ALLOCED
353 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
354 Concerning HEAP, most of the time (certainly under [Static] and [GHCi],
355 we ensure that the heap is allocated above some fixed address HEAP_BASE
356 (defined in MBlock.h). In this case we set TEXT_BEFORE_HEAP, and we
357 get a nice fast test.
359 Sometimes we can't be quite sure. For example in Windows, we can't
360 fix where our heap address space comes from. In this case we un-set
361 TEXT_BEFORE_HEAP. That makes it more expensive to test whether a pointer
362 comes from the HEAP section, because we need to look at the allocator's
363 address maps (see HEAP_ALLOCED macro)
365 Implementation of CODE and DATA
366 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
367 Concerning CODE and DATA, there are three main regimes:
369 [Static] Totally The segments are contiguous, and laid out
370 statically linked exactly as above
372 [GHCi] Static, GHCi may load new modules, but it knows the
373 except for GHCi address map, so for any given address it can
374 still tell which section it belongs to
376 [DLL] OS-supported Chunks of CODE and DATA may be mixed in
377 dynamic loading the address space, and we can't tell how
380 For the [Static] case, we assume memory is laid out like this
381 (in order of increasing addresses)
385 TEXT_SECTION_END_MARKER (usually _etext)
387 DATA_SECTION_END_MARKER (usually _end)
392 For the [GHCi] case, we have to consult GHCi's dynamic linker's
393 address maps, which is done by macros
394 is_dynamically_loaded_code_or_rodata_ptr
395 is_dynamically_loaded_code_or_rwdata_ptr
397 For the [DLL] case, IS_CODE and IS_DATA are really not usable at all.
401 #undef TEXT_BEFORE_HEAP
402 #ifndef mingw32_TARGET_OS
403 #define TEXT_BEFORE_HEAP 1
406 extern void* TEXT_SECTION_END_MARKER_DECL;
407 extern void* DATA_SECTION_END_MARKER_DECL;
409 /* Take into account code sections in dynamically loaded object files. */
410 #define IS_CODE_PTR(p) ( ((P_)(p) < (P_)&TEXT_SECTION_END_MARKER) \
411 || is_dynamically_loaded_code_or_rodata_ptr((char *)p) )
412 #define IS_DATA_PTR(p) ( ((P_)(p) >= (P_)&TEXT_SECTION_END_MARKER && \
413 (P_)(p) < (P_)&DATA_SECTION_END_MARKER) \
414 || is_dynamically_loaded_rwdata_ptr((char *)p) )
415 #define IS_USER_PTR(p) ( ((P_)(p) >= (P_)&DATA_SECTION_END_MARKER) \
416 && is_not_dynamically_loaded_ptr((char *)p) )
418 /* The HEAP_ALLOCED test below is called FOR EVERY SINGLE CLOSURE
419 * during GC. It needs to be FAST.
421 * BEWARE: when we're dynamically loading code (for GHCi), make sure
422 * that we don't load any code above HEAP_BASE, or this test won't work.
424 #ifdef TEXT_BEFORE_HEAP
425 # define HEAP_ALLOCED(x) ((StgPtr)(x) >= (StgPtr)(HEAP_BASE))
427 extern int is_heap_alloced(const void* x);
428 # define HEAP_ALLOCED(x) (is_heap_alloced(x))
432 /* --------------------------------------------------------------------------
433 Macros for distinguishing data pointers from code pointers
434 --------------------------------------------------------------------------
438 The garbage collector needs to make some critical distinctions between pointers.
439 In particular we need
441 LOOKS_LIKE_GHC_INFO(p) p points to an info table
443 For both of these macros, p is
444 *either* a pointer to a closure (static or heap allocated)
445 *or* a return address on the (Haskell) stack
447 (Return addresses are in fact info-pointers, so that the Haskell stack
448 looks very like a chunk of heap.)
450 The garbage collector uses LOOKS_LIKE_GHC_INFO when walking the stack, as it
451 walks over the "pending arguments" on its way to the next return address.
452 It is called moderately often, but not as often as HEAP_ALLOCED
454 ToDo: LOOKS_LIKE_GHC_INFO(p) does not return True when p points to a
455 constructor info table allocated by GHCi. We should really rename
456 LOOKS_LIKE_GHC_INFO to LOOKS_LIKE_GHC_RETURN_INFO.
460 LOOKS_LIKE_GHC_INFO is more complicated because of the need to distinguish
461 between static closures and info tables. It's a known portability problem.
462 We have three approaches:
464 Plan A: Address-space partitioning.
465 Keep info tables in the (single, contiguous) text segment: IS_CODE_PTR(p)
466 and static closures in the (single, contiguous) data segment: IS_DATA_PTR(p)
468 Plan A can fail for two reasons:
469 * In many environments (eg. dynamic loading),
470 text and data aren't in a single contiguous range.
471 * When we compile through vanilla C (no mangling) we sometimes
472 can't guaranteee to put info tables in the text section. This
473 happens eg. on MacOS where the C compiler refuses to put const
474 data in the text section if it has any code pointers in it
475 (which info tables do *only* when we're compiling without
476 TABLES_NEXT_TO_CODE).
478 Hence, Plan B: (compile-via-C-with-mangling, or native code generation)
479 Put a zero word before each static closure.
480 When compiling to native code, or via C-with-mangling, info tables
481 are laid out "backwards" from the address specified in the info pointer
482 (the entry code goes forward from the info pointer). Hence, the word
483 before the one referenced the info pointer is part of the info table,
484 and is guaranteed non-zero.
486 For reasons nobody seems to fully understand, the statically-allocated tables
487 of INTLIKE and CHARLIKE closures can't have this zero word, so we
488 have to test separately for them.
490 Plan B fails altogether for the compile-through-vanilla-C route, because
491 info tables aren't laid out backwards.
494 Hence, Plan C: (unregisterised, compile-through-vanilla-C route only)
495 If we didn't manage to get info tables into the text section, then
496 we can distinguish between a static closure pointer and an info
497 pointer as follows: the first word of an info table is a code pointer,
498 and therefore in text space, whereas the first word of a closure pointer
499 is an info pointer, and therefore not. Shazam!
503 /* When working with Win32 DLLs, static closures are identified by
504 being prefixed with a zero word. This is needed so that we can
505 distinguish between pointers to static closures and (reversed!)
508 This 'scheme' breaks down for closure tables such as CHARLIKE,
509 so we catch these separately.
511 LOOKS_LIKE_STATIC_CLOSURE()
512 - discriminates between static closures and info tbls
513 (needed by LOOKS_LIKE_GHC_INFO() below - [Win32 DLLs only.])
515 - distinguishes between static and heap allocated data.
517 #if defined(ENABLE_WIN32_DLL_SUPPORT)
518 /* definitely do not enable for mingw DietHEP */
519 #define LOOKS_LIKE_STATIC(r) (!(HEAP_ALLOCED(r)))
521 /* Tiresome predicates needed to check for pointers into the closure tables */
522 #define IS_CHARLIKE_CLOSURE(p) \
523 ( (P_)(p) >= (P_)stg_CHARLIKE_closure && \
524 (char*)(p) <= ((char*)stg_CHARLIKE_closure + \
525 (MAX_CHARLIKE-MIN_CHARLIKE) * sizeof(StgIntCharlikeClosure)) )
526 #define IS_INTLIKE_CLOSURE(p) \
527 ( (P_)(p) >= (P_)stg_INTLIKE_closure && \
528 (char*)(p) <= ((char*)stg_INTLIKE_closure + \
529 (MAX_INTLIKE-MIN_INTLIKE) * sizeof(StgIntCharlikeClosure)) )
531 #define LOOKS_LIKE_STATIC_CLOSURE(r) (((*(((unsigned long *)(r))-1)) == 0) || IS_CHARLIKE_CLOSURE(r) || IS_INTLIKE_CLOSURE(r))
533 #define LOOKS_LIKE_STATIC(r) IS_DATA_PTR(r)
534 #define LOOKS_LIKE_STATIC_CLOSURE(r) IS_DATA_PTR(r)
538 /* -----------------------------------------------------------------------------
539 Macros for distinguishing infotables from closures.
541 You'd think it'd be easy to tell an info pointer from a closure pointer:
542 closures live on the heap and infotables are in read only memory. Right?
543 Wrong! Static closures live in read only memory and Hugs allocates
544 infotables for constructors on the (writable) C heap.
545 -------------------------------------------------------------------------- */
547 /* not accurate by any means, but stops the assertions failing... */
548 /* TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO */
549 #define IS_HUGS_CONSTR_INFO(info) IS_USER_PTR(info)
551 /* LOOKS_LIKE_GHC_INFO is called moderately often during GC, but
552 * Certainly not as often as HEAP_ALLOCED.
554 #ifdef TEXT_BEFORE_HEAP /* needed for mingw DietHEP */
555 # define LOOKS_LIKE_GHC_INFO(info) IS_CODE_PTR(info)
557 # define LOOKS_LIKE_GHC_INFO(info) (!HEAP_ALLOCED(info) \
558 && !LOOKS_LIKE_STATIC_CLOSURE(info))
562 /* -----------------------------------------------------------------------------
563 Macros for calculating how big a closure will be (used during allocation)
564 -------------------------------------------------------------------------- */
566 static __inline__ StgOffset AP_sizeW ( nat n_args )
567 { return sizeofW(StgAP_UPD) + n_args; }
569 static __inline__ StgOffset PAP_sizeW ( nat n_args )
570 { return sizeofW(StgPAP) + n_args; }
572 static __inline__ StgOffset CONSTR_sizeW( nat p, nat np )
573 { return sizeofW(StgHeader) + p + np; }
575 static __inline__ StgOffset THUNK_SELECTOR_sizeW ( void )
576 { return sizeofW(StgHeader) + MIN_UPD_SIZE; }
578 static __inline__ StgOffset BLACKHOLE_sizeW ( void )
579 { return sizeofW(StgHeader) + MIN_UPD_SIZE; }
581 /* --------------------------------------------------------------------------
583 * ------------------------------------------------------------------------*/
585 static __inline__ StgOffset sizeW_fromITBL( const StgInfoTable* itbl )
586 { return sizeofW(StgClosure)
587 + sizeofW(StgPtr) * itbl->layout.payload.ptrs
588 + sizeofW(StgWord) * itbl->layout.payload.nptrs; }
590 static __inline__ StgOffset pap_sizeW( StgPAP* x )
591 { return PAP_sizeW(x->n_args); }
593 static __inline__ StgOffset arr_words_sizeW( StgArrWords* x )
594 { return sizeofW(StgArrWords) + x->words; }
596 static __inline__ StgOffset mut_arr_ptrs_sizeW( StgMutArrPtrs* x )
597 { return sizeofW(StgMutArrPtrs) + x->ptrs; }
599 static __inline__ StgWord tso_sizeW ( StgTSO *tso )
600 { return TSO_STRUCT_SIZEW + tso->stack_size; }
602 #endif /* STORAGE_H */