Thu, 12 Jun 2008 13:50:55 -0700
Merge
1 /*
2 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 // A space is an abstraction for the "storage units" backing
26 // up the generation abstraction. It includes specific
27 // implementations for keeping track of free and used space,
28 // for iterating over objects and free blocks, etc.
30 // Here's the Space hierarchy:
31 //
32 // - Space -- an asbtract base class describing a heap area
33 // - CompactibleSpace -- a space supporting compaction
34 // - CompactibleFreeListSpace -- (used for CMS generation)
35 // - ContiguousSpace -- a compactible space in which all free space
36 // is contiguous
37 // - EdenSpace -- contiguous space used as nursery
38 // - ConcEdenSpace -- contiguous space with a 'soft end safe' allocation
39 // - OffsetTableContigSpace -- contiguous space with a block offset array
40 // that allows "fast" block_start calls
41 // - TenuredSpace -- (used for TenuredGeneration)
42 // - ContigPermSpace -- an offset table contiguous space for perm gen
44 // Forward decls.
45 class Space;
46 class BlockOffsetArray;
47 class BlockOffsetArrayContigSpace;
48 class Generation;
49 class CompactibleSpace;
50 class BlockOffsetTable;
51 class GenRemSet;
52 class CardTableRS;
53 class DirtyCardToOopClosure;
55 // An oop closure that is circumscribed by a filtering memory region.
56 class SpaceMemRegionOopsIterClosure: public OopClosure {
57 private:
58 OopClosure* _cl;
59 MemRegion _mr;
60 protected:
61 template <class T> void do_oop_work(T* p) {
62 if (_mr.contains(p)) {
63 _cl->do_oop(p);
64 }
65 }
66 public:
67 SpaceMemRegionOopsIterClosure(OopClosure* cl, MemRegion mr):
68 _cl(cl), _mr(mr) {}
69 virtual void do_oop(oop* p);
70 virtual void do_oop(narrowOop* p);
71 };
73 // A Space describes a heap area. Class Space is an abstract
74 // base class.
75 //
76 // Space supports allocation, size computation and GC support is provided.
77 //
78 // Invariant: bottom() and end() are on page_size boundaries and
79 // bottom() <= top() <= end()
80 // top() is inclusive and end() is exclusive.
82 class Space: public CHeapObj {
83 friend class VMStructs;
84 protected:
85 HeapWord* _bottom;
86 HeapWord* _end;
88 // Used in support of save_marks()
89 HeapWord* _saved_mark_word;
91 MemRegionClosure* _preconsumptionDirtyCardClosure;
93 // A sequential tasks done structure. This supports
94 // parallel GC, where we have threads dynamically
95 // claiming sub-tasks from a larger parallel task.
96 SequentialSubTasksDone _par_seq_tasks;
98 Space():
99 _bottom(NULL), _end(NULL), _preconsumptionDirtyCardClosure(NULL) { }
101 public:
102 // Accessors
103 HeapWord* bottom() const { return _bottom; }
104 HeapWord* end() const { return _end; }
105 virtual void set_bottom(HeapWord* value) { _bottom = value; }
106 virtual void set_end(HeapWord* value) { _end = value; }
108 virtual HeapWord* saved_mark_word() const { return _saved_mark_word; }
109 void set_saved_mark_word(HeapWord* p) { _saved_mark_word = p; }
111 MemRegionClosure* preconsumptionDirtyCardClosure() const {
112 return _preconsumptionDirtyCardClosure;
113 }
114 void setPreconsumptionDirtyCardClosure(MemRegionClosure* cl) {
115 _preconsumptionDirtyCardClosure = cl;
116 }
118 // Returns a subregion of the space containing all the objects in
119 // the space.
120 virtual MemRegion used_region() const { return MemRegion(bottom(), end()); }
122 // Returns a region that is guaranteed to contain (at least) all objects
123 // allocated at the time of the last call to "save_marks". If the space
124 // initializes its DirtyCardToOopClosure's specifying the "contig" option
125 // (that is, if the space is contiguous), then this region must contain only
126 // such objects: the memregion will be from the bottom of the region to the
127 // saved mark. Otherwise, the "obj_allocated_since_save_marks" method of
128 // the space must distiguish between objects in the region allocated before
129 // and after the call to save marks.
130 virtual MemRegion used_region_at_save_marks() const {
131 return MemRegion(bottom(), saved_mark_word());
132 }
134 // Initialization.
135 // "initialize" should be called once on a space, before it is used for
136 // any purpose. The "mr" arguments gives the bounds of the space, and
137 // the "clear_space" argument should be true unless the memory in "mr" is
138 // known to be zeroed.
139 virtual void initialize(MemRegion mr, bool clear_space);
141 // Sets the bounds (bottom and end) of the current space to those of "mr."
142 void set_bounds(MemRegion mr);
144 // The "clear" method must be called on a region that may have
145 // had allocation performed in it, but is now to be considered empty.
146 virtual void clear();
148 // For detecting GC bugs. Should only be called at GC boundaries, since
149 // some unused space may be used as scratch space during GC's.
150 // Default implementation does nothing. We also call this when expanding
151 // a space to satisfy an allocation request. See bug #4668531
152 virtual void mangle_unused_area() {}
153 virtual void mangle_region(MemRegion mr) {}
155 // Testers
156 bool is_empty() const { return used() == 0; }
157 bool not_empty() const { return used() > 0; }
159 // Returns true iff the given the space contains the
160 // given address as part of an allocated object. For
161 // ceratin kinds of spaces, this might be a potentially
162 // expensive operation. To prevent performance problems
163 // on account of its inadvertent use in product jvm's,
164 // we restrict its use to assertion checks only.
165 virtual bool is_in(const void* p) const;
167 // Returns true iff the given reserved memory of the space contains the
168 // given address.
169 bool is_in_reserved(const void* p) const { return _bottom <= p && p < _end; }
171 // Returns true iff the given block is not allocated.
172 virtual bool is_free_block(const HeapWord* p) const = 0;
174 // Test whether p is double-aligned
175 static bool is_aligned(void* p) {
176 return ((intptr_t)p & (sizeof(double)-1)) == 0;
177 }
179 // Size computations. Sizes are in bytes.
180 size_t capacity() const { return byte_size(bottom(), end()); }
181 virtual size_t used() const = 0;
182 virtual size_t free() const = 0;
184 // Iterate over all the ref-containing fields of all objects in the
185 // space, calling "cl.do_oop" on each. Fields in objects allocated by
186 // applications of the closure are not included in the iteration.
187 virtual void oop_iterate(OopClosure* cl);
189 // Same as above, restricted to the intersection of a memory region and
190 // the space. Fields in objects allocated by applications of the closure
191 // are not included in the iteration.
192 virtual void oop_iterate(MemRegion mr, OopClosure* cl) = 0;
194 // Iterate over all objects in the space, calling "cl.do_object" on
195 // each. Objects allocated by applications of the closure are not
196 // included in the iteration.
197 virtual void object_iterate(ObjectClosure* blk) = 0;
199 // Iterate over all objects that intersect with mr, calling "cl->do_object"
200 // on each. There is an exception to this: if this closure has already
201 // been invoked on an object, it may skip such objects in some cases. This is
202 // Most likely to happen in an "upwards" (ascending address) iteration of
203 // MemRegions.
204 virtual void object_iterate_mem(MemRegion mr, UpwardsObjectClosure* cl);
206 // Iterate over as many initialized objects in the space as possible,
207 // calling "cl.do_object_careful" on each. Return NULL if all objects
208 // in the space (at the start of the iteration) were iterated over.
209 // Return an address indicating the extent of the iteration in the
210 // event that the iteration had to return because of finding an
211 // uninitialized object in the space, or if the closure "cl"
212 // signalled early termination.
213 virtual HeapWord* object_iterate_careful(ObjectClosureCareful* cl);
214 virtual HeapWord* object_iterate_careful_m(MemRegion mr,
215 ObjectClosureCareful* cl);
217 // Create and return a new dirty card to oop closure. Can be
218 // overriden to return the appropriate type of closure
219 // depending on the type of space in which the closure will
220 // operate. ResourceArea allocated.
221 virtual DirtyCardToOopClosure* new_dcto_cl(OopClosure* cl,
222 CardTableModRefBS::PrecisionStyle precision,
223 HeapWord* boundary = NULL);
225 // If "p" is in the space, returns the address of the start of the
226 // "block" that contains "p". We say "block" instead of "object" since
227 // some heaps may not pack objects densely; a chunk may either be an
228 // object or a non-object. If "p" is not in the space, return NULL.
229 virtual HeapWord* block_start_const(const void* p) const = 0;
231 // The non-const version may have benevolent side effects on the data
232 // structure supporting these calls, possibly speeding up future calls.
233 // The default implementation, however, is simply to call the const
234 // version.
235 inline virtual HeapWord* block_start(const void* p);
237 // Requires "addr" to be the start of a chunk, and returns its size.
238 // "addr + size" is required to be the start of a new chunk, or the end
239 // of the active area of the heap.
240 virtual size_t block_size(const HeapWord* addr) const = 0;
242 // Requires "addr" to be the start of a block, and returns "TRUE" iff
243 // the block is an object.
244 virtual bool block_is_obj(const HeapWord* addr) const = 0;
246 // Requires "addr" to be the start of a block, and returns "TRUE" iff
247 // the block is an object and the object is alive.
248 virtual bool obj_is_alive(const HeapWord* addr) const;
250 // Allocation (return NULL if full). Assumes the caller has established
251 // mutually exclusive access to the space.
252 virtual HeapWord* allocate(size_t word_size) = 0;
254 // Allocation (return NULL if full). Enforces mutual exclusion internally.
255 virtual HeapWord* par_allocate(size_t word_size) = 0;
257 // Returns true if this object has been allocated since a
258 // generation's "save_marks" call.
259 virtual bool obj_allocated_since_save_marks(const oop obj) const = 0;
261 // Mark-sweep-compact support: all spaces can update pointers to objects
262 // moving as a part of compaction.
263 virtual void adjust_pointers();
265 // PrintHeapAtGC support
266 virtual void print() const;
267 virtual void print_on(outputStream* st) const;
268 virtual void print_short() const;
269 virtual void print_short_on(outputStream* st) const;
272 // Accessor for parallel sequential tasks.
273 SequentialSubTasksDone* par_seq_tasks() { return &_par_seq_tasks; }
275 // IF "this" is a ContiguousSpace, return it, else return NULL.
276 virtual ContiguousSpace* toContiguousSpace() {
277 return NULL;
278 }
280 // Debugging
281 virtual void verify(bool allow_dirty) const = 0;
282 };
284 // A MemRegionClosure (ResourceObj) whose "do_MemRegion" function applies an
285 // OopClosure to (the addresses of) all the ref-containing fields that could
286 // be modified by virtue of the given MemRegion being dirty. (Note that
287 // because of the imprecise nature of the write barrier, this may iterate
288 // over oops beyond the region.)
289 // This base type for dirty card to oop closures handles memory regions
290 // in non-contiguous spaces with no boundaries, and should be sub-classed
291 // to support other space types. See ContiguousDCTOC for a sub-class
292 // that works with ContiguousSpaces.
294 class DirtyCardToOopClosure: public MemRegionClosureRO {
295 protected:
296 OopClosure* _cl;
297 Space* _sp;
298 CardTableModRefBS::PrecisionStyle _precision;
299 HeapWord* _boundary; // If non-NULL, process only non-NULL oops
300 // pointing below boundary.
301 HeapWord* _min_done; // ObjHeadPreciseArray precision requires
302 // a downwards traversal; this is the
303 // lowest location already done (or,
304 // alternatively, the lowest address that
305 // shouldn't be done again. NULL means infinity.)
306 NOT_PRODUCT(HeapWord* _last_bottom;)
307 NOT_PRODUCT(HeapWord* _last_explicit_min_done;)
309 // Get the actual top of the area on which the closure will
310 // operate, given where the top is assumed to be (the end of the
311 // memory region passed to do_MemRegion) and where the object
312 // at the top is assumed to start. For example, an object may
313 // start at the top but actually extend past the assumed top,
314 // in which case the top becomes the end of the object.
315 virtual HeapWord* get_actual_top(HeapWord* top, HeapWord* top_obj);
317 // Walk the given memory region from bottom to (actual) top
318 // looking for objects and applying the oop closure (_cl) to
319 // them. The base implementation of this treats the area as
320 // blocks, where a block may or may not be an object. Sub-
321 // classes should override this to provide more accurate
322 // or possibly more efficient walking.
323 virtual void walk_mem_region(MemRegion mr, HeapWord* bottom, HeapWord* top);
325 public:
326 DirtyCardToOopClosure(Space* sp, OopClosure* cl,
327 CardTableModRefBS::PrecisionStyle precision,
328 HeapWord* boundary) :
329 _sp(sp), _cl(cl), _precision(precision), _boundary(boundary),
330 _min_done(NULL) {
331 NOT_PRODUCT(_last_bottom = NULL);
332 NOT_PRODUCT(_last_explicit_min_done = NULL);
333 }
335 void do_MemRegion(MemRegion mr);
337 void set_min_done(HeapWord* min_done) {
338 _min_done = min_done;
339 NOT_PRODUCT(_last_explicit_min_done = _min_done);
340 }
341 #ifndef PRODUCT
342 void set_last_bottom(HeapWord* last_bottom) {
343 _last_bottom = last_bottom;
344 }
345 #endif
346 };
348 // A structure to represent a point at which objects are being copied
349 // during compaction.
350 class CompactPoint : public StackObj {
351 public:
352 Generation* gen;
353 CompactibleSpace* space;
354 HeapWord* threshold;
355 CompactPoint(Generation* _gen, CompactibleSpace* _space,
356 HeapWord* _threshold) :
357 gen(_gen), space(_space), threshold(_threshold) {}
358 };
361 // A space that supports compaction operations. This is usually, but not
362 // necessarily, a space that is normally contiguous. But, for example, a
363 // free-list-based space whose normal collection is a mark-sweep without
364 // compaction could still support compaction in full GC's.
366 class CompactibleSpace: public Space {
367 friend class VMStructs;
368 friend class CompactibleFreeListSpace;
369 friend class CompactingPermGenGen;
370 friend class CMSPermGenGen;
371 private:
372 HeapWord* _compaction_top;
373 CompactibleSpace* _next_compaction_space;
375 public:
376 virtual void initialize(MemRegion mr, bool clear_space);
377 virtual void clear();
379 // Used temporarily during a compaction phase to hold the value
380 // top should have when compaction is complete.
381 HeapWord* compaction_top() const { return _compaction_top; }
383 void set_compaction_top(HeapWord* value) {
384 assert(value == NULL || (value >= bottom() && value <= end()),
385 "should point inside space");
386 _compaction_top = value;
387 }
389 // Perform operations on the space needed after a compaction
390 // has been performed.
391 virtual void reset_after_compaction() {}
393 // Returns the next space (in the current generation) to be compacted in
394 // the global compaction order. Also is used to select the next
395 // space into which to compact.
397 virtual CompactibleSpace* next_compaction_space() const {
398 return _next_compaction_space;
399 }
401 void set_next_compaction_space(CompactibleSpace* csp) {
402 _next_compaction_space = csp;
403 }
405 // MarkSweep support phase2
407 // Start the process of compaction of the current space: compute
408 // post-compaction addresses, and insert forwarding pointers. The fields
409 // "cp->gen" and "cp->compaction_space" are the generation and space into
410 // which we are currently compacting. This call updates "cp" as necessary,
411 // and leaves the "compaction_top" of the final value of
412 // "cp->compaction_space" up-to-date. Offset tables may be updated in
413 // this phase as if the final copy had occurred; if so, "cp->threshold"
414 // indicates when the next such action should be taken.
415 virtual void prepare_for_compaction(CompactPoint* cp);
416 // MarkSweep support phase3
417 virtual void adjust_pointers();
418 // MarkSweep support phase4
419 virtual void compact();
421 // The maximum percentage of objects that can be dead in the compacted
422 // live part of a compacted space ("deadwood" support.)
423 virtual int allowed_dead_ratio() const { return 0; };
425 // Some contiguous spaces may maintain some data structures that should
426 // be updated whenever an allocation crosses a boundary. This function
427 // returns the first such boundary.
428 // (The default implementation returns the end of the space, so the
429 // boundary is never crossed.)
430 virtual HeapWord* initialize_threshold() { return end(); }
432 // "q" is an object of the given "size" that should be forwarded;
433 // "cp" names the generation ("gen") and containing "this" (which must
434 // also equal "cp->space"). "compact_top" is where in "this" the
435 // next object should be forwarded to. If there is room in "this" for
436 // the object, insert an appropriate forwarding pointer in "q".
437 // If not, go to the next compaction space (there must
438 // be one, since compaction must succeed -- we go to the first space of
439 // the previous generation if necessary, updating "cp"), reset compact_top
440 // and then forward. In either case, returns the new value of "compact_top".
441 // If the forwarding crosses "cp->threshold", invokes the "cross_threhold"
442 // function of the then-current compaction space, and updates "cp->threshold
443 // accordingly".
444 virtual HeapWord* forward(oop q, size_t size, CompactPoint* cp,
445 HeapWord* compact_top);
447 // Return a size with adjusments as required of the space.
448 virtual size_t adjust_object_size_v(size_t size) const { return size; }
450 protected:
451 // Used during compaction.
452 HeapWord* _first_dead;
453 HeapWord* _end_of_live;
455 // Minimum size of a free block.
456 virtual size_t minimum_free_block_size() const = 0;
458 // This the function is invoked when an allocation of an object covering
459 // "start" to "end occurs crosses the threshold; returns the next
460 // threshold. (The default implementation does nothing.)
461 virtual HeapWord* cross_threshold(HeapWord* start, HeapWord* the_end) {
462 return end();
463 }
465 // Requires "allowed_deadspace_words > 0", that "q" is the start of a
466 // free block of the given "word_len", and that "q", were it an object,
467 // would not move if forwared. If the size allows, fill the free
468 // block with an object, to prevent excessive compaction. Returns "true"
469 // iff the free region was made deadspace, and modifies
470 // "allowed_deadspace_words" to reflect the number of available deadspace
471 // words remaining after this operation.
472 bool insert_deadspace(size_t& allowed_deadspace_words, HeapWord* q,
473 size_t word_len);
474 };
476 #define SCAN_AND_FORWARD(cp,scan_limit,block_is_obj,block_size) { \
477 /* Compute the new addresses for the live objects and store it in the mark \
478 * Used by universe::mark_sweep_phase2() \
479 */ \
480 HeapWord* compact_top; /* This is where we are currently compacting to. */ \
481 \
482 /* We're sure to be here before any objects are compacted into this \
483 * space, so this is a good time to initialize this: \
484 */ \
485 set_compaction_top(bottom()); \
486 \
487 if (cp->space == NULL) { \
488 assert(cp->gen != NULL, "need a generation"); \
489 assert(cp->threshold == NULL, "just checking"); \
490 assert(cp->gen->first_compaction_space() == this, "just checking"); \
491 cp->space = cp->gen->first_compaction_space(); \
492 compact_top = cp->space->bottom(); \
493 cp->space->set_compaction_top(compact_top); \
494 cp->threshold = cp->space->initialize_threshold(); \
495 } else { \
496 compact_top = cp->space->compaction_top(); \
497 } \
498 \
499 /* We allow some amount of garbage towards the bottom of the space, so \
500 * we don't start compacting before there is a significant gain to be made.\
501 * Occasionally, we want to ensure a full compaction, which is determined \
502 * by the MarkSweepAlwaysCompactCount parameter. \
503 */ \
504 int invocations = SharedHeap::heap()->perm_gen()->stat_record()->invocations;\
505 bool skip_dead = ((invocations % MarkSweepAlwaysCompactCount) != 0); \
506 \
507 size_t allowed_deadspace = 0; \
508 if (skip_dead) { \
509 int ratio = allowed_dead_ratio(); \
510 allowed_deadspace = (capacity() * ratio / 100) / HeapWordSize; \
511 } \
512 \
513 HeapWord* q = bottom(); \
514 HeapWord* t = scan_limit(); \
515 \
516 HeapWord* end_of_live= q; /* One byte beyond the last byte of the last \
517 live object. */ \
518 HeapWord* first_dead = end();/* The first dead object. */ \
519 LiveRange* liveRange = NULL; /* The current live range, recorded in the \
520 first header of preceding free area. */ \
521 _first_dead = first_dead; \
522 \
523 const intx interval = PrefetchScanIntervalInBytes; \
524 \
525 while (q < t) { \
526 assert(!block_is_obj(q) || \
527 oop(q)->mark()->is_marked() || oop(q)->mark()->is_unlocked() || \
528 oop(q)->mark()->has_bias_pattern(), \
529 "these are the only valid states during a mark sweep"); \
530 if (block_is_obj(q) && oop(q)->is_gc_marked()) { \
531 /* prefetch beyond q */ \
532 Prefetch::write(q, interval); \
533 /* size_t size = oop(q)->size(); changing this for cms for perm gen */\
534 size_t size = block_size(q); \
535 compact_top = cp->space->forward(oop(q), size, cp, compact_top); \
536 q += size; \
537 end_of_live = q; \
538 } else { \
539 /* run over all the contiguous dead objects */ \
540 HeapWord* end = q; \
541 do { \
542 /* prefetch beyond end */ \
543 Prefetch::write(end, interval); \
544 end += block_size(end); \
545 } while (end < t && (!block_is_obj(end) || !oop(end)->is_gc_marked()));\
546 \
547 /* see if we might want to pretend this object is alive so that \
548 * we don't have to compact quite as often. \
549 */ \
550 if (allowed_deadspace > 0 && q == compact_top) { \
551 size_t sz = pointer_delta(end, q); \
552 if (insert_deadspace(allowed_deadspace, q, sz)) { \
553 compact_top = cp->space->forward(oop(q), sz, cp, compact_top); \
554 q = end; \
555 end_of_live = end; \
556 continue; \
557 } \
558 } \
559 \
560 /* otherwise, it really is a free region. */ \
561 \
562 /* for the previous LiveRange, record the end of the live objects. */ \
563 if (liveRange) { \
564 liveRange->set_end(q); \
565 } \
566 \
567 /* record the current LiveRange object. \
568 * liveRange->start() is overlaid on the mark word. \
569 */ \
570 liveRange = (LiveRange*)q; \
571 liveRange->set_start(end); \
572 liveRange->set_end(end); \
573 \
574 /* see if this is the first dead region. */ \
575 if (q < first_dead) { \
576 first_dead = q; \
577 } \
578 \
579 /* move on to the next object */ \
580 q = end; \
581 } \
582 } \
583 \
584 assert(q == t, "just checking"); \
585 if (liveRange != NULL) { \
586 liveRange->set_end(q); \
587 } \
588 _end_of_live = end_of_live; \
589 if (end_of_live < first_dead) { \
590 first_dead = end_of_live; \
591 } \
592 _first_dead = first_dead; \
593 \
594 /* save the compaction_top of the compaction space. */ \
595 cp->space->set_compaction_top(compact_top); \
596 }
598 #define SCAN_AND_ADJUST_POINTERS(adjust_obj_size) { \
599 /* adjust all the interior pointers to point at the new locations of objects \
600 * Used by MarkSweep::mark_sweep_phase3() */ \
601 \
602 HeapWord* q = bottom(); \
603 HeapWord* t = _end_of_live; /* Established by "prepare_for_compaction". */ \
604 \
605 assert(_first_dead <= _end_of_live, "Stands to reason, no?"); \
606 \
607 if (q < t && _first_dead > q && \
608 !oop(q)->is_gc_marked()) { \
609 /* we have a chunk of the space which hasn't moved and we've \
610 * reinitialized the mark word during the previous pass, so we can't \
611 * use is_gc_marked for the traversal. */ \
612 HeapWord* end = _first_dead; \
613 \
614 while (q < end) { \
615 /* I originally tried to conjoin "block_start(q) == q" to the \
616 * assertion below, but that doesn't work, because you can't \
617 * accurately traverse previous objects to get to the current one \
618 * after their pointers (including pointers into permGen) have been \
619 * updated, until the actual compaction is done. dld, 4/00 */ \
620 assert(block_is_obj(q), \
621 "should be at block boundaries, and should be looking at objs"); \
622 \
623 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::track_interior_pointers(oop(q))); \
624 \
625 /* point all the oops to the new location */ \
626 size_t size = oop(q)->adjust_pointers(); \
627 size = adjust_obj_size(size); \
628 \
629 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::check_interior_pointers()); \
630 \
631 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::validate_live_oop(oop(q), size)); \
632 \
633 q += size; \
634 } \
635 \
636 if (_first_dead == t) { \
637 q = t; \
638 } else { \
639 /* $$$ This is funky. Using this to read the previously written \
640 * LiveRange. See also use below. */ \
641 q = (HeapWord*)oop(_first_dead)->mark()->decode_pointer(); \
642 } \
643 } \
644 \
645 const intx interval = PrefetchScanIntervalInBytes; \
646 \
647 debug_only(HeapWord* prev_q = NULL); \
648 while (q < t) { \
649 /* prefetch beyond q */ \
650 Prefetch::write(q, interval); \
651 if (oop(q)->is_gc_marked()) { \
652 /* q is alive */ \
653 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::track_interior_pointers(oop(q))); \
654 /* point all the oops to the new location */ \
655 size_t size = oop(q)->adjust_pointers(); \
656 size = adjust_obj_size(size); \
657 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::check_interior_pointers()); \
658 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::validate_live_oop(oop(q), size)); \
659 debug_only(prev_q = q); \
660 q += size; \
661 } else { \
662 /* q is not a live object, so its mark should point at the next \
663 * live object */ \
664 debug_only(prev_q = q); \
665 q = (HeapWord*) oop(q)->mark()->decode_pointer(); \
666 assert(q > prev_q, "we should be moving forward through memory"); \
667 } \
668 } \
669 \
670 assert(q == t, "just checking"); \
671 }
673 #define SCAN_AND_COMPACT(obj_size) { \
674 /* Copy all live objects to their new location \
675 * Used by MarkSweep::mark_sweep_phase4() */ \
676 \
677 HeapWord* q = bottom(); \
678 HeapWord* const t = _end_of_live; \
679 debug_only(HeapWord* prev_q = NULL); \
680 \
681 if (q < t && _first_dead > q && \
682 !oop(q)->is_gc_marked()) { \
683 debug_only( \
684 /* we have a chunk of the space which hasn't moved and we've reinitialized \
685 * the mark word during the previous pass, so we can't use is_gc_marked for \
686 * the traversal. */ \
687 HeapWord* const end = _first_dead; \
688 \
689 while (q < end) { \
690 size_t size = obj_size(q); \
691 assert(!oop(q)->is_gc_marked(), \
692 "should be unmarked (special dense prefix handling)"); \
693 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::live_oop_moved_to(q, size, q)); \
694 debug_only(prev_q = q); \
695 q += size; \
696 } \
697 ) /* debug_only */ \
698 \
699 if (_first_dead == t) { \
700 q = t; \
701 } else { \
702 /* $$$ Funky */ \
703 q = (HeapWord*) oop(_first_dead)->mark()->decode_pointer(); \
704 } \
705 } \
706 \
707 const intx scan_interval = PrefetchScanIntervalInBytes; \
708 const intx copy_interval = PrefetchCopyIntervalInBytes; \
709 while (q < t) { \
710 if (!oop(q)->is_gc_marked()) { \
711 /* mark is pointer to next marked oop */ \
712 debug_only(prev_q = q); \
713 q = (HeapWord*) oop(q)->mark()->decode_pointer(); \
714 assert(q > prev_q, "we should be moving forward through memory"); \
715 } else { \
716 /* prefetch beyond q */ \
717 Prefetch::read(q, scan_interval); \
718 \
719 /* size and destination */ \
720 size_t size = obj_size(q); \
721 HeapWord* compaction_top = (HeapWord*)oop(q)->forwardee(); \
722 \
723 /* prefetch beyond compaction_top */ \
724 Prefetch::write(compaction_top, copy_interval); \
725 \
726 /* copy object and reinit its mark */ \
727 VALIDATE_MARK_SWEEP_ONLY(MarkSweep::live_oop_moved_to(q, size, \
728 compaction_top)); \
729 assert(q != compaction_top, "everything in this pass should be moving"); \
730 Copy::aligned_conjoint_words(q, compaction_top, size); \
731 oop(compaction_top)->init_mark(); \
732 assert(oop(compaction_top)->klass() != NULL, "should have a class"); \
733 \
734 debug_only(prev_q = q); \
735 q += size; \
736 } \
737 } \
738 \
739 /* Let's remember if we were empty before we did the compaction. */ \
740 bool was_empty = used_region().is_empty(); \
741 /* Reset space after compaction is complete */ \
742 reset_after_compaction(); \
743 /* We do this clear, below, since it has overloaded meanings for some */ \
744 /* space subtypes. For example, OffsetTableContigSpace's that were */ \
745 /* compacted into will have had their offset table thresholds updated */ \
746 /* continuously, but those that weren't need to have their thresholds */ \
747 /* re-initialized. Also mangles unused area for debugging. */ \
748 if (used_region().is_empty()) { \
749 if (!was_empty) clear(); \
750 } else { \
751 if (ZapUnusedHeapArea) mangle_unused_area(); \
752 } \
753 }
755 // A space in which the free area is contiguous. It therefore supports
756 // faster allocation, and compaction.
757 class ContiguousSpace: public CompactibleSpace {
758 friend class OneContigSpaceCardGeneration;
759 friend class VMStructs;
760 protected:
761 HeapWord* _top;
762 HeapWord* _concurrent_iteration_safe_limit;
764 // Allocation helpers (return NULL if full).
765 inline HeapWord* allocate_impl(size_t word_size, HeapWord* end_value);
766 inline HeapWord* par_allocate_impl(size_t word_size, HeapWord* end_value);
768 public:
769 virtual void initialize(MemRegion mr, bool clear_space);
771 // Accessors
772 HeapWord* top() const { return _top; }
773 void set_top(HeapWord* value) { _top = value; }
775 virtual void set_saved_mark() { _saved_mark_word = top(); }
776 void reset_saved_mark() { _saved_mark_word = bottom(); }
778 virtual void clear();
780 WaterMark bottom_mark() { return WaterMark(this, bottom()); }
781 WaterMark top_mark() { return WaterMark(this, top()); }
782 WaterMark saved_mark() { return WaterMark(this, saved_mark_word()); }
783 bool saved_mark_at_top() const { return saved_mark_word() == top(); }
785 void mangle_unused_area();
786 void mangle_region(MemRegion mr);
788 // Size computations: sizes in bytes.
789 size_t capacity() const { return byte_size(bottom(), end()); }
790 size_t used() const { return byte_size(bottom(), top()); }
791 size_t free() const { return byte_size(top(), end()); }
793 // Override from space.
794 bool is_in(const void* p) const;
796 virtual bool is_free_block(const HeapWord* p) const;
798 // In a contiguous space we have a more obvious bound on what parts
799 // contain objects.
800 MemRegion used_region() const { return MemRegion(bottom(), top()); }
802 MemRegion used_region_at_save_marks() const {
803 return MemRegion(bottom(), saved_mark_word());
804 }
806 // Allocation (return NULL if full)
807 virtual HeapWord* allocate(size_t word_size);
808 virtual HeapWord* par_allocate(size_t word_size);
810 virtual bool obj_allocated_since_save_marks(const oop obj) const {
811 return (HeapWord*)obj >= saved_mark_word();
812 }
814 // Iteration
815 void oop_iterate(OopClosure* cl);
816 void oop_iterate(MemRegion mr, OopClosure* cl);
817 void object_iterate(ObjectClosure* blk);
818 void object_iterate_mem(MemRegion mr, UpwardsObjectClosure* cl);
819 // iterates on objects up to the safe limit
820 HeapWord* object_iterate_careful(ObjectClosureCareful* cl);
821 inline HeapWord* concurrent_iteration_safe_limit();
822 // changes the safe limit, all objects from bottom() to the new
823 // limit should be properly initialized
824 inline void set_concurrent_iteration_safe_limit(HeapWord* new_limit);
826 #ifndef SERIALGC
827 // In support of parallel oop_iterate.
828 #define ContigSpace_PAR_OOP_ITERATE_DECL(OopClosureType, nv_suffix) \
829 void par_oop_iterate(MemRegion mr, OopClosureType* blk);
831 ALL_PAR_OOP_ITERATE_CLOSURES(ContigSpace_PAR_OOP_ITERATE_DECL)
832 #undef ContigSpace_PAR_OOP_ITERATE_DECL
833 #endif // SERIALGC
835 // Compaction support
836 virtual void reset_after_compaction() {
837 assert(compaction_top() >= bottom() && compaction_top() <= end(), "should point inside space");
838 set_top(compaction_top());
839 // set new iteration safe limit
840 set_concurrent_iteration_safe_limit(compaction_top());
841 }
842 virtual size_t minimum_free_block_size() const { return 0; }
844 // Override.
845 DirtyCardToOopClosure* new_dcto_cl(OopClosure* cl,
846 CardTableModRefBS::PrecisionStyle precision,
847 HeapWord* boundary = NULL);
849 // Apply "blk->do_oop" to the addresses of all reference fields in objects
850 // starting with the _saved_mark_word, which was noted during a generation's
851 // save_marks and is required to denote the head of an object.
852 // Fields in objects allocated by applications of the closure
853 // *are* included in the iteration.
854 // Updates _saved_mark_word to point to just after the last object
855 // iterated over.
856 #define ContigSpace_OOP_SINCE_SAVE_MARKS_DECL(OopClosureType, nv_suffix) \
857 void oop_since_save_marks_iterate##nv_suffix(OopClosureType* blk);
859 ALL_SINCE_SAVE_MARKS_CLOSURES(ContigSpace_OOP_SINCE_SAVE_MARKS_DECL)
860 #undef ContigSpace_OOP_SINCE_SAVE_MARKS_DECL
862 // Same as object_iterate, but starting from "mark", which is required
863 // to denote the start of an object. Objects allocated by
864 // applications of the closure *are* included in the iteration.
865 virtual void object_iterate_from(WaterMark mark, ObjectClosure* blk);
867 // Very inefficient implementation.
868 virtual HeapWord* block_start_const(const void* p) const;
869 size_t block_size(const HeapWord* p) const;
870 // If a block is in the allocated area, it is an object.
871 bool block_is_obj(const HeapWord* p) const { return p < top(); }
873 // Addresses for inlined allocation
874 HeapWord** top_addr() { return &_top; }
875 HeapWord** end_addr() { return &_end; }
877 // Overrides for more efficient compaction support.
878 void prepare_for_compaction(CompactPoint* cp);
880 // PrintHeapAtGC support.
881 virtual void print_on(outputStream* st) const;
883 // Checked dynamic downcasts.
884 virtual ContiguousSpace* toContiguousSpace() {
885 return this;
886 }
888 // Debugging
889 virtual void verify(bool allow_dirty) const;
891 // Used to increase collection frequency. "factor" of 0 means entire
892 // space.
893 void allocate_temporary_filler(int factor);
895 };
898 // A dirty card to oop closure that does filtering.
899 // It knows how to filter out objects that are outside of the _boundary.
900 class Filtering_DCTOC : public DirtyCardToOopClosure {
901 protected:
902 // Override.
903 void walk_mem_region(MemRegion mr,
904 HeapWord* bottom, HeapWord* top);
906 // Walk the given memory region, from bottom to top, applying
907 // the given oop closure to (possibly) all objects found. The
908 // given oop closure may or may not be the same as the oop
909 // closure with which this closure was created, as it may
910 // be a filtering closure which makes use of the _boundary.
911 // We offer two signatures, so the FilteringClosure static type is
912 // apparent.
913 virtual void walk_mem_region_with_cl(MemRegion mr,
914 HeapWord* bottom, HeapWord* top,
915 OopClosure* cl) = 0;
916 virtual void walk_mem_region_with_cl(MemRegion mr,
917 HeapWord* bottom, HeapWord* top,
918 FilteringClosure* cl) = 0;
920 public:
921 Filtering_DCTOC(Space* sp, OopClosure* cl,
922 CardTableModRefBS::PrecisionStyle precision,
923 HeapWord* boundary) :
924 DirtyCardToOopClosure(sp, cl, precision, boundary) {}
925 };
927 // A dirty card to oop closure for contiguous spaces
928 // (ContiguousSpace and sub-classes).
929 // It is a FilteringClosure, as defined above, and it knows:
930 //
931 // 1. That the actual top of any area in a memory region
932 // contained by the space is bounded by the end of the contiguous
933 // region of the space.
934 // 2. That the space is really made up of objects and not just
935 // blocks.
937 class ContiguousSpaceDCTOC : public Filtering_DCTOC {
938 protected:
939 // Overrides.
940 HeapWord* get_actual_top(HeapWord* top, HeapWord* top_obj);
942 virtual void walk_mem_region_with_cl(MemRegion mr,
943 HeapWord* bottom, HeapWord* top,
944 OopClosure* cl);
945 virtual void walk_mem_region_with_cl(MemRegion mr,
946 HeapWord* bottom, HeapWord* top,
947 FilteringClosure* cl);
949 public:
950 ContiguousSpaceDCTOC(ContiguousSpace* sp, OopClosure* cl,
951 CardTableModRefBS::PrecisionStyle precision,
952 HeapWord* boundary) :
953 Filtering_DCTOC(sp, cl, precision, boundary)
954 {}
955 };
958 // Class EdenSpace describes eden-space in new generation.
960 class DefNewGeneration;
962 class EdenSpace : public ContiguousSpace {
963 friend class VMStructs;
964 private:
965 DefNewGeneration* _gen;
967 // _soft_end is used as a soft limit on allocation. As soft limits are
968 // reached, the slow-path allocation code can invoke other actions and then
969 // adjust _soft_end up to a new soft limit or to end().
970 HeapWord* _soft_end;
972 public:
973 EdenSpace(DefNewGeneration* gen) : _gen(gen) { _soft_end = NULL; }
975 // Get/set just the 'soft' limit.
976 HeapWord* soft_end() { return _soft_end; }
977 HeapWord** soft_end_addr() { return &_soft_end; }
978 void set_soft_end(HeapWord* value) { _soft_end = value; }
980 // Override.
981 void clear();
983 // Set both the 'hard' and 'soft' limits (_end and _soft_end).
984 void set_end(HeapWord* value) {
985 set_soft_end(value);
986 ContiguousSpace::set_end(value);
987 }
989 // Allocation (return NULL if full)
990 HeapWord* allocate(size_t word_size);
991 HeapWord* par_allocate(size_t word_size);
992 };
994 // Class ConcEdenSpace extends EdenSpace for the sake of safe
995 // allocation while soft-end is being modified concurrently
997 class ConcEdenSpace : public EdenSpace {
998 public:
999 ConcEdenSpace(DefNewGeneration* gen) : EdenSpace(gen) { }
1001 // Allocation (return NULL if full)
1002 HeapWord* par_allocate(size_t word_size);
1003 };
1006 // A ContigSpace that Supports an efficient "block_start" operation via
1007 // a BlockOffsetArray (whose BlockOffsetSharedArray may be shared with
1008 // other spaces.) This is the abstract base class for old generation
1009 // (tenured, perm) spaces.
1011 class OffsetTableContigSpace: public ContiguousSpace {
1012 friend class VMStructs;
1013 protected:
1014 BlockOffsetArrayContigSpace _offsets;
1015 Mutex _par_alloc_lock;
1017 public:
1018 // Constructor
1019 OffsetTableContigSpace(BlockOffsetSharedArray* sharedOffsetArray,
1020 MemRegion mr);
1022 void set_bottom(HeapWord* value);
1023 void set_end(HeapWord* value);
1025 virtual void initialize(MemRegion mr, bool clear_space);
1026 void clear();
1028 inline HeapWord* block_start_const(const void* p) const;
1030 // Add offset table update.
1031 virtual inline HeapWord* allocate(size_t word_size);
1032 inline HeapWord* par_allocate(size_t word_size);
1034 // MarkSweep support phase3
1035 virtual HeapWord* initialize_threshold();
1036 virtual HeapWord* cross_threshold(HeapWord* start, HeapWord* end);
1038 virtual void print_on(outputStream* st) const;
1040 // Debugging
1041 void verify(bool allow_dirty) const;
1043 // Shared space support
1044 void serialize_block_offset_array_offsets(SerializeOopClosure* soc);
1045 };
1048 // Class TenuredSpace is used by TenuredGeneration
1050 class TenuredSpace: public OffsetTableContigSpace {
1051 friend class VMStructs;
1052 protected:
1053 // Mark sweep support
1054 int allowed_dead_ratio() const;
1055 public:
1056 // Constructor
1057 TenuredSpace(BlockOffsetSharedArray* sharedOffsetArray,
1058 MemRegion mr) :
1059 OffsetTableContigSpace(sharedOffsetArray, mr) {}
1060 };
1063 // Class ContigPermSpace is used by CompactingPermGen
1065 class ContigPermSpace: public OffsetTableContigSpace {
1066 friend class VMStructs;
1067 protected:
1068 // Mark sweep support
1069 int allowed_dead_ratio() const;
1070 public:
1071 // Constructor
1072 ContigPermSpace(BlockOffsetSharedArray* sharedOffsetArray, MemRegion mr) :
1073 OffsetTableContigSpace(sharedOffsetArray, mr) {}
1074 };