src/share/vm/memory/heap.hpp

Thu, 21 Oct 2010 11:55:10 -0700

author
never
date
Thu, 21 Oct 2010 11:55:10 -0700
changeset 2262
1e9a9d2e6509
parent 1907
c18cbe5936b8
child 2314
f95d63e2154a
permissions
-rw-r--r--

6970683: improvements to hs_err output
Reviewed-by: kvn, jrose, dholmes, coleenp

     1 /*
     2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 // Blocks
    27 class HeapBlock VALUE_OBJ_CLASS_SPEC {
    28   friend class VMStructs;
    30  public:
    31   struct Header {
    32     size_t  _length;                             // the length in segments
    33     bool    _used;                               // Used bit
    34   };
    36  protected:
    37   union {
    38     Header _header;
    39     int64_t _padding[ (sizeof(Header) + sizeof(int64_t)-1) / sizeof(int64_t) ];
    40                         // pad to 0 mod 8
    41   };
    43  public:
    44   // Initialization
    45   void initialize(size_t length)                 { _header._length = length; set_used(); }
    47   // Accessors
    48   void* allocated_space() const                  { return (void*)(this + 1); }
    49   size_t length() const                          { return _header._length; }
    51   // Used/free
    52   void set_used()                                { _header._used = true; }
    53   void set_free()                                { _header._used = false; }
    54   bool free()                                    { return !_header._used; }
    55 };
    57 class FreeBlock: public HeapBlock {
    58   friend class VMStructs;
    59  protected:
    60   FreeBlock* _link;
    62  public:
    63   // Initialization
    64   void initialize(size_t length)             { HeapBlock::initialize(length); _link= NULL; }
    66   // Merging
    67   void set_length(size_t l)                  { _header._length = l; }
    69   // Accessors
    70   FreeBlock* link() const                    { return _link; }
    71   void set_link(FreeBlock* link)             { _link = link; }
    72 };
    74 class CodeHeap : public CHeapObj {
    75   friend class VMStructs;
    76  private:
    77   VirtualSpace _memory;                          // the memory holding the blocks
    78   VirtualSpace _segmap;                          // the memory holding the segment map
    80   size_t       _number_of_committed_segments;
    81   size_t       _number_of_reserved_segments;
    82   size_t       _segment_size;
    83   int          _log2_segment_size;
    85   size_t       _next_segment;
    87   FreeBlock*   _freelist;
    88   size_t       _free_segments;                   // No. of segments in freelist
    90   // Helper functions
    91   size_t   number_of_segments(size_t size) const { return (size + _segment_size - 1) >> _log2_segment_size; }
    92   size_t   size(size_t number_of_segments) const { return number_of_segments << _log2_segment_size; }
    94   size_t   segment_for(void* p) const            { return ((char*)p - _memory.low()) >> _log2_segment_size; }
    95   HeapBlock* block_at(size_t i) const            { return (HeapBlock*)(_memory.low() + (i << _log2_segment_size)); }
    97   void  mark_segmap_as_free(size_t beg, size_t end);
    98   void  mark_segmap_as_used(size_t beg, size_t end);
   100   // Freelist management helpers
   101   FreeBlock* following_block(FreeBlock *b);
   102   void insert_after(FreeBlock* a, FreeBlock* b);
   103   void merge_right (FreeBlock* a);
   105   // Toplevel freelist management
   106   void add_to_freelist(HeapBlock *b);
   107   FreeBlock* search_freelist(size_t length);
   109   // Iteration helpers
   110   void*      next_free(HeapBlock* b) const;
   111   HeapBlock* first_block() const;
   112   HeapBlock* next_block(HeapBlock* b) const;
   113   HeapBlock* block_start(void* p) const;
   115   // to perform additional actions on creation of executable code
   116   void on_code_mapping(char* base, size_t size);
   118  public:
   119   CodeHeap();
   121   // Heap extents
   122   bool  reserve(size_t reserved_size, size_t committed_size, size_t segment_size);
   123   void  release();                               // releases all allocated memory
   124   bool  expand_by(size_t size);                  // expands commited memory by size
   125   void  shrink_by(size_t size);                  // shrinks commited memory by size
   126   void  clear();                                 // clears all heap contents
   128   // Memory allocation
   129   void* allocate  (size_t size);                 // allocates a block of size or returns NULL
   130   void  deallocate(void* p);                     // deallocates a block
   132   // Attributes
   133   void*  begin() const                           { return _memory.low (); }
   134   void*  end() const                             { return _memory.high(); }
   135   bool   contains(void* p) const                 { return begin() <= p && p < end(); }
   136   void*  find_start(void* p) const;              // returns the block containing p or NULL
   137   size_t alignment_unit() const;                 // alignment of any block
   138   size_t alignment_offset() const;               // offset of first byte of any block, within the enclosing alignment unit
   139   static size_t header_size();                   // returns the header size for each heap block
   141   // Returns reserved area high and low addresses
   142   char *low_boundary() const                     { return _memory.low_boundary (); }
   143   char *high() const                             { return _memory.high(); }
   144   char *high_boundary() const                    { return _memory.high_boundary(); }
   146   // Iteration
   148   // returns the first block or NULL
   149   void* first() const       { return next_free(first_block()); }
   150   // returns the next block given a block p or NULL
   151   void* next(void* p) const { return next_free(next_block(block_start(p))); }
   153   // Statistics
   154   size_t capacity() const;
   155   size_t max_capacity() const;
   156   size_t allocated_capacity() const;
   157   size_t unallocated_capacity() const            { return max_capacity() - allocated_capacity(); }
   159   // Debugging
   160   void verify();
   161   void print()  PRODUCT_RETURN;
   162 };

mercurial