1 /*
   2  * Copyright (c) 2019, 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 #include "precompiled.hpp"
  25 #include "gc/z/zArray.inline.hpp"
  26 #include "gc/z/zNMethodAllocator.hpp"
  27 #include "memory/allocation.hpp"
  28 
  29 ZArray<void*> ZNMethodAllocator::_deferred_frees;
  30 bool          ZNMethodAllocator::_defer_frees(false);
  31 
  32 void ZNMethodAllocator::immediate_free(void* data) {
  33   FREE_C_HEAP_ARRAY(uint8_t, data);
  34 }
  35 
  36 void ZNMethodAllocator::deferred_free(void* data) {
  37   _deferred_frees.add(data);
  38 }
  39 
  40 void* ZNMethodAllocator::allocate(size_t size) {
  41   return NEW_C_HEAP_ARRAY(uint8_t, size, mtGC);
  42 }
  43 
  44 void ZNMethodAllocator::free(void* data) {
  45   if (data == NULL) {
  46     return;
  47   }
  48 
  49   if (_defer_frees) {
  50     deferred_free(data);
  51   } else {
  52     immediate_free(data);
  53   }
  54 }
  55 
  56 void ZNMethodAllocator::activate_deferred_frees() {
  57   assert(_deferred_frees.is_empty(), "precondition");
  58   _defer_frees = true;
  59 }
  60 
  61 void ZNMethodAllocator::deactivate_and_process_deferred_frees() {
  62   _defer_frees = false;
  63 
  64   ZArrayIterator<void*> iter(&_deferred_frees);
  65   for (void* data; iter.next(&data);) {
  66     immediate_free(data);
  67   }
  68   _deferred_frees.clear();
  69 }