jdk

Форк
0
/
growableArray.cpp 
92 строки · 3.5 Кб
1
/*
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.
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 "memory/allocation.inline.hpp"
27
#include "memory/resourceArea.hpp"
28
#include "runtime/javaThread.hpp"
29
#include "utilities/growableArray.hpp"
30

31
void* GrowableArrayResourceAllocator::allocate(int max, int elementSize) {
32
  assert(max >= 0, "integer overflow");
33
  size_t byte_size = elementSize * (size_t) max;
34

35
  return (void*)resource_allocate_bytes(byte_size);
36
}
37

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;
41

42
  return arena->Amalloc(byte_size);
43
}
44

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;
48

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);
52
}
53

54
void GrowableArrayCHeapAllocator::deallocate(void* elements) {
55
  FreeHeap(elements);
56
}
57

58
#ifdef ASSERT
59

60
GrowableArrayNestingCheck::GrowableArrayNestingCheck(bool on_resource_area) :
61
    _nesting(on_resource_area ? Thread::current()->resource_area()->nesting() : 0) {
62
}
63

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");
70
  }
71
}
72

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()) {
76
    return;
77
  }
78

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");
86
}
87

88
void GrowableArrayMetadata::on_resource_area_alloc_check() const {
89
  _nesting_check.on_resource_area_alloc();
90
}
91

92
#endif // ASSERT
93

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.