heap.hpp revision 5776:de6a9e811145
1218792Snp/*
2218792Snp * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3218792Snp * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4218792Snp *
5218792Snp * This code is free software; you can redistribute it and/or modify it
6218792Snp * under the terms of the GNU General Public License version 2 only, as
7218792Snp * published by the Free Software Foundation.
8218792Snp *
9218792Snp * This code is distributed in the hope that it will be useful, but WITHOUT
10218792Snp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11218792Snp * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12218792Snp * version 2 for more details (a copy is included in the LICENSE file that
13218792Snp * accompanied this code).
14218792Snp *
15218792Snp * You should have received a copy of the GNU General Public License version
16218792Snp * 2 along with this work; if not, write to the Free Software Foundation,
17218792Snp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18218792Snp *
19218792Snp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20218792Snp * or visit www.oracle.com if you need additional information or have any
21218792Snp * questions.
22218792Snp *
23218792Snp */
24218792Snp
25218792Snp#ifndef SHARE_VM_MEMORY_HEAP_HPP
26218792Snp#define SHARE_VM_MEMORY_HEAP_HPP
27218792Snp
28218792Snp#include "memory/allocation.hpp"
29218792Snp#include "runtime/virtualspace.hpp"
30218792Snp
31218792Snp// Blocks
32237819Snp
33218792Snpclass HeapBlock VALUE_OBJ_CLASS_SPEC {
34218792Snp  friend class VMStructs;
35218792Snp
36218792Snp public:
37218792Snp  struct Header {
38218792Snp    size_t  _length;                             // the length in segments
39218792Snp    bool    _used;                               // Used bit
40219286Snp  };
41219286Snp
42219286Snp protected:
43218792Snp  union {
44218792Snp    Header _header;
45218792Snp    int64_t _padding[ (sizeof(Header) + sizeof(int64_t)-1) / sizeof(int64_t) ];
46218792Snp                        // pad to 0 mod 8
47218792Snp  };
48219436Snp
49218792Snp public:
50218792Snp  // Initialization
51218792Snp  void initialize(size_t length)                 { _header._length = length; set_used(); }
52218792Snp
53218792Snp  // Accessors
54218792Snp  void* allocated_space() const                  { return (void*)(this + 1); }
55218792Snp  size_t length() const                          { return _header._length; }
56218792Snp
57222003Snp  // Used/free
58248925Snp  void set_used()                                { _header._used = true; }
59248925Snp  void set_free()                                { _header._used = false; }
60248925Snp  bool free()                                    { return !_header._used; }
61248925Snp};
62218792Snp
63218792Snpclass FreeBlock: public HeapBlock {
64221474Snp  friend class VMStructs;
65218792Snp protected:
66218792Snp  FreeBlock* _link;
67218792Snp
68222509Snp public:
69218792Snp  // Initialization
70218792Snp  void initialize(size_t length)             { HeapBlock::initialize(length); _link= NULL; }
71218792Snp
72218792Snp  // Merging
73218792Snp  void set_length(size_t l)                  { _header._length = l; }
74218792Snp
75218792Snp  // Accessors
76218792Snp  FreeBlock* link() const                    { return _link; }
77218792Snp  void set_link(FreeBlock* link)             { _link = link; }
78218792Snp};
79227843Smarius
80218792Snpclass CodeHeap : public CHeapObj<mtCode> {
81218792Snp  friend class VMStructs;
82218792Snp private:
83218792Snp  VirtualSpace _memory;                          // the memory holding the blocks
84218792Snp  VirtualSpace _segmap;                          // the memory holding the segment map
85218792Snp
86218792Snp  size_t       _number_of_committed_segments;
87218792Snp  size_t       _number_of_reserved_segments;
88218792Snp  size_t       _segment_size;
89218792Snp  int          _log2_segment_size;
90218792Snp
91218792Snp  size_t       _next_segment;
92218792Snp
93218792Snp  FreeBlock*   _freelist;
94218792Snp  size_t       _freelist_segments;               // No. of segments in freelist
95218792Snp
96218792Snp  // Helper functions
97218792Snp  size_t   size_to_segments(size_t size) const { return (size + _segment_size - 1) >> _log2_segment_size; }
98218792Snp  size_t   segments_to_size(size_t number_of_segments) const { return number_of_segments << _log2_segment_size; }
99218792Snp
100218792Snp  size_t   segment_for(void* p) const            { return ((char*)p - _memory.low()) >> _log2_segment_size; }
101218792Snp  HeapBlock* block_at(size_t i) const            { return (HeapBlock*)(_memory.low() + (i << _log2_segment_size)); }
102218792Snp
103218792Snp  void  mark_segmap_as_free(size_t beg, size_t end);
104218792Snp  void  mark_segmap_as_used(size_t beg, size_t end);
105218792Snp
106218792Snp  // Freelist management helpers
107218792Snp  FreeBlock* following_block(FreeBlock *b);
108218792Snp  void insert_after(FreeBlock* a, FreeBlock* b);
109218792Snp  void merge_right (FreeBlock* a);
110218792Snp
111218792Snp  // Toplevel freelist management
112218792Snp  void add_to_freelist(HeapBlock *b);
113218792Snp  FreeBlock* search_freelist(size_t length, bool is_critical);
114218792Snp
115218792Snp  // Iteration helpers
116218792Snp  void*      next_free(HeapBlock* b) const;
117248925Snp  HeapBlock* first_block() const;
118248925Snp  HeapBlock* next_block(HeapBlock* b) const;
119248925Snp  HeapBlock* block_start(void* p) const;
120248925Snp
121248925Snp  // to perform additional actions on creation of executable code
122248925Snp  void on_code_mapping(char* base, size_t size);
123248925Snp
124248925Snp public:
125248925Snp  CodeHeap();
126248925Snp
127248925Snp  // Heap extents
128248925Snp  bool  reserve(size_t reserved_size, size_t committed_size, size_t segment_size);
129248925Snp  void  release();                               // releases all allocated memory
130248925Snp  bool  expand_by(size_t size);                  // expands commited memory by size
131248925Snp  void  shrink_by(size_t size);                  // shrinks commited memory by size
132248925Snp  void  clear();                                 // clears all heap contents
133248925Snp
134248925Snp  // Memory allocation
135248925Snp  void* allocate  (size_t size, bool is_critical);  // allocates a block of size or returns NULL
136248925Snp  void  deallocate(void* p);                     // deallocates a block
137248925Snp
138248925Snp  // Attributes
139248925Snp  char* low_boundary() const                     { return _memory.low_boundary (); }
140248925Snp  char* high() const                             { return _memory.high(); }
141248925Snp  char* high_boundary() const                    { return _memory.high_boundary(); }
142248925Snp
143248925Snp  bool  contains(const void* p) const            { return low_boundary() <= p && p < high(); }
144248925Snp  void* find_start(void* p) const;              // returns the block containing p or NULL
145248925Snp  size_t alignment_unit() const;                // alignment of any block
146248925Snp  size_t alignment_offset() const;              // offset of first byte of any block, within the enclosing alignment unit
147248925Snp  static size_t header_size();                  // returns the header size for each heap block
148248925Snp
149218792Snp  // Iteration
150218792Snp
151218792Snp  // returns the first block or NULL
152218792Snp  void* first() const       { return next_free(first_block()); }
153218792Snp  // returns the next block given a block p or NULL
154218792Snp  void* next(void* p) const { return next_free(next_block(block_start(p))); }
155218792Snp
156218792Snp  // Statistics
157248925Snp  size_t capacity() const;
158218792Snp  size_t max_capacity() const;
159237263Snp  size_t allocated_capacity() const;
160237263Snp  size_t unallocated_capacity() const            { return max_capacity() - allocated_capacity(); }
161237263Snp
162237263Snpprivate:
163255006Snp  size_t heap_unallocated_capacity() const;
164228561Snp
165237263Snppublic:
166255006Snp  // Debugging
167228561Snp  void verify();
168228561Snp  void print()  PRODUCT_RETURN;
169218792Snp};
170218792Snp
171228561Snp#endif // SHARE_VM_MEMORY_HEAP_HPP
172248925Snp