2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2015-04-28 16:46:39 -04:00
|
|
|
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-27 19:08:38 -07:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-11-23 13:22:55 -08:00
|
|
|
#ifndef SHARE_VM_MEMORY_HEAP_HPP
|
|
|
|
#define SHARE_VM_MEMORY_HEAP_HPP
|
|
|
|
|
2014-09-17 08:00:07 +02:00
|
|
|
#include "code/codeBlob.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "memory/allocation.hpp"
|
2015-04-28 16:46:39 -04:00
|
|
|
#include "memory/virtualspace.hpp"
|
2015-07-01 10:53:26 +02:00
|
|
|
#include "utilities/macros.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
// Blocks
|
|
|
|
|
|
|
|
class HeapBlock VALUE_OBJ_CLASS_SPEC {
|
|
|
|
friend class VMStructs;
|
|
|
|
|
|
|
|
public:
|
|
|
|
struct Header {
|
|
|
|
size_t _length; // the length in segments
|
|
|
|
bool _used; // Used bit
|
|
|
|
};
|
|
|
|
|
|
|
|
protected:
|
|
|
|
union {
|
|
|
|
Header _header;
|
|
|
|
int64_t _padding[ (sizeof(Header) + sizeof(int64_t)-1) / sizeof(int64_t) ];
|
|
|
|
// pad to 0 mod 8
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Initialization
|
|
|
|
void initialize(size_t length) { _header._length = length; set_used(); }
|
|
|
|
|
|
|
|
// Accessors
|
|
|
|
void* allocated_space() const { return (void*)(this + 1); }
|
|
|
|
size_t length() const { return _header._length; }
|
|
|
|
|
|
|
|
// Used/free
|
|
|
|
void set_used() { _header._used = true; }
|
|
|
|
void set_free() { _header._used = false; }
|
|
|
|
bool free() { return !_header._used; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class FreeBlock: public HeapBlock {
|
|
|
|
friend class VMStructs;
|
|
|
|
protected:
|
|
|
|
FreeBlock* _link;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Initialization
|
|
|
|
void initialize(size_t length) { HeapBlock::initialize(length); _link= NULL; }
|
|
|
|
|
|
|
|
// Merging
|
|
|
|
void set_length(size_t l) { _header._length = l; }
|
|
|
|
|
|
|
|
// Accessors
|
|
|
|
FreeBlock* link() const { return _link; }
|
|
|
|
void set_link(FreeBlock* link) { _link = link; }
|
|
|
|
};
|
|
|
|
|
2012-06-28 17:03:16 -04:00
|
|
|
class CodeHeap : public CHeapObj<mtCode> {
|
2007-12-01 00:00:00 +00:00
|
|
|
friend class VMStructs;
|
2016-12-11 19:07:04 -08:00
|
|
|
protected:
|
2007-12-01 00:00:00 +00:00
|
|
|
VirtualSpace _memory; // the memory holding the blocks
|
|
|
|
VirtualSpace _segmap; // the memory holding the segment map
|
|
|
|
|
|
|
|
size_t _number_of_committed_segments;
|
|
|
|
size_t _number_of_reserved_segments;
|
|
|
|
size_t _segment_size;
|
|
|
|
int _log2_segment_size;
|
|
|
|
|
|
|
|
size_t _next_segment;
|
|
|
|
|
|
|
|
FreeBlock* _freelist;
|
2013-04-11 13:57:44 +02:00
|
|
|
size_t _freelist_segments; // No. of segments in freelist
|
2014-03-07 07:42:40 +01:00
|
|
|
int _freelist_length;
|
2014-09-17 08:00:07 +02:00
|
|
|
size_t _max_allocated_capacity; // Peak capacity that was allocated during lifetime of the heap
|
|
|
|
|
|
|
|
const char* _name; // Name of the CodeHeap
|
|
|
|
const int _code_blob_type; // CodeBlobType it contains
|
2015-11-09 11:35:44 +01:00
|
|
|
int _blob_count; // Number of CodeBlobs
|
|
|
|
int _nmethod_count; // Number of nmethods
|
|
|
|
int _adapter_count; // Number of adapters
|
|
|
|
int _full_count; // Number of times the code heap was full
|
|
|
|
|
2014-03-07 07:42:40 +01:00
|
|
|
|
|
|
|
enum { free_sentinel = 0xFF };
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// Helper functions
|
2013-04-11 13:57:44 +02:00
|
|
|
size_t size_to_segments(size_t size) const { return (size + _segment_size - 1) >> _log2_segment_size; }
|
|
|
|
size_t segments_to_size(size_t number_of_segments) const { return number_of_segments << _log2_segment_size; }
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
size_t segment_for(void* p) const { return ((char*)p - _memory.low()) >> _log2_segment_size; }
|
2014-03-07 07:42:40 +01:00
|
|
|
bool is_segment_unused(int val) const { return val == free_sentinel; }
|
2007-12-01 00:00:00 +00:00
|
|
|
HeapBlock* block_at(size_t i) const { return (HeapBlock*)(_memory.low() + (i << _log2_segment_size)); }
|
|
|
|
|
|
|
|
void mark_segmap_as_free(size_t beg, size_t end);
|
|
|
|
void mark_segmap_as_used(size_t beg, size_t end);
|
|
|
|
|
|
|
|
// Freelist management helpers
|
2014-03-07 07:42:40 +01:00
|
|
|
FreeBlock* following_block(FreeBlock* b);
|
2007-12-01 00:00:00 +00:00
|
|
|
void insert_after(FreeBlock* a, FreeBlock* b);
|
2014-03-07 07:42:40 +01:00
|
|
|
bool merge_right (FreeBlock* a);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// Toplevel freelist management
|
2014-03-07 07:42:40 +01:00
|
|
|
void add_to_freelist(HeapBlock* b);
|
2014-10-24 14:25:46 +02:00
|
|
|
FreeBlock* search_freelist(size_t length);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// Iteration helpers
|
2015-01-12 09:55:20 +01:00
|
|
|
void* next_used(HeapBlock* b) const;
|
2007-12-01 00:00:00 +00:00
|
|
|
HeapBlock* first_block() const;
|
|
|
|
HeapBlock* next_block(HeapBlock* b) const;
|
|
|
|
HeapBlock* block_start(void* p) const;
|
|
|
|
|
|
|
|
// to perform additional actions on creation of executable code
|
|
|
|
void on_code_mapping(char* base, size_t size);
|
2014-03-07 07:42:40 +01:00
|
|
|
void clear(); // clears all heap contents
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
public:
|
2014-09-17 08:00:07 +02:00
|
|
|
CodeHeap(const char* name, const int code_blob_type);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// Heap extents
|
2014-09-17 08:00:07 +02:00
|
|
|
bool reserve(ReservedSpace rs, size_t committed_size, size_t segment_size);
|
2014-01-23 14:47:23 +01:00
|
|
|
bool expand_by(size_t size); // expands committed memory by size
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// Memory allocation
|
2014-10-24 14:25:46 +02:00
|
|
|
void* allocate (size_t size); // Allocate 'size' bytes in the code cache or return NULL
|
|
|
|
void deallocate(void* p); // Deallocate memory
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// Attributes
|
2017-07-10 23:28:25 +02:00
|
|
|
char* low_boundary() const { return _memory.low_boundary(); }
|
2013-04-11 13:57:44 +02:00
|
|
|
char* high() const { return _memory.high(); }
|
|
|
|
char* high_boundary() const { return _memory.high_boundary(); }
|
|
|
|
|
2017-07-10 23:28:25 +02:00
|
|
|
bool contains(const void* p) const { return low_boundary() <= p && p < high(); }
|
|
|
|
bool contains_blob(const CodeBlob* blob) const { return contains(blob->code_begin()); }
|
2017-02-10 08:16:49 +01:00
|
|
|
|
2015-07-01 10:53:26 +02:00
|
|
|
virtual void* find_start(void* p) const; // returns the block containing p or NULL
|
2016-12-11 19:07:04 -08:00
|
|
|
virtual CodeBlob* find_blob_unsafe(void* start) const;
|
2014-03-07 07:42:40 +01:00
|
|
|
size_t alignment_unit() const; // alignment of any block
|
|
|
|
size_t alignment_offset() const; // offset of first byte of any block, within the enclosing alignment unit
|
|
|
|
static size_t header_size(); // returns the header size for each heap block
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2014-03-07 07:42:40 +01:00
|
|
|
size_t allocated_in_freelist() const { return _freelist_segments * CodeCacheSegmentSize; }
|
|
|
|
int freelist_length() const { return _freelist_length; } // number of elements in the freelist
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// returns the first block or NULL
|
2015-07-01 10:53:26 +02:00
|
|
|
virtual void* first() const { return next_used(first_block()); }
|
2007-12-01 00:00:00 +00:00
|
|
|
// returns the next block given a block p or NULL
|
2015-07-01 10:53:26 +02:00
|
|
|
virtual void* next(void* p) const { return next_used(next_block(block_start(p))); }
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// Statistics
|
|
|
|
size_t capacity() const;
|
|
|
|
size_t max_capacity() const;
|
2014-03-07 07:42:40 +01:00
|
|
|
int allocated_segments() const;
|
2007-12-01 00:00:00 +00:00
|
|
|
size_t allocated_capacity() const;
|
2014-09-17 08:00:07 +02:00
|
|
|
size_t max_allocated_capacity() const { return _max_allocated_capacity; }
|
2007-12-01 00:00:00 +00:00
|
|
|
size_t unallocated_capacity() const { return max_capacity() - allocated_capacity(); }
|
|
|
|
|
2014-09-17 08:00:07 +02:00
|
|
|
// Returns true if the CodeHeap contains CodeBlobs of the given type
|
2014-10-01 10:01:46 +02:00
|
|
|
bool accepts(int code_blob_type) const { return (_code_blob_type == CodeBlobType::All) ||
|
|
|
|
(_code_blob_type == code_blob_type); }
|
2014-09-17 08:00:07 +02:00
|
|
|
int code_blob_type() const { return _code_blob_type; }
|
|
|
|
|
|
|
|
// Debugging / Profiling
|
|
|
|
const char* name() const { return _name; }
|
2015-11-09 11:35:44 +01:00
|
|
|
int blob_count() { return _blob_count; }
|
|
|
|
int nmethod_count() { return _nmethod_count; }
|
|
|
|
void set_nmethod_count(int count) { _nmethod_count = count; }
|
|
|
|
int adapter_count() { return _adapter_count; }
|
|
|
|
void set_adapter_count(int count) { _adapter_count = count; }
|
|
|
|
int full_count() { return _full_count; }
|
|
|
|
void report_full() { _full_count++; }
|
2014-09-17 08:00:07 +02:00
|
|
|
|
2013-04-11 13:57:44 +02:00
|
|
|
private:
|
|
|
|
size_t heap_unallocated_capacity() const;
|
|
|
|
|
|
|
|
public:
|
2007-12-01 00:00:00 +00:00
|
|
|
// Debugging
|
2014-03-07 07:42:40 +01:00
|
|
|
void verify() PRODUCT_RETURN;
|
2007-12-01 00:00:00 +00:00
|
|
|
void print() PRODUCT_RETURN;
|
|
|
|
};
|
2010-11-23 13:22:55 -08:00
|
|
|
|
|
|
|
#endif // SHARE_VM_MEMORY_HEAP_HPP
|