space.hpp revision 13249:a2753984d2c1
1174294Sobrien/*
2131702Smbr * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3174294Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4174294Sobrien *
5174294Sobrien * This code is free software; you can redistribute it and/or modify it
6131702Smbr * under the terms of the GNU General Public License version 2 only, as
7174294Sobrien * published by the Free Software Foundation.
8131702Smbr *
9174294Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
10174294Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11131702Smbr * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12174294Sobrien * version 2 for more details (a copy is included in the LICENSE file that
13174294Sobrien * accompanied this code).
14131702Smbr *
15174294Sobrien * You should have received a copy of the GNU General Public License version
16174294Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
17131702Smbr * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18174294Sobrien *
19131702Smbr * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20174294Sobrien * or visit www.oracle.com if you need additional information or have any
21174294Sobrien * questions.
22174294Sobrien *
23131702Smbr */
24174294Sobrien
25131702Smbr#ifndef SHARE_VM_GC_SHARED_SPACE_HPP
26174294Sobrien#define SHARE_VM_GC_SHARED_SPACE_HPP
27174294Sobrien
28174294Sobrien#include "gc/shared/blockOffsetTable.hpp"
29174294Sobrien#include "gc/shared/cardTableModRefBS.hpp"
30174294Sobrien#include "gc/shared/workgroup.hpp"
31174294Sobrien#include "memory/allocation.hpp"
32174294Sobrien#include "memory/iterator.hpp"
33174294Sobrien#include "memory/memRegion.hpp"
34174294Sobrien#include "oops/markOop.hpp"
35174294Sobrien#include "runtime/mutexLocker.hpp"
36174294Sobrien#include "utilities/align.hpp"
37174294Sobrien#include "utilities/macros.hpp"
38174294Sobrien
39174294Sobrien// A space is an abstraction for the "storage units" backing
40174294Sobrien// up the generation abstraction. It includes specific
41174294Sobrien// implementations for keeping track of free and used space,
42174294Sobrien// for iterating over objects and free blocks, etc.
43174294Sobrien
44174294Sobrien// Forward decls.
45174294Sobrienclass Space;
46174294Sobrienclass BlockOffsetArray;
47174294Sobrienclass BlockOffsetArrayContigSpace;
48174294Sobrienclass Generation;
49174294Sobrienclass CompactibleSpace;
50174294Sobrienclass BlockOffsetTable;
51174294Sobrienclass CardTableRS;
52174294Sobrienclass DirtyCardToOopClosure;
53174294Sobrien
54174294Sobrien// A Space describes a heap area. Class Space is an abstract
55174294Sobrien// base class.
56174294Sobrien//
57174294Sobrien// Space supports allocation, size computation and GC support is provided.
58174294Sobrien//
59174294Sobrien// Invariant: bottom() and end() are on page_size boundaries and
60174294Sobrien// bottom() <= top() <= end()
61174294Sobrien// top() is inclusive and end() is exclusive.
62174294Sobrien
63174294Sobrienclass Space: public CHeapObj<mtGC> {
64174294Sobrien  friend class VMStructs;
65174294Sobrien protected:
66174294Sobrien  HeapWord* _bottom;
67174294Sobrien  HeapWord* _end;
68174294Sobrien
69174294Sobrien  // Used in support of save_marks()
70174294Sobrien  HeapWord* _saved_mark_word;
71174294Sobrien
72174294Sobrien  // A sequential tasks done structure. This supports
73174294Sobrien  // parallel GC, where we have threads dynamically
74174294Sobrien  // claiming sub-tasks from a larger parallel task.
75174294Sobrien  SequentialSubTasksDone _par_seq_tasks;
76174294Sobrien
77174294Sobrien  Space():
78174294Sobrien    _bottom(NULL), _end(NULL) { }
79174294Sobrien
80174294Sobrien public:
81174294Sobrien  // Accessors
82174294Sobrien  HeapWord* bottom() const         { return _bottom; }
83174294Sobrien  HeapWord* end() const            { return _end;    }
84174294Sobrien  virtual void set_bottom(HeapWord* value) { _bottom = value; }
85174294Sobrien  virtual void set_end(HeapWord* value)    { _end = value; }
86174294Sobrien
87174294Sobrien  virtual HeapWord* saved_mark_word() const  { return _saved_mark_word; }
88174294Sobrien
89174294Sobrien  void set_saved_mark_word(HeapWord* p) { _saved_mark_word = p; }
90174294Sobrien
91174294Sobrien  // Returns true if this object has been allocated since a
92174294Sobrien  // generation's "save_marks" call.
93174294Sobrien  virtual bool obj_allocated_since_save_marks(const oop obj) const {
94174294Sobrien    return (HeapWord*)obj >= saved_mark_word();
95174294Sobrien  }
96174294Sobrien
97174294Sobrien  virtual MemRegionClosure* preconsumptionDirtyCardClosure() const {
98174294Sobrien    return NULL;
99174294Sobrien  }
100174294Sobrien
101174294Sobrien  // Returns a subregion of the space containing only the allocated objects in
102174294Sobrien  // the space.
103174294Sobrien  virtual MemRegion used_region() const = 0;
104174294Sobrien
105174294Sobrien  // Returns a region that is guaranteed to contain (at least) all objects
106174294Sobrien  // allocated at the time of the last call to "save_marks".  If the space
107174294Sobrien  // initializes its DirtyCardToOopClosure's specifying the "contig" option
108174294Sobrien  // (that is, if the space is contiguous), then this region must contain only
109174294Sobrien  // such objects: the memregion will be from the bottom of the region to the
110174294Sobrien  // saved mark.  Otherwise, the "obj_allocated_since_save_marks" method of
111174294Sobrien  // the space must distinguish between objects in the region allocated before
112174294Sobrien  // and after the call to save marks.
113174294Sobrien  MemRegion used_region_at_save_marks() const {
114174294Sobrien    return MemRegion(bottom(), saved_mark_word());
115174294Sobrien  }
116174294Sobrien
117174294Sobrien  // Initialization.
118174294Sobrien  // "initialize" should be called once on a space, before it is used for
119174294Sobrien  // any purpose.  The "mr" arguments gives the bounds of the space, and
120174294Sobrien  // the "clear_space" argument should be true unless the memory in "mr" is
121174294Sobrien  // known to be zeroed.
122174294Sobrien  virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space);
123174294Sobrien
124174294Sobrien  // The "clear" method must be called on a region that may have
125174294Sobrien  // had allocation performed in it, but is now to be considered empty.
126174294Sobrien  virtual void clear(bool mangle_space);
127174294Sobrien
128174294Sobrien  // For detecting GC bugs.  Should only be called at GC boundaries, since
129174294Sobrien  // some unused space may be used as scratch space during GC's.
130174294Sobrien  // We also call this when expanding a space to satisfy an allocation
131174294Sobrien  // request. See bug #4668531
132174294Sobrien  virtual void mangle_unused_area() = 0;
133174294Sobrien  virtual void mangle_unused_area_complete() = 0;
134174294Sobrien
135174294Sobrien  // Testers
136174294Sobrien  bool is_empty() const              { return used() == 0; }
137174294Sobrien  bool not_empty() const             { return used() > 0; }
138174294Sobrien
139174294Sobrien  // Returns true iff the given the space contains the
140174294Sobrien  // given address as part of an allocated object. For
141174294Sobrien  // certain kinds of spaces, this might be a potentially
142174294Sobrien  // expensive operation. To prevent performance problems
143174294Sobrien  // on account of its inadvertent use in product jvm's,
144174294Sobrien  // we restrict its use to assertion checks only.
145174294Sobrien  bool is_in(const void* p) const {
146174294Sobrien    return used_region().contains(p);
147174294Sobrien  }
148174294Sobrien
149174294Sobrien  // Returns true iff the given reserved memory of the space contains the
150174294Sobrien  // given address.
151174294Sobrien  bool is_in_reserved(const void* p) const { return _bottom <= p && p < _end; }
152174294Sobrien
153174294Sobrien  // Returns true iff the given block is not allocated.
154174294Sobrien  virtual bool is_free_block(const HeapWord* p) const = 0;
155174294Sobrien
156174294Sobrien  // Test whether p is double-aligned
157174294Sobrien  static bool is_aligned(void* p) {
158174294Sobrien    return ::is_aligned(p, sizeof(double));
159174294Sobrien  }
160174294Sobrien
161174294Sobrien  // Size computations.  Sizes are in bytes.
162174294Sobrien  size_t capacity()     const { return byte_size(bottom(), end()); }
163174294Sobrien  virtual size_t used() const = 0;
164174294Sobrien  virtual size_t free() const = 0;
165174294Sobrien
166174294Sobrien  // Iterate over all the ref-containing fields of all objects in the
167174294Sobrien  // space, calling "cl.do_oop" on each.  Fields in objects allocated by
168174294Sobrien  // applications of the closure are not included in the iteration.
169174294Sobrien  virtual void oop_iterate(ExtendedOopClosure* cl);
170174294Sobrien
171174294Sobrien  // Iterate over all objects in the space, calling "cl.do_object" on
172174294Sobrien  // each.  Objects allocated by applications of the closure are not
173174294Sobrien  // included in the iteration.
174174294Sobrien  virtual void object_iterate(ObjectClosure* blk) = 0;
175174294Sobrien  // Similar to object_iterate() except only iterates over
176174294Sobrien  // objects whose internal references point to objects in the space.
177174294Sobrien  virtual void safe_object_iterate(ObjectClosure* blk) = 0;
178174294Sobrien
179174294Sobrien  // Create and return a new dirty card to oop closure. Can be
180174294Sobrien  // overridden to return the appropriate type of closure
181174294Sobrien  // depending on the type of space in which the closure will
182174294Sobrien  // operate. ResourceArea allocated.
183174294Sobrien  virtual DirtyCardToOopClosure* new_dcto_cl(ExtendedOopClosure* cl,
184174294Sobrien                                             CardTableModRefBS::PrecisionStyle precision,
185174294Sobrien                                             HeapWord* boundary,
186174294Sobrien                                             bool parallel);
187174294Sobrien
188174294Sobrien  // If "p" is in the space, returns the address of the start of the
189174294Sobrien  // "block" that contains "p".  We say "block" instead of "object" since
190174294Sobrien  // some heaps may not pack objects densely; a chunk may either be an
191174294Sobrien  // object or a non-object.  If "p" is not in the space, return NULL.
192174294Sobrien  virtual HeapWord* block_start_const(const void* p) const = 0;
193174294Sobrien
194174294Sobrien  // The non-const version may have benevolent side effects on the data
195174294Sobrien  // structure supporting these calls, possibly speeding up future calls.
196174294Sobrien  // The default implementation, however, is simply to call the const
197174294Sobrien  // version.
198174294Sobrien  virtual HeapWord* block_start(const void* p);
199174294Sobrien
200174294Sobrien  // Requires "addr" to be the start of a chunk, and returns its size.
201174294Sobrien  // "addr + size" is required to be the start of a new chunk, or the end
202174294Sobrien  // of the active area of the heap.
203174294Sobrien  virtual size_t block_size(const HeapWord* addr) const = 0;
204174294Sobrien
205174294Sobrien  // Requires "addr" to be the start of a block, and returns "TRUE" iff
206174294Sobrien  // the block is an object.
207174294Sobrien  virtual bool block_is_obj(const HeapWord* addr) const = 0;
208174294Sobrien
209174294Sobrien  // Requires "addr" to be the start of a block, and returns "TRUE" iff
210174294Sobrien  // the block is an object and the object is alive.
211174294Sobrien  virtual bool obj_is_alive(const HeapWord* addr) const;
212174294Sobrien
213174294Sobrien  // Allocation (return NULL if full).  Assumes the caller has established
214174294Sobrien  // mutually exclusive access to the space.
215174294Sobrien  virtual HeapWord* allocate(size_t word_size) = 0;
216174294Sobrien
217174294Sobrien  // Allocation (return NULL if full).  Enforces mutual exclusion internally.
218174294Sobrien  virtual HeapWord* par_allocate(size_t word_size) = 0;
219174294Sobrien
220174294Sobrien  // Mark-sweep-compact support: all spaces can update pointers to objects
221174294Sobrien  // moving as a part of compaction.
222174294Sobrien  virtual void adjust_pointers() = 0;
223174294Sobrien
224174294Sobrien  virtual void print() const;
225174294Sobrien  virtual void print_on(outputStream* st) const;
226174294Sobrien  virtual void print_short() const;
227174294Sobrien  virtual void print_short_on(outputStream* st) const;
228174294Sobrien
229174294Sobrien
230174294Sobrien  // Accessor for parallel sequential tasks.
231174294Sobrien  SequentialSubTasksDone* par_seq_tasks() { return &_par_seq_tasks; }
232174294Sobrien
233174294Sobrien  // IF "this" is a ContiguousSpace, return it, else return NULL.
234174294Sobrien  virtual ContiguousSpace* toContiguousSpace() {
235174294Sobrien    return NULL;
236174294Sobrien  }
237174294Sobrien
238174294Sobrien  // Debugging
239174294Sobrien  virtual void verify() const = 0;
240174294Sobrien};
241174294Sobrien
242174294Sobrien// A MemRegionClosure (ResourceObj) whose "do_MemRegion" function applies an
243174294Sobrien// OopClosure to (the addresses of) all the ref-containing fields that could
244174294Sobrien// be modified by virtue of the given MemRegion being dirty. (Note that
245174294Sobrien// because of the imprecise nature of the write barrier, this may iterate
246174294Sobrien// over oops beyond the region.)
247174294Sobrien// This base type for dirty card to oop closures handles memory regions
248174294Sobrien// in non-contiguous spaces with no boundaries, and should be sub-classed
249174294Sobrien// to support other space types. See ContiguousDCTOC for a sub-class
250174294Sobrien// that works with ContiguousSpaces.
251174294Sobrien
252174294Sobrienclass DirtyCardToOopClosure: public MemRegionClosureRO {
253174294Sobrienprotected:
254174294Sobrien  ExtendedOopClosure* _cl;
255174294Sobrien  Space* _sp;
256174294Sobrien  CardTableModRefBS::PrecisionStyle _precision;
257174294Sobrien  HeapWord* _boundary;          // If non-NULL, process only non-NULL oops
258174294Sobrien                                // pointing below boundary.
259174294Sobrien  HeapWord* _min_done;          // ObjHeadPreciseArray precision requires
260174294Sobrien                                // a downwards traversal; this is the
261174294Sobrien                                // lowest location already done (or,
262174294Sobrien                                // alternatively, the lowest address that
263174294Sobrien                                // shouldn't be done again.  NULL means infinity.)
264174294Sobrien  NOT_PRODUCT(HeapWord* _last_bottom;)
265174294Sobrien  NOT_PRODUCT(HeapWord* _last_explicit_min_done;)
266174294Sobrien
267174294Sobrien  // Get the actual top of the area on which the closure will
268174294Sobrien  // operate, given where the top is assumed to be (the end of the
269174294Sobrien  // memory region passed to do_MemRegion) and where the object
270174294Sobrien  // at the top is assumed to start. For example, an object may
271174294Sobrien  // start at the top but actually extend past the assumed top,
272174294Sobrien  // in which case the top becomes the end of the object.
273174294Sobrien  virtual HeapWord* get_actual_top(HeapWord* top, HeapWord* top_obj);
274174294Sobrien
275174294Sobrien  // Walk the given memory region from bottom to (actual) top
276174294Sobrien  // looking for objects and applying the oop closure (_cl) to
277174294Sobrien  // them. The base implementation of this treats the area as
278174294Sobrien  // blocks, where a block may or may not be an object. Sub-
279174294Sobrien  // classes should override this to provide more accurate
280174294Sobrien  // or possibly more efficient walking.
281174294Sobrien  virtual void walk_mem_region(MemRegion mr, HeapWord* bottom, HeapWord* top);
282174294Sobrien
283174294Sobrienpublic:
284174294Sobrien  DirtyCardToOopClosure(Space* sp, ExtendedOopClosure* cl,
285174294Sobrien                        CardTableModRefBS::PrecisionStyle precision,
286174294Sobrien                        HeapWord* boundary) :
287174294Sobrien    _sp(sp), _cl(cl), _precision(precision), _boundary(boundary),
288174294Sobrien    _min_done(NULL) {
289174294Sobrien    NOT_PRODUCT(_last_bottom = NULL);
290174294Sobrien    NOT_PRODUCT(_last_explicit_min_done = NULL);
291174294Sobrien  }
292174294Sobrien
293174294Sobrien  void do_MemRegion(MemRegion mr);
294174294Sobrien
295174294Sobrien  void set_min_done(HeapWord* min_done) {
296174294Sobrien    _min_done = min_done;
297174294Sobrien    NOT_PRODUCT(_last_explicit_min_done = _min_done);
298174294Sobrien  }
299174294Sobrien#ifndef PRODUCT
300174294Sobrien  void set_last_bottom(HeapWord* last_bottom) {
301174294Sobrien    _last_bottom = last_bottom;
302174294Sobrien  }
303174294Sobrien#endif
304174294Sobrien};
305174294Sobrien
306174294Sobrien// A structure to represent a point at which objects are being copied
307174294Sobrien// during compaction.
308174294Sobrienclass CompactPoint : public StackObj {
309174294Sobrienpublic:
310174294Sobrien  Generation* gen;
311174294Sobrien  CompactibleSpace* space;
312174294Sobrien  HeapWord* threshold;
313174294Sobrien
314174294Sobrien  CompactPoint(Generation* g = NULL) :
315174294Sobrien    gen(g), space(NULL), threshold(0) {}
316174294Sobrien};
317174294Sobrien
318174294Sobrien// A space that supports compaction operations.  This is usually, but not
319174294Sobrien// necessarily, a space that is normally contiguous.  But, for example, a
320174294Sobrien// free-list-based space whose normal collection is a mark-sweep without
321174294Sobrien// compaction could still support compaction in full GC's.
322174294Sobrien//
323174294Sobrien// The compaction operations are implemented by the
324174294Sobrien// scan_and_{adjust_pointers,compact,forward} function templates.
325174294Sobrien// The following are, non-virtual, auxiliary functions used by these function templates:
326174294Sobrien// - scan_limit()
327174294Sobrien// - scanned_block_is_obj()
328174294Sobrien// - scanned_block_size()
329174294Sobrien// - adjust_obj_size()
330174294Sobrien// - obj_size()
331174294Sobrien// These functions are to be used exclusively by the scan_and_* function templates,
332174294Sobrien// and must be defined for all (non-abstract) subclasses of CompactibleSpace.
333174294Sobrien//
334174294Sobrien// NOTE: Any subclasses to CompactibleSpace wanting to change/define the behavior
335174294Sobrien// in any of the auxiliary functions must also override the corresponding
336174294Sobrien// prepare_for_compaction/adjust_pointers/compact functions using them.
337174294Sobrien// If not, such changes will not be used or have no effect on the compaction operations.
338174294Sobrien//
339174294Sobrien// This translates to the following dependencies:
340174294Sobrien// Overrides/definitions of
341174294Sobrien//  - scan_limit
342174294Sobrien//  - scanned_block_is_obj
343174294Sobrien//  - scanned_block_size
344174294Sobrien// require override/definition of prepare_for_compaction().
345174294Sobrien// Similar dependencies exist between
346174294Sobrien//  - adjust_obj_size  and adjust_pointers()
347174294Sobrien//  - obj_size         and compact().
348174294Sobrien//
349174294Sobrien// Additionally, this also means that changes to block_size() or block_is_obj() that
350174294Sobrien// should be effective during the compaction operations must provide a corresponding
351174294Sobrien// definition of scanned_block_size/scanned_block_is_obj respectively.
352174294Sobrienclass CompactibleSpace: public Space {
353174294Sobrien  friend class VMStructs;
354174294Sobrien  friend class CompactibleFreeListSpace;
355174294Sobrienprivate:
356174294Sobrien  HeapWord* _compaction_top;
357174294Sobrien  CompactibleSpace* _next_compaction_space;
358174294Sobrien
359174294Sobrien  // Auxiliary functions for scan_and_{forward,adjust_pointers,compact} support.
360174294Sobrien  inline size_t adjust_obj_size(size_t size) const {
361174294Sobrien    return size;
362174294Sobrien  }
363174294Sobrien
364174294Sobrien  inline size_t obj_size(const HeapWord* addr) const;
365174294Sobrien
366174294Sobrien  template <class SpaceType>
367174294Sobrien  static inline void verify_up_to_first_dead(SpaceType* space) NOT_DEBUG_RETURN;
368174294Sobrien
369174294Sobrien  template <class SpaceType>
370174294Sobrien  static inline void clear_empty_region(SpaceType* space);
371174294Sobrien
372174294Sobrienpublic:
373174294Sobrien  CompactibleSpace() :
374174294Sobrien   _compaction_top(NULL), _next_compaction_space(NULL) {}
375174294Sobrien
376174294Sobrien  virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space);
377174294Sobrien  virtual void clear(bool mangle_space);
378174294Sobrien
379174294Sobrien  // Used temporarily during a compaction phase to hold the value
380174294Sobrien  // top should have when compaction is complete.
381174294Sobrien  HeapWord* compaction_top() const { return _compaction_top;    }
382174294Sobrien
383174294Sobrien  void set_compaction_top(HeapWord* value) {
384174294Sobrien    assert(value == NULL || (value >= bottom() && value <= end()),
385174294Sobrien      "should point inside space");
386174294Sobrien    _compaction_top = value;
387174294Sobrien  }
388174294Sobrien
389174294Sobrien  // Perform operations on the space needed after a compaction
390174294Sobrien  // has been performed.
391174294Sobrien  virtual void reset_after_compaction() = 0;
392174294Sobrien
393174294Sobrien  // Returns the next space (in the current generation) to be compacted in
394174294Sobrien  // the global compaction order.  Also is used to select the next
395174294Sobrien  // space into which to compact.
396174294Sobrien
397174294Sobrien  virtual CompactibleSpace* next_compaction_space() const {
398174294Sobrien    return _next_compaction_space;
399174294Sobrien  }
400174294Sobrien
401174294Sobrien  void set_next_compaction_space(CompactibleSpace* csp) {
402174294Sobrien    _next_compaction_space = csp;
403174294Sobrien  }
404174294Sobrien
405174294Sobrien  // MarkSweep support phase2
406174294Sobrien
407174294Sobrien  // Start the process of compaction of the current space: compute
408174294Sobrien  // post-compaction addresses, and insert forwarding pointers.  The fields
409174294Sobrien  // "cp->gen" and "cp->compaction_space" are the generation and space into
410174294Sobrien  // which we are currently compacting.  This call updates "cp" as necessary,
411174294Sobrien  // and leaves the "compaction_top" of the final value of
412174294Sobrien  // "cp->compaction_space" up-to-date.  Offset tables may be updated in
413174294Sobrien  // this phase as if the final copy had occurred; if so, "cp->threshold"
414174294Sobrien  // indicates when the next such action should be taken.
415174294Sobrien  virtual void prepare_for_compaction(CompactPoint* cp) = 0;
416174294Sobrien  // MarkSweep support phase3
417174294Sobrien  virtual void adjust_pointers();
418174294Sobrien  // MarkSweep support phase4
419174294Sobrien  virtual void compact();
420174294Sobrien
421174294Sobrien  // The maximum percentage of objects that can be dead in the compacted
422174294Sobrien  // live part of a compacted space ("deadwood" support.)
423174294Sobrien  virtual size_t allowed_dead_ratio() const { return 0; };
424174294Sobrien
425174294Sobrien  // Some contiguous spaces may maintain some data structures that should
426174294Sobrien  // be updated whenever an allocation crosses a boundary.  This function
427174294Sobrien  // returns the first such boundary.
428174294Sobrien  // (The default implementation returns the end of the space, so the
429174294Sobrien  // boundary is never crossed.)
430174294Sobrien  virtual HeapWord* initialize_threshold() { return end(); }
431174294Sobrien
432174294Sobrien  // "q" is an object of the given "size" that should be forwarded;
433174294Sobrien  // "cp" names the generation ("gen") and containing "this" (which must
434174294Sobrien  // also equal "cp->space").  "compact_top" is where in "this" the
435174294Sobrien  // next object should be forwarded to.  If there is room in "this" for
436174294Sobrien  // the object, insert an appropriate forwarding pointer in "q".
437174294Sobrien  // If not, go to the next compaction space (there must
438174294Sobrien  // be one, since compaction must succeed -- we go to the first space of
439174294Sobrien  // the previous generation if necessary, updating "cp"), reset compact_top
440174294Sobrien  // and then forward.  In either case, returns the new value of "compact_top".
441174294Sobrien  // If the forwarding crosses "cp->threshold", invokes the "cross_threshold"
442174294Sobrien  // function of the then-current compaction space, and updates "cp->threshold
443174294Sobrien  // accordingly".
444174294Sobrien  virtual HeapWord* forward(oop q, size_t size, CompactPoint* cp,
445174294Sobrien                    HeapWord* compact_top);
446174294Sobrien
447174294Sobrien  // Return a size with adjustments as required of the space.
448174294Sobrien  virtual size_t adjust_object_size_v(size_t size) const { return size; }
449174294Sobrien
450174294Sobrienprotected:
451174294Sobrien  // Used during compaction.
452174294Sobrien  HeapWord* _first_dead;
453174294Sobrien  HeapWord* _end_of_live;
454174294Sobrien
455174294Sobrien  // Minimum size of a free block.
456174294Sobrien  virtual size_t minimum_free_block_size() const { return 0; }
457174294Sobrien
458174294Sobrien  // This the function is invoked when an allocation of an object covering
459174294Sobrien  // "start" to "end occurs crosses the threshold; returns the next
460174294Sobrien  // threshold.  (The default implementation does nothing.)
461174294Sobrien  virtual HeapWord* cross_threshold(HeapWord* start, HeapWord* the_end) {
462174294Sobrien    return end();
463174294Sobrien  }
464174294Sobrien
465174294Sobrien  // Below are template functions for scan_and_* algorithms (avoiding virtual calls).
466174294Sobrien  // The space argument should be a subclass of CompactibleSpace, implementing
467174294Sobrien  // scan_limit(), scanned_block_is_obj(), and scanned_block_size(),
468174294Sobrien  // and possibly also overriding obj_size(), and adjust_obj_size().
469174294Sobrien  // These functions should avoid virtual calls whenever possible.
470174294Sobrien
471174294Sobrien  // Frequently calls adjust_obj_size().
472174294Sobrien  template <class SpaceType>
473174294Sobrien  static inline void scan_and_adjust_pointers(SpaceType* space);
474174294Sobrien
475174294Sobrien  // Frequently calls obj_size().
476174294Sobrien  template <class SpaceType>
477174294Sobrien  static inline void scan_and_compact(SpaceType* space);
478174294Sobrien
479174294Sobrien  // Frequently calls scanned_block_is_obj() and scanned_block_size().
480174294Sobrien  // Requires the scan_limit() function.
481174294Sobrien  template <class SpaceType>
482174294Sobrien  static inline void scan_and_forward(SpaceType* space, CompactPoint* cp);
483174294Sobrien};
484174294Sobrien
485174294Sobrienclass GenSpaceMangler;
486174294Sobrien
487174294Sobrien// A space in which the free area is contiguous.  It therefore supports
488174294Sobrien// faster allocation, and compaction.
489174294Sobrienclass ContiguousSpace: public CompactibleSpace {
490174294Sobrien  friend class VMStructs;
491174294Sobrien  // Allow scan_and_forward function to call (private) overrides for auxiliary functions on this class
492174294Sobrien  template <typename SpaceType>
493174294Sobrien  friend void CompactibleSpace::scan_and_forward(SpaceType* space, CompactPoint* cp);
494174294Sobrien
495174294Sobrien private:
496174294Sobrien  // Auxiliary functions for scan_and_forward support.
497174294Sobrien  // See comments for CompactibleSpace for more information.
498174294Sobrien  inline HeapWord* scan_limit() const {
499174294Sobrien    return top();
500174294Sobrien  }
501174294Sobrien
502174294Sobrien  inline bool scanned_block_is_obj(const HeapWord* addr) const {
503174294Sobrien    return true; // Always true, since scan_limit is top
504174294Sobrien  }
505174294Sobrien
506174294Sobrien  inline size_t scanned_block_size(const HeapWord* addr) const;
507174294Sobrien
508174294Sobrien protected:
509174294Sobrien  HeapWord* _top;
510174294Sobrien  HeapWord* _concurrent_iteration_safe_limit;
511174294Sobrien  // A helper for mangling the unused area of the space in debug builds.
512174294Sobrien  GenSpaceMangler* _mangler;
513174294Sobrien
514174294Sobrien  GenSpaceMangler* mangler() { return _mangler; }
515174294Sobrien
516174294Sobrien  // Allocation helpers (return NULL if full).
517174294Sobrien  inline HeapWord* allocate_impl(size_t word_size);
518174294Sobrien  inline HeapWord* par_allocate_impl(size_t word_size);
519174294Sobrien
520174294Sobrien public:
521174294Sobrien  ContiguousSpace();
522174294Sobrien  ~ContiguousSpace();
523174294Sobrien
524174294Sobrien  virtual void initialize(MemRegion mr, bool clear_space, bool mangle_space);
525174294Sobrien  virtual void clear(bool mangle_space);
526174294Sobrien
527174294Sobrien  // Accessors
528174294Sobrien  HeapWord* top() const            { return _top;    }
529174294Sobrien  void set_top(HeapWord* value)    { _top = value; }
530174294Sobrien
531174294Sobrien  void set_saved_mark()            { _saved_mark_word = top();    }
532174294Sobrien  void reset_saved_mark()          { _saved_mark_word = bottom(); }
533174294Sobrien
534174294Sobrien  bool saved_mark_at_top() const { return saved_mark_word() == top(); }
535174294Sobrien
536174294Sobrien  // In debug mode mangle (write it with a particular bit
537174294Sobrien  // pattern) the unused part of a space.
538174294Sobrien
539174294Sobrien  // Used to save the an address in a space for later use during mangling.
540174294Sobrien  void set_top_for_allocations(HeapWord* v) PRODUCT_RETURN;
541174294Sobrien  // Used to save the space's current top for later use during mangling.
542174294Sobrien  void set_top_for_allocations() PRODUCT_RETURN;
543174294Sobrien
544174294Sobrien  // Mangle regions in the space from the current top up to the
545174294Sobrien  // previously mangled part of the space.
546174294Sobrien  void mangle_unused_area() PRODUCT_RETURN;
547174294Sobrien  // Mangle [top, end)
548174294Sobrien  void mangle_unused_area_complete() PRODUCT_RETURN;
549174294Sobrien
550174294Sobrien  // Do some sparse checking on the area that should have been mangled.
551174294Sobrien  void check_mangled_unused_area(HeapWord* limit) PRODUCT_RETURN;
552174294Sobrien  // Check the complete area that should have been mangled.
553174294Sobrien  // This code may be NULL depending on the macro DEBUG_MANGLING.
554174294Sobrien  void check_mangled_unused_area_complete() PRODUCT_RETURN;
555174294Sobrien
556174294Sobrien  // Size computations: sizes in bytes.
557174294Sobrien  size_t capacity() const        { return byte_size(bottom(), end()); }
558174294Sobrien  size_t used() const            { return byte_size(bottom(), top()); }
559174294Sobrien  size_t free() const            { return byte_size(top(),    end()); }
560174294Sobrien
561174294Sobrien  virtual bool is_free_block(const HeapWord* p) const;
562174294Sobrien
563174294Sobrien  // In a contiguous space we have a more obvious bound on what parts
564174294Sobrien  // contain objects.
565174294Sobrien  MemRegion used_region() const { return MemRegion(bottom(), top()); }
566174294Sobrien
567174294Sobrien  // Allocation (return NULL if full)
568174294Sobrien  virtual HeapWord* allocate(size_t word_size);
569174294Sobrien  virtual HeapWord* par_allocate(size_t word_size);
570174294Sobrien  HeapWord* allocate_aligned(size_t word_size);
571174294Sobrien
572174294Sobrien  // Iteration
573174294Sobrien  void oop_iterate(ExtendedOopClosure* cl);
574174294Sobrien  void object_iterate(ObjectClosure* blk);
575174294Sobrien  // For contiguous spaces this method will iterate safely over objects
576174294Sobrien  // in the space (i.e., between bottom and top) when at a safepoint.
577174294Sobrien  void safe_object_iterate(ObjectClosure* blk);
578174294Sobrien
579174294Sobrien  // Iterate over as many initialized objects in the space as possible,
580174294Sobrien  // calling "cl.do_object_careful" on each. Return NULL if all objects
581174294Sobrien  // in the space (at the start of the iteration) were iterated over.
582174294Sobrien  // Return an address indicating the extent of the iteration in the
583174294Sobrien  // event that the iteration had to return because of finding an
584174294Sobrien  // uninitialized object in the space, or if the closure "cl"
585174294Sobrien  // signaled early termination.
586174294Sobrien  HeapWord* object_iterate_careful(ObjectClosureCareful* cl);
587174294Sobrien  HeapWord* concurrent_iteration_safe_limit() {
588174294Sobrien    assert(_concurrent_iteration_safe_limit <= top(),
589174294Sobrien           "_concurrent_iteration_safe_limit update missed");
590174294Sobrien    return _concurrent_iteration_safe_limit;
591174294Sobrien  }
592174294Sobrien  // changes the safe limit, all objects from bottom() to the new
593174294Sobrien  // limit should be properly initialized
594174294Sobrien  void set_concurrent_iteration_safe_limit(HeapWord* new_limit) {
595174294Sobrien    assert(new_limit <= top(), "uninitialized objects in the safe range");
596174294Sobrien    _concurrent_iteration_safe_limit = new_limit;
597174294Sobrien  }
598174294Sobrien
599174294Sobrien
600174294Sobrien#if INCLUDE_ALL_GCS
601174294Sobrien  // In support of parallel oop_iterate.
602174294Sobrien  #define ContigSpace_PAR_OOP_ITERATE_DECL(OopClosureType, nv_suffix)  \
603174294Sobrien    void par_oop_iterate(MemRegion mr, OopClosureType* blk);
604174294Sobrien
605174294Sobrien    ALL_PAR_OOP_ITERATE_CLOSURES(ContigSpace_PAR_OOP_ITERATE_DECL)
606174294Sobrien  #undef ContigSpace_PAR_OOP_ITERATE_DECL
607174294Sobrien#endif // INCLUDE_ALL_GCS
608174294Sobrien
609174294Sobrien  // Compaction support
610174294Sobrien  virtual void reset_after_compaction() {
611174294Sobrien    assert(compaction_top() >= bottom() && compaction_top() <= end(), "should point inside space");
612174294Sobrien    set_top(compaction_top());
613174294Sobrien    // set new iteration safe limit
614174294Sobrien    set_concurrent_iteration_safe_limit(compaction_top());
615174294Sobrien  }
616174294Sobrien
617174294Sobrien  // Override.
618174294Sobrien  DirtyCardToOopClosure* new_dcto_cl(ExtendedOopClosure* cl,
619174294Sobrien                                     CardTableModRefBS::PrecisionStyle precision,
620174294Sobrien                                     HeapWord* boundary,
621174294Sobrien                                     bool parallel);
622174294Sobrien
623174294Sobrien  // Apply "blk->do_oop" to the addresses of all reference fields in objects
624174294Sobrien  // starting with the _saved_mark_word, which was noted during a generation's
625174294Sobrien  // save_marks and is required to denote the head of an object.
626174294Sobrien  // Fields in objects allocated by applications of the closure
627174294Sobrien  // *are* included in the iteration.
628174294Sobrien  // Updates _saved_mark_word to point to just after the last object
629174294Sobrien  // iterated over.
630174294Sobrien#define ContigSpace_OOP_SINCE_SAVE_MARKS_DECL(OopClosureType, nv_suffix)  \
631174294Sobrien  void oop_since_save_marks_iterate##nv_suffix(OopClosureType* blk);
632174294Sobrien
633174294Sobrien  ALL_SINCE_SAVE_MARKS_CLOSURES(ContigSpace_OOP_SINCE_SAVE_MARKS_DECL)
634174294Sobrien#undef ContigSpace_OOP_SINCE_SAVE_MARKS_DECL
635174294Sobrien
636174294Sobrien  // Same as object_iterate, but starting from "mark", which is required
637174294Sobrien  // to denote the start of an object.  Objects allocated by
638174294Sobrien  // applications of the closure *are* included in the iteration.
639174294Sobrien  virtual void object_iterate_from(HeapWord* mark, ObjectClosure* blk);
640174294Sobrien
641174294Sobrien  // Very inefficient implementation.
642174294Sobrien  virtual HeapWord* block_start_const(const void* p) const;
643174294Sobrien  size_t block_size(const HeapWord* p) const;
644174294Sobrien  // If a block is in the allocated area, it is an object.
645174294Sobrien  bool block_is_obj(const HeapWord* p) const { return p < top(); }
646174294Sobrien
647174294Sobrien  // Addresses for inlined allocation
648174294Sobrien  HeapWord** top_addr() { return &_top; }
649174294Sobrien  HeapWord** end_addr() { return &_end; }
650174294Sobrien
651174294Sobrien  // Overrides for more efficient compaction support.
652174294Sobrien  void prepare_for_compaction(CompactPoint* cp);
653174294Sobrien
654174294Sobrien  virtual void print_on(outputStream* st) const;
655174294Sobrien
656174294Sobrien  // Checked dynamic downcasts.
657174294Sobrien  virtual ContiguousSpace* toContiguousSpace() {
658174294Sobrien    return this;
659174294Sobrien  }
660174294Sobrien
661174294Sobrien  // Debugging
662174294Sobrien  virtual void verify() const;
663174294Sobrien
664174294Sobrien  // Used to increase collection frequency.  "factor" of 0 means entire
665174294Sobrien  // space.
666174294Sobrien  void allocate_temporary_filler(int factor);
667174294Sobrien};
668174294Sobrien
669174294Sobrien
670174294Sobrien// A dirty card to oop closure that does filtering.
671174294Sobrien// It knows how to filter out objects that are outside of the _boundary.
672174294Sobrienclass FilteringDCTOC : public DirtyCardToOopClosure {
673174294Sobrienprotected:
674174294Sobrien  // Override.
675174294Sobrien  void walk_mem_region(MemRegion mr,
676174294Sobrien                       HeapWord* bottom, HeapWord* top);
677174294Sobrien
678174294Sobrien  // Walk the given memory region, from bottom to top, applying
679174294Sobrien  // the given oop closure to (possibly) all objects found. The
680174294Sobrien  // given oop closure may or may not be the same as the oop
681174294Sobrien  // closure with which this closure was created, as it may
682174294Sobrien  // be a filtering closure which makes use of the _boundary.
683174294Sobrien  // We offer two signatures, so the FilteringClosure static type is
684174294Sobrien  // apparent.
685174294Sobrien  virtual void walk_mem_region_with_cl(MemRegion mr,
686174294Sobrien                                       HeapWord* bottom, HeapWord* top,
687174294Sobrien                                       ExtendedOopClosure* cl) = 0;
688174294Sobrien  virtual void walk_mem_region_with_cl(MemRegion mr,
689174294Sobrien                                       HeapWord* bottom, HeapWord* top,
690174294Sobrien                                       FilteringClosure* cl) = 0;
691174294Sobrien
692174294Sobrienpublic:
693174294Sobrien  FilteringDCTOC(Space* sp, ExtendedOopClosure* cl,
694174294Sobrien                  CardTableModRefBS::PrecisionStyle precision,
695174294Sobrien                  HeapWord* boundary) :
696174294Sobrien    DirtyCardToOopClosure(sp, cl, precision, boundary) {}
697174294Sobrien};
698174294Sobrien
699174294Sobrien// A dirty card to oop closure for contiguous spaces
700174294Sobrien// (ContiguousSpace and sub-classes).
701174294Sobrien// It is a FilteringClosure, as defined above, and it knows:
702174294Sobrien//
703174294Sobrien// 1. That the actual top of any area in a memory region
704174294Sobrien//    contained by the space is bounded by the end of the contiguous
705174294Sobrien//    region of the space.
706174294Sobrien// 2. That the space is really made up of objects and not just
707174294Sobrien//    blocks.
708174294Sobrien
709174294Sobrienclass ContiguousSpaceDCTOC : public FilteringDCTOC {
710174294Sobrienprotected:
711174294Sobrien  // Overrides.
712174294Sobrien  HeapWord* get_actual_top(HeapWord* top, HeapWord* top_obj);
713174294Sobrien
714174294Sobrien  virtual void walk_mem_region_with_cl(MemRegion mr,
715174294Sobrien                                       HeapWord* bottom, HeapWord* top,
716174294Sobrien                                       ExtendedOopClosure* cl);
717174294Sobrien  virtual void walk_mem_region_with_cl(MemRegion mr,
718174294Sobrien                                       HeapWord* bottom, HeapWord* top,
719174294Sobrien                                       FilteringClosure* cl);
720174294Sobrien
721174294Sobrienpublic:
722174294Sobrien  ContiguousSpaceDCTOC(ContiguousSpace* sp, ExtendedOopClosure* cl,
723174294Sobrien                       CardTableModRefBS::PrecisionStyle precision,
724174294Sobrien                       HeapWord* boundary) :
725174294Sobrien    FilteringDCTOC(sp, cl, precision, boundary)
726174294Sobrien  {}
727174294Sobrien};
728174294Sobrien
729174294Sobrien// A ContigSpace that Supports an efficient "block_start" operation via
730174294Sobrien// a BlockOffsetArray (whose BlockOffsetSharedArray may be shared with
731174294Sobrien// other spaces.)  This is the abstract base class for old generation
732174294Sobrien// (tenured) spaces.
733174294Sobrien
734174294Sobrienclass OffsetTableContigSpace: public ContiguousSpace {
735174294Sobrien  friend class VMStructs;
736174294Sobrien protected:
737174294Sobrien  BlockOffsetArrayContigSpace _offsets;
738174294Sobrien  Mutex _par_alloc_lock;
739174294Sobrien
740174294Sobrien public:
741174294Sobrien  // Constructor
742174294Sobrien  OffsetTableContigSpace(BlockOffsetSharedArray* sharedOffsetArray,
743174294Sobrien                         MemRegion mr);
744174294Sobrien
745174294Sobrien  void set_bottom(HeapWord* value);
746174294Sobrien  void set_end(HeapWord* value);
747174294Sobrien
748174294Sobrien  void clear(bool mangle_space);
749174294Sobrien
750174294Sobrien  inline HeapWord* block_start_const(const void* p) const;
751174294Sobrien
752174294Sobrien  // Add offset table update.
753174294Sobrien  virtual inline HeapWord* allocate(size_t word_size);
754174294Sobrien  inline HeapWord* par_allocate(size_t word_size);
755174294Sobrien
756174294Sobrien  // MarkSweep support phase3
757174294Sobrien  virtual HeapWord* initialize_threshold();
758174294Sobrien  virtual HeapWord* cross_threshold(HeapWord* start, HeapWord* end);
759174294Sobrien
760174294Sobrien  virtual void print_on(outputStream* st) const;
761174294Sobrien
762174294Sobrien  // Debugging
763174294Sobrien  void verify() const;
764174294Sobrien};
765174294Sobrien
766174294Sobrien
767174294Sobrien// Class TenuredSpace is used by TenuredGeneration
768174294Sobrien
769174294Sobrienclass TenuredSpace: public OffsetTableContigSpace {
770174294Sobrien  friend class VMStructs;
771174294Sobrien protected:
772174294Sobrien  // Mark sweep support
773174294Sobrien  size_t allowed_dead_ratio() const;
774174294Sobrien public:
775174294Sobrien  // Constructor
776174294Sobrien  TenuredSpace(BlockOffsetSharedArray* sharedOffsetArray,
777174294Sobrien               MemRegion mr) :
778174294Sobrien    OffsetTableContigSpace(sharedOffsetArray, mr) {}
779174294Sobrien};
780174294Sobrien#endif // SHARE_VM_GC_SHARED_SPACE_HPP
781174294Sobrien