author | chegar |
Thu, 17 Oct 2019 20:53:35 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58678 | 9cf78a70fa4f |
parent 53416 | 9db898820f63 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
51078 | 2 |
* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved. |
1 | 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 |
* |
|
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
25 |
#include "adlc.hpp" |
|
26 |
||
52583
a3aa8d5380d9
8212779: ADL Parser does not check allocation return values in all cases
jcm
parents:
51092
diff
changeset
|
27 |
void* AllocateHeap(size_t size) { |
a3aa8d5380d9
8212779: ADL Parser does not check allocation return values in all cases
jcm
parents:
51092
diff
changeset
|
28 |
unsigned char* ptr = (unsigned char*) malloc(size); |
a3aa8d5380d9
8212779: ADL Parser does not check allocation return values in all cases
jcm
parents:
51092
diff
changeset
|
29 |
if (ptr == NULL && size != 0) { |
a3aa8d5380d9
8212779: ADL Parser does not check allocation return values in all cases
jcm
parents:
51092
diff
changeset
|
30 |
fprintf(stderr, "Error: Out of memory in ADLC\n"); // logging can cause crash! |
a3aa8d5380d9
8212779: ADL Parser does not check allocation return values in all cases
jcm
parents:
51092
diff
changeset
|
31 |
fflush(stderr); |
a3aa8d5380d9
8212779: ADL Parser does not check allocation return values in all cases
jcm
parents:
51092
diff
changeset
|
32 |
exit(1); |
a3aa8d5380d9
8212779: ADL Parser does not check allocation return values in all cases
jcm
parents:
51092
diff
changeset
|
33 |
} |
a3aa8d5380d9
8212779: ADL Parser does not check allocation return values in all cases
jcm
parents:
51092
diff
changeset
|
34 |
return ptr; |
a3aa8d5380d9
8212779: ADL Parser does not check allocation return values in all cases
jcm
parents:
51092
diff
changeset
|
35 |
} |
a3aa8d5380d9
8212779: ADL Parser does not check allocation return values in all cases
jcm
parents:
51092
diff
changeset
|
36 |
|
53416
9db898820f63
8217291: Failure of ::realloc() should be handled correctly in adlc/forms.cpp
thartmann
parents:
52583
diff
changeset
|
37 |
void* ReAllocateHeap(void* old_ptr, size_t size) { |
9db898820f63
8217291: Failure of ::realloc() should be handled correctly in adlc/forms.cpp
thartmann
parents:
52583
diff
changeset
|
38 |
unsigned char* ptr = (unsigned char*) realloc(old_ptr, size); |
9db898820f63
8217291: Failure of ::realloc() should be handled correctly in adlc/forms.cpp
thartmann
parents:
52583
diff
changeset
|
39 |
if (ptr == NULL && size != 0) { |
9db898820f63
8217291: Failure of ::realloc() should be handled correctly in adlc/forms.cpp
thartmann
parents:
52583
diff
changeset
|
40 |
fprintf(stderr, "Error: Out of memory in ADLC\n"); // logging can cause crash! |
9db898820f63
8217291: Failure of ::realloc() should be handled correctly in adlc/forms.cpp
thartmann
parents:
52583
diff
changeset
|
41 |
fflush(stderr); |
9db898820f63
8217291: Failure of ::realloc() should be handled correctly in adlc/forms.cpp
thartmann
parents:
52583
diff
changeset
|
42 |
exit(1); |
9db898820f63
8217291: Failure of ::realloc() should be handled correctly in adlc/forms.cpp
thartmann
parents:
52583
diff
changeset
|
43 |
} |
9db898820f63
8217291: Failure of ::realloc() should be handled correctly in adlc/forms.cpp
thartmann
parents:
52583
diff
changeset
|
44 |
return ptr; |
9db898820f63
8217291: Failure of ::realloc() should be handled correctly in adlc/forms.cpp
thartmann
parents:
52583
diff
changeset
|
45 |
} |
9db898820f63
8217291: Failure of ::realloc() should be handled correctly in adlc/forms.cpp
thartmann
parents:
52583
diff
changeset
|
46 |
|
19696
bd5a0131bde1
8021954: VM SIGSEGV during classloading on MacOS; hs_err_pid file produced
coleenp
parents:
7397
diff
changeset
|
47 |
void* Chunk::operator new(size_t requested_size, size_t length) throw() { |
1 | 48 |
return CHeapObj::operator new(requested_size + length); |
49 |
} |
|
50 |
||
51 |
void Chunk::operator delete(void* p, size_t length) { |
|
52 |
CHeapObj::operator delete(p); |
|
53 |
} |
|
54 |
||
55 |
Chunk::Chunk(size_t length) { |
|
56 |
_next = NULL; // Chain on the linked list |
|
57 |
_len = length; // Save actual size |
|
58 |
} |
|
59 |
||
60 |
//------------------------------chop------------------------------------------- |
|
61 |
void Chunk::chop() { |
|
62 |
Chunk *k = this; |
|
63 |
while( k ) { |
|
64 |
Chunk *tmp = k->_next; |
|
65 |
// clear out this chunk (to detect allocation bugs) |
|
51078 | 66 |
memset(k, 0xBE, k->_len); |
1 | 67 |
free(k); // Free chunk (was malloc'd) |
68 |
k = tmp; |
|
69 |
} |
|
70 |
} |
|
71 |
||
72 |
void Chunk::next_chop() { |
|
73 |
_next->chop(); |
|
74 |
_next = NULL; |
|
75 |
} |
|
76 |
||
77 |
//------------------------------Arena------------------------------------------ |
|
78 |
Arena::Arena( size_t init_size ) { |
|
79 |
init_size = (init_size+3) & ~3; |
|
80 |
_first = _chunk = new (init_size) Chunk(init_size); |
|
81 |
_hwm = _chunk->bottom(); // Save the cached hwm, max |
|
82 |
_max = _chunk->top(); |
|
83 |
set_size_in_bytes(init_size); |
|
84 |
} |
|
85 |
||
86 |
Arena::Arena() { |
|
87 |
_first = _chunk = new (Chunk::init_size) Chunk(Chunk::init_size); |
|
88 |
_hwm = _chunk->bottom(); // Save the cached hwm, max |
|
89 |
_max = _chunk->top(); |
|
90 |
set_size_in_bytes(Chunk::init_size); |
|
91 |
} |
|
92 |
||
93 |
Arena::Arena( Arena *a ) |
|
94 |
: _chunk(a->_chunk), _hwm(a->_hwm), _max(a->_max), _first(a->_first) { |
|
95 |
set_size_in_bytes(a->size_in_bytes()); |
|
96 |
} |
|
97 |
||
98 |
//------------------------------used------------------------------------------- |
|
99 |
// Total of all Chunks in arena |
|
100 |
size_t Arena::used() const { |
|
101 |
size_t sum = _chunk->_len - (_max-_hwm); // Size leftover in this Chunk |
|
51050
96ea37459ca7
8207011: Remove uses of the register storage class specifier
mikael
parents:
47216
diff
changeset
|
102 |
Chunk *k = _first; |
1 | 103 |
while( k != _chunk) { // Whilst have Chunks in a row |
104 |
sum += k->_len; // Total size of this Chunk |
|
105 |
k = k->_next; // Bump along to next Chunk |
|
106 |
} |
|
107 |
return sum; // Return total consumed space. |
|
108 |
} |
|
109 |
||
110 |
//------------------------------grow------------------------------------------- |
|
111 |
// Grow a new Chunk |
|
112 |
void* Arena::grow( size_t x ) { |
|
113 |
// Get minimal required size. Either real big, or even bigger for giant objs |
|
114 |
size_t len = max(x, Chunk::size); |
|
115 |
||
51050
96ea37459ca7
8207011: Remove uses of the register storage class specifier
mikael
parents:
47216
diff
changeset
|
116 |
Chunk *k = _chunk; // Get filled-up chunk address |
1 | 117 |
_chunk = new (len) Chunk(len); |
118 |
||
119 |
if( k ) k->_next = _chunk; // Append new chunk to end of linked list |
|
120 |
else _first = _chunk; |
|
121 |
_hwm = _chunk->bottom(); // Save the cached hwm, max |
|
122 |
_max = _chunk->top(); |
|
123 |
set_size_in_bytes(size_in_bytes() + len); |
|
124 |
void* result = _hwm; |
|
125 |
_hwm += x; |
|
126 |
return result; |
|
127 |
} |
|
128 |
||
129 |
//------------------------------calloc----------------------------------------- |
|
130 |
// Allocate zeroed storage in Arena |
|
131 |
void *Arena::Acalloc( size_t items, size_t x ) { |
|
132 |
size_t z = items*x; // Total size needed |
|
133 |
void *ptr = Amalloc(z); // Get space |
|
134 |
memset( ptr, 0, z ); // Zap space |
|
135 |
return ptr; // Return space |
|
136 |
} |
|
137 |
||
138 |
//------------------------------realloc---------------------------------------- |
|
139 |
// Reallocate storage in Arena. |
|
140 |
void *Arena::Arealloc( void *old_ptr, size_t old_size, size_t new_size ) { |
|
141 |
char *c_old = (char*)old_ptr; // Handy name |
|
142 |
// Stupid fast special case |
|
143 |
if( new_size <= old_size ) { // Shrink in-place |
|
144 |
if( c_old+old_size == _hwm) // Attempt to free the excess bytes |
|
145 |
_hwm = c_old+new_size; // Adjust hwm |
|
146 |
return c_old; |
|
147 |
} |
|
148 |
||
149 |
// See if we can resize in-place |
|
150 |
if( (c_old+old_size == _hwm) && // Adjusting recent thing |
|
151 |
(c_old+new_size <= _max) ) { // Still fits where it sits |
|
152 |
_hwm = c_old+new_size; // Adjust hwm |
|
153 |
return c_old; // Return old pointer |
|
154 |
} |
|
155 |
||
156 |
// Oops, got to relocate guts |
|
157 |
void *new_ptr = Amalloc(new_size); |
|
158 |
memcpy( new_ptr, c_old, old_size ); |
|
159 |
Afree(c_old,old_size); // Mostly done to keep stats accurate |
|
160 |
return new_ptr; |
|
161 |
} |
|
162 |
||
163 |
//------------------------------reset------------------------------------------ |
|
164 |
// Reset this Arena to empty, and return this Arenas guts in a new Arena. |
|
165 |
Arena *Arena::reset(void) { |
|
166 |
Arena *a = new Arena(this); // New empty arena |
|
167 |
_first = _chunk = NULL; // Normal, new-arena initialization |
|
168 |
_hwm = _max = NULL; |
|
169 |
return a; // Return Arena with guts |
|
170 |
} |
|
171 |
||
172 |
//------------------------------contains--------------------------------------- |
|
173 |
// Determine if pointer belongs to this Arena or not. |
|
174 |
bool Arena::contains( const void *ptr ) const { |
|
175 |
if( (void*)_chunk->bottom() <= ptr && ptr < (void*)_hwm ) |
|
176 |
return true; // Check for in this chunk |
|
177 |
for( Chunk *c = _first; c; c = c->_next ) |
|
178 |
if( (void*)c->bottom() <= ptr && ptr < (void*)c->top()) |
|
179 |
return true; // Check for every chunk in Arena |
|
180 |
return false; // Not in any Chunk, so not in Arena |
|
181 |
} |
|
182 |
||
183 |
//----------------------------------------------------------------------------- |
|
184 |
// CHeapObj |
|
185 |
||
19696
bd5a0131bde1
8021954: VM SIGSEGV during classloading on MacOS; hs_err_pid file produced
coleenp
parents:
7397
diff
changeset
|
186 |
void* CHeapObj::operator new(size_t size) throw() { |
52583
a3aa8d5380d9
8212779: ADL Parser does not check allocation return values in all cases
jcm
parents:
51092
diff
changeset
|
187 |
return (void *) AllocateHeap(size); |
1 | 188 |
} |
189 |
||
190 |
void CHeapObj::operator delete(void* p){ |
|
191 |
free(p); |
|
192 |
} |