src/hotspot/share/gc/z/zNMethodAllocator.cpp
author pliden
Mon, 18 Mar 2019 11:50:40 +0100
changeset 54170 8f1771ea1e39
parent 53891 29273bb37241
permissions -rw-r--r--
8220595: ZGC: Introduce ZAttachedArray Reviewed-by: stefank, eosterlund
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53891
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
     1
/*
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
     4
 *
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
     7
 * published by the Free Software Foundation.
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
     8
 *
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    13
 * accompanied this code).
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    14
 *
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    18
 *
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    21
 * questions.
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    22
 */
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    23
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    24
#include "precompiled.hpp"
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    25
#include "gc/z/zArray.inline.hpp"
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    26
#include "gc/z/zNMethodAllocator.hpp"
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    27
#include "memory/allocation.hpp"
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    28
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    29
ZArray<void*> ZNMethodAllocator::_deferred_frees;
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    30
bool          ZNMethodAllocator::_defer_frees(false);
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    31
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    32
void ZNMethodAllocator::immediate_free(void* data) {
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    33
  FREE_C_HEAP_ARRAY(uint8_t, data);
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    34
}
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    35
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    36
void ZNMethodAllocator::deferred_free(void* data) {
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    37
  _deferred_frees.add(data);
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    38
}
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    39
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    40
void* ZNMethodAllocator::allocate(size_t size) {
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    41
  return NEW_C_HEAP_ARRAY(uint8_t, size, mtGC);
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    42
}
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    43
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    44
void ZNMethodAllocator::free(void* data) {
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    45
  if (data == NULL) {
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    46
    return;
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    47
  }
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    48
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    49
  if (_defer_frees) {
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    50
    deferred_free(data);
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    51
  } else {
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    52
    immediate_free(data);
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    53
  }
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    54
}
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    55
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    56
void ZNMethodAllocator::activate_deferred_frees() {
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    57
  assert(_deferred_frees.is_empty(), "precondition");
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    58
  _defer_frees = true;
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    59
}
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    60
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    61
void ZNMethodAllocator::deactivate_and_process_deferred_frees() {
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    62
  _defer_frees = false;
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    63
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    64
  ZArrayIterator<void*> iter(&_deferred_frees);
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    65
  for (void* data; iter.next(&data);) {
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    66
    immediate_free(data);
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    67
  }
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    68
  _deferred_frees.clear();
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents:
diff changeset
    69
}