1 /*
   2  * Copyright (c) 2012, 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  */
  24 #ifndef SHARE_VM_MEMORY_METACHUNK_HPP
  25 #define SHARE_VM_MEMORY_METACHUNK_HPP
  26 
  27 //  Metachunk - Quantum of allocation from a Virtualspace
  28 //    Metachunks are reused (when freed are put on a global freelist) and
  29 //    have no permanent association to a SpaceManager.
  30 
  31 //            +--------------+ <- end
  32 //            |              |          --+       ---+
  33 //            |              |            | free     |
  34 //            |              |            |          |
  35 //            |              |            |          | capacity
  36 //            |              |            |          |
  37 //            |              | <- top   --+          |
  38 //            |              |           ---+        |
  39 //            |              |              | used   |
  40 //            |              |              |        |
  41 //            |              |              |        |
  42 //            +--------------+ <- bottom ---+     ---+
  43 
  44 class VirtualSpaceNode;
  45 
  46 class Metachunk VALUE_OBJ_CLASS_SPEC {
  47   // link to support lists of chunks
  48   Metachunk* _next;
  49   Metachunk* _prev;
  50   VirtualSpaceNode* _container;
  51 
  52   MetaWord* _bottom;
  53   MetaWord* _end;
  54   MetaWord* _top;
  55   size_t _word_size;
  56   // Used in a guarantee() so included in the Product builds
  57   // even through it is only for debugging.
  58   bool _is_free;
  59 
  60   // Metachunks are allocated out of a MetadataVirtualSpace and
  61   // and use some of its space to describe itself (plus alignment
  62   // considerations).  Metadata is allocated in the rest of the chunk.
  63   // This size is the overhead of maintaining the Metachunk within
  64   // the space.
  65   static size_t _overhead;
  66 
  67  public:
  68   Metachunk(size_t word_size , VirtualSpaceNode* container);
  69 
  70   // Used to add a Metachunk to a list of Metachunks
  71   void set_next(Metachunk* v) { _next = v; assert(v != this, "Boom");}
  72   void set_prev(Metachunk* v) { _prev = v; assert(v != this, "Boom");}
  73   void set_container(VirtualSpaceNode* v) { _container = v; }
  74 
  75   MetaWord* allocate(size_t word_size);
  76 
  77   // Accessors
  78   Metachunk* next() const { return _next; }
  79   Metachunk* prev() const { return _prev; }
  80   VirtualSpaceNode* container() const { return _container; }
  81   MetaWord* bottom() const { return _bottom; }
  82   MetaWord* end() const { return _end; }
  83   MetaWord* top() const { return _top; }
  84   size_t word_size() const { return _word_size; }
  85   size_t size() const volatile { return _word_size; }
  86   void set_size(size_t v) { _word_size = v; }
  87   bool is_free() { return _is_free; }
  88   void set_is_free(bool v) { _is_free = v; }
  89   static size_t overhead() { return _overhead; }
  90   void clear_next()              { set_next(NULL); }
  91   void link_prev(Metachunk* ptr) { set_prev(ptr); }
  92   uintptr_t* end()              { return ((uintptr_t*) this) + size(); }
  93   bool cantCoalesce() const     { return false; }
  94   void link_next(Metachunk* ptr) { set_next(ptr); }
  95   void link_after(Metachunk* ptr){
  96     link_next(ptr);
  97     if (ptr != NULL) ptr->link_prev(this);
  98   }
  99 
 100   // Reset top to bottom so chunk can be reused.
 101   void reset_empty() { _top = (_bottom + _overhead); _next = NULL; _prev = NULL; }
 102   bool is_empty() { return _top == (_bottom + _overhead); }
 103 
 104   // used (has been allocated)
 105   // free (available for future allocations)
 106   // capacity (total size of chunk)
 107   size_t used_word_size() const;
 108   size_t free_word_size() const;
 109   size_t capacity_word_size()const;
 110 
 111   // Debug support
 112 #ifdef ASSERT
 113   void* prev_addr() const { return (void*)&_prev; }
 114   void* next_addr() const { return (void*)&_next; }
 115   void* size_addr() const { return (void*)&_word_size; }
 116 #endif
 117   bool verify_chunk_in_free_list(Metachunk* tc) const { return true; }
 118   bool verify_par_locked() { return true; }
 119 
 120   void assert_is_mangled() const {/* Don't check "\*/}
 121 
 122   NOT_PRODUCT(void mangle();)
 123 
 124   void print_on(outputStream* st) const;
 125   void verify();
 126 };
 127 #endif  // SHARE_VM_MEMORY_METACHUNK_HPP