1 /*
   2  * Copyright (c) 1997, 2018, 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 
  25 #include "precompiled.hpp"
  26 #include "gc/shared/genCollectedHeap.hpp"
  27 #include "memory/allocation.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "memory/arena.hpp"
  30 #include "memory/metaspaceShared.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "memory/universe.hpp"
  33 #include "runtime/atomic.hpp"
  34 #include "runtime/os.hpp"
  35 #include "runtime/task.hpp"
  36 #include "runtime/threadCritical.hpp"
  37 #include "services/memTracker.hpp"
  38 #include "utilities/ostream.hpp"
  39 
  40 void* MetaspaceObj::_shared_metaspace_base = NULL;
  41 void* MetaspaceObj::_shared_metaspace_top  = NULL;
  42 
  43 void* StackObj::operator new(size_t size)     throw() { ShouldNotCallThis(); return 0; }
  44 void  StackObj::operator delete(void* p)              { ShouldNotCallThis(); }
  45 void* StackObj::operator new [](size_t size)  throw() { ShouldNotCallThis(); return 0; }
  46 void  StackObj::operator delete [](void* p)           { ShouldNotCallThis(); }
  47 
  48 void* MetaspaceObj::operator new(size_t size, ClassLoaderData* loader_data,
  49                                  size_t word_size,
  50                                  MetaspaceObj::Type type, TRAPS) throw() {
  51   // Klass has it's own operator new
  52   return Metaspace::allocate(loader_data, word_size, type, THREAD);
  53 }
  54 
  55 bool MetaspaceObj::is_metaspace_object() const {
  56   return Metaspace::contains((void*)this);
  57 }
  58 
  59 void MetaspaceObj::print_address_on(outputStream* st) const {
  60   st->print(" {" INTPTR_FORMAT "}", p2i(this));
  61 }
  62 
  63 void* ResourceObj::operator new(size_t size, Arena *arena) throw() {
  64   address res = (address)arena->Amalloc(size);
  65   DEBUG_ONLY(set_allocation_type(res, ARENA);)
  66   return res;
  67 }
  68 
  69 void* ResourceObj::operator new [](size_t size, Arena *arena) throw() {
  70   address res = (address)arena->Amalloc(size);
  71   DEBUG_ONLY(set_allocation_type(res, ARENA);)
  72   return res;
  73 }
  74 
  75 void* ResourceObj::operator new(size_t size, allocation_type type, MEMFLAGS flags) throw() {
  76   address res = NULL;
  77   switch (type) {
  78    case C_HEAP:
  79     res = (address)AllocateHeap(size, flags, CALLER_PC);
  80     DEBUG_ONLY(set_allocation_type(res, C_HEAP);)
  81     break;
  82    case RESOURCE_AREA:
  83     // new(size) sets allocation type RESOURCE_AREA.
  84     res = (address)operator new(size);
  85     break;
  86    default:
  87     ShouldNotReachHere();
  88   }
  89   return res;
  90 }
  91 
  92 void* ResourceObj::operator new [](size_t size, allocation_type type, MEMFLAGS flags) throw() {
  93   return (address) operator new(size, type, flags);
  94 }
  95 
  96 void* ResourceObj::operator new(size_t size, const std::nothrow_t&  nothrow_constant,
  97     allocation_type type, MEMFLAGS flags) throw() {
  98   // should only call this with std::nothrow, use other operator new() otherwise
  99   address res = NULL;
 100   switch (type) {
 101    case C_HEAP:
 102     res = (address)AllocateHeap(size, flags, CALLER_PC, AllocFailStrategy::RETURN_NULL);
 103     DEBUG_ONLY(if (res!= NULL) set_allocation_type(res, C_HEAP);)
 104     break;
 105    case RESOURCE_AREA:
 106     // new(size) sets allocation type RESOURCE_AREA.
 107     res = (address)operator new(size, std::nothrow);
 108     break;
 109    default:
 110     ShouldNotReachHere();
 111   }
 112   return res;
 113 }
 114 
 115 void* ResourceObj::operator new [](size_t size, const std::nothrow_t&  nothrow_constant,
 116     allocation_type type, MEMFLAGS flags) throw() {
 117   return (address)operator new(size, nothrow_constant, type, flags);
 118 }
 119 
 120 void ResourceObj::operator delete(void* p) {
 121   assert(((ResourceObj *)p)->allocated_on_C_heap(),
 122          "delete only allowed for C_HEAP objects");
 123   DEBUG_ONLY(((ResourceObj *)p)->_allocation_t[0] = (uintptr_t)badHeapOopVal;)
 124   FreeHeap(p);
 125 }
 126 
 127 void ResourceObj::operator delete [](void* p) {
 128   operator delete(p);
 129 }
 130 
 131 #ifdef ASSERT
 132 void ResourceObj::set_allocation_type(address res, allocation_type type) {
 133     // Set allocation type in the resource object
 134     uintptr_t allocation = (uintptr_t)res;
 135     assert((allocation & allocation_mask) == 0, "address should be aligned to 4 bytes at least: " INTPTR_FORMAT, p2i(res));
 136     assert(type <= allocation_mask, "incorrect allocation type");
 137     ResourceObj* resobj = (ResourceObj *)res;
 138     resobj->_allocation_t[0] = ~(allocation + type);
 139     if (type != STACK_OR_EMBEDDED) {
 140       // Called from operator new() and CollectionSetChooser(),
 141       // set verification value.
 142       resobj->_allocation_t[1] = (uintptr_t)&(resobj->_allocation_t[1]) + type;
 143     }
 144 }
 145 
 146 ResourceObj::allocation_type ResourceObj::get_allocation_type() const {
 147     assert(~(_allocation_t[0] | allocation_mask) == (uintptr_t)this, "lost resource object");
 148     return (allocation_type)((~_allocation_t[0]) & allocation_mask);
 149 }
 150 
 151 bool ResourceObj::is_type_set() const {
 152     allocation_type type = (allocation_type)(_allocation_t[1] & allocation_mask);
 153     return get_allocation_type()  == type &&
 154            (_allocation_t[1] - type) == (uintptr_t)(&_allocation_t[1]);
 155 }
 156 
 157 ResourceObj::ResourceObj() { // default constructor
 158     if (~(_allocation_t[0] | allocation_mask) != (uintptr_t)this) {
 159       // Operator new() is not called for allocations
 160       // on stack and for embedded objects.
 161       set_allocation_type((address)this, STACK_OR_EMBEDDED);
 162     } else if (allocated_on_stack()) { // STACK_OR_EMBEDDED
 163       // For some reason we got a value which resembles
 164       // an embedded or stack object (operator new() does not
 165       // set such type). Keep it since it is valid value
 166       // (even if it was garbage).
 167       // Ignore garbage in other fields.
 168     } else if (is_type_set()) {
 169       // Operator new() was called and type was set.
 170       assert(!allocated_on_stack(),
 171              "not embedded or stack, this(" PTR_FORMAT ") type %d a[0]=(" PTR_FORMAT ") a[1]=(" PTR_FORMAT ")",
 172              p2i(this), get_allocation_type(), _allocation_t[0], _allocation_t[1]);
 173     } else {
 174       // Operator new() was not called.
 175       // Assume that it is embedded or stack object.
 176       set_allocation_type((address)this, STACK_OR_EMBEDDED);
 177     }
 178     _allocation_t[1] = 0; // Zap verification value
 179 }
 180 
 181 ResourceObj::ResourceObj(const ResourceObj& r) { // default copy constructor
 182     // Used in ClassFileParser::parse_constant_pool_entries() for ClassFileStream.
 183     // Note: garbage may resembles valid value.
 184     assert(~(_allocation_t[0] | allocation_mask) != (uintptr_t)this || !is_type_set(),
 185            "embedded or stack only, this(" PTR_FORMAT ") type %d a[0]=(" PTR_FORMAT ") a[1]=(" PTR_FORMAT ")",
 186            p2i(this), get_allocation_type(), _allocation_t[0], _allocation_t[1]);
 187     set_allocation_type((address)this, STACK_OR_EMBEDDED);
 188     _allocation_t[1] = 0; // Zap verification value
 189 }
 190 
 191 ResourceObj& ResourceObj::operator=(const ResourceObj& r) { // default copy assignment
 192     // Used in InlineTree::ok_to_inline() for WarmCallInfo.
 193     assert(allocated_on_stack(),
 194            "copy only into local, this(" PTR_FORMAT ") type %d a[0]=(" PTR_FORMAT ") a[1]=(" PTR_FORMAT ")",
 195            p2i(this), get_allocation_type(), _allocation_t[0], _allocation_t[1]);
 196     // Keep current _allocation_t value;
 197     return *this;
 198 }
 199 
 200 ResourceObj::~ResourceObj() {
 201     // allocated_on_C_heap() also checks that encoded (in _allocation) address == this.
 202     if (!allocated_on_C_heap()) { // ResourceObj::delete() will zap _allocation for C_heap.
 203       _allocation_t[0] = (uintptr_t)badHeapOopVal; // zap type
 204     }
 205 }
 206 #endif // ASSERT
 207 
 208 //--------------------------------------------------------------------------------------
 209 // Non-product code
 210 
 211 #ifndef PRODUCT
 212 void AllocatedObj::print() const       { print_on(tty); }
 213 void AllocatedObj::print_value() const { print_value_on(tty); }
 214 
 215 void AllocatedObj::print_on(outputStream* st) const {
 216   st->print_cr("AllocatedObj(" INTPTR_FORMAT ")", p2i(this));
 217 }
 218 
 219 void AllocatedObj::print_value_on(outputStream* st) const {
 220   st->print("AllocatedObj(" INTPTR_FORMAT ")", p2i(this));
 221 }
 222 
 223 AllocStats::AllocStats() {
 224   start_mallocs      = os::num_mallocs;
 225   start_frees        = os::num_frees;
 226   start_malloc_bytes = os::alloc_bytes;
 227   start_mfree_bytes  = os::free_bytes;
 228   start_res_bytes    = Arena::_bytes_allocated;
 229 }
 230 
 231 julong  AllocStats::num_mallocs() { return os::num_mallocs - start_mallocs; }
 232 julong  AllocStats::alloc_bytes() { return os::alloc_bytes - start_malloc_bytes; }
 233 julong  AllocStats::num_frees()   { return os::num_frees - start_frees; }
 234 julong  AllocStats::free_bytes()  { return os::free_bytes - start_mfree_bytes; }
 235 julong  AllocStats::resource_bytes() { return Arena::_bytes_allocated - start_res_bytes; }
 236 void    AllocStats::print() {
 237   tty->print_cr(UINT64_FORMAT " mallocs (" UINT64_FORMAT "MB), "
 238                 UINT64_FORMAT " frees (" UINT64_FORMAT "MB), " UINT64_FORMAT "MB resrc",
 239                 num_mallocs(), alloc_bytes()/M, num_frees(), free_bytes()/M, resource_bytes()/M);
 240 }
 241 
 242 ReallocMark::ReallocMark() {
 243 #ifdef ASSERT
 244   Thread *thread = Thread::current();
 245   _nesting = thread->resource_area()->nesting();
 246 #endif
 247 }
 248 
 249 void ReallocMark::check() {
 250 #ifdef ASSERT
 251   if (_nesting != Thread::current()->resource_area()->nesting()) {
 252     fatal("allocation bug: array could grow within nested ResourceMark");
 253   }
 254 #endif
 255 }
 256 
 257 #endif // Non-product