2
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25
#include "precompiled.hpp"
26
#include "memory/allocation.inline.hpp"
27
#include "memory/resourceArea.hpp"
28
#include "runtime/javaThread.hpp"
29
#include "utilities/growableArray.hpp"
31
void* GrowableArrayResourceAllocator::allocate(int max, int elementSize) {
32
assert(max >= 0, "integer overflow");
33
size_t byte_size = elementSize * (size_t) max;
35
return (void*)resource_allocate_bytes(byte_size);
38
void* GrowableArrayArenaAllocator::allocate(int max, int element_size, Arena* arena) {
39
assert(max >= 0, "integer overflow");
40
size_t byte_size = element_size * (size_t) max;
42
return arena->Amalloc(byte_size);
45
void* GrowableArrayCHeapAllocator::allocate(int max, int element_size, MEMFLAGS memflags) {
46
assert(max >= 0, "integer overflow");
47
size_t byte_size = element_size * (size_t) max;
49
// memory type has to be specified for C heap allocation
50
assert(memflags != mtNone, "memory type not specified for C heap object");
51
return (void*)AllocateHeap(byte_size, memflags);
54
void GrowableArrayCHeapAllocator::deallocate(void* elements) {
60
GrowableArrayNestingCheck::GrowableArrayNestingCheck(bool on_resource_area) :
61
_nesting(on_resource_area ? Thread::current()->resource_area()->nesting() : 0) {
64
void GrowableArrayNestingCheck::on_resource_area_alloc() const {
65
// Check for insidious allocation bug: if a GrowableArray overflows, the
66
// grown array must be allocated under the same ResourceMark as the original.
67
// Otherwise, the _data array will be deallocated too early.
68
if (_nesting != Thread::current()->resource_area()->nesting()) {
69
fatal("allocation bug: GrowableArray could grow within nested ResourceMark");
73
void GrowableArrayMetadata::init_checks(const GrowableArrayBase* array) const {
74
// Stack allocated arrays support all three element allocation locations
75
if (array->allocated_on_stack_or_embedded()) {
79
// Otherwise there's a strict one-to-one mapping
80
assert(on_C_heap() == array->allocated_on_C_heap(),
81
"growable array must be C heap allocated if elements are");
82
assert(on_resource_area() == array->allocated_on_res_area(),
83
"growable array must be resource allocated if elements are");
84
assert(on_arena() == array->allocated_on_arena(),
85
"growable array must be arena allocated if elements are");
88
void GrowableArrayMetadata::on_resource_area_alloc_check() const {
89
_nesting_check.on_resource_area_alloc();