1 /*
2 * Copyright (c) 2015, 2017, 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 */
32 if (area->size() >= size) {
33 if (area->size() == size) {
34 // Exact match, remove area
35 const uintptr_t start = area->start();
36 _freelist.remove(area);
37 delete area;
38 return start;
39 } else {
40 // Larger than requested, shrink area
41 const uintptr_t start = area->start();
42 area->shrink_from_front(size);
43 return start;
44 }
45 }
46 }
47
48 // Out of memory
49 return UINTPTR_MAX;
50 }
51
52 uintptr_t ZMemoryManager::alloc_from_back(size_t size) {
53 ZListReverseIterator<ZMemory> iter(&_freelist);
54 for (ZMemory* area; iter.next(&area);) {
55 if (area->size() >= size) {
56 if (area->size() == size) {
57 // Exact match, remove area
58 const uintptr_t start = area->start();
59 _freelist.remove(area);
60 delete area;
61 return start;
62 } else {
63 // Larger than requested, shrink area
64 area->shrink_from_back(size);
65 return area->end();
66 }
67 }
68 }
69
70 // Out of memory
71 return UINTPTR_MAX;
72 }
73
74 void ZMemoryManager::free(uintptr_t start, size_t size) {
75 assert(start != UINTPTR_MAX, "Invalid address");
76 const uintptr_t end = start + size;
77
78 ZListIterator<ZMemory> iter(&_freelist);
79 for (ZMemory* area; iter.next(&area);) {
80 if (start < area->start()) {
81 ZMemory* const prev = _freelist.prev(area);
82 if (prev != NULL && start == prev->end()) {
83 if (end == area->start()) {
84 // Merge with prev and current area
85 prev->grow_from_back(size + area->size());
86 _freelist.remove(area);
87 delete area;
88 } else {
89 // Merge with prev area
90 prev->grow_from_back(size);
|
1 /*
2 * Copyright (c) 2015, 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 */
32 if (area->size() >= size) {
33 if (area->size() == size) {
34 // Exact match, remove area
35 const uintptr_t start = area->start();
36 _freelist.remove(area);
37 delete area;
38 return start;
39 } else {
40 // Larger than requested, shrink area
41 const uintptr_t start = area->start();
42 area->shrink_from_front(size);
43 return start;
44 }
45 }
46 }
47
48 // Out of memory
49 return UINTPTR_MAX;
50 }
51
52 uintptr_t ZMemoryManager::alloc_from_front_at_most(size_t size, size_t* allocated) {
53 ZMemory* area = _freelist.first();
54 if (area != NULL) {
55 if (area->size() <= size) {
56 // Smaller than or equal to requested, remove area
57 const uintptr_t start = area->start();
58 *allocated = area->size();
59 _freelist.remove(area);
60 delete area;
61 return start;
62 } else {
63 // Larger than requested, shrink area
64 const uintptr_t start = area->start();
65 area->shrink_from_front(size);
66 *allocated = size;
67 return start;
68 }
69 }
70
71 // Out of memory
72 *allocated = 0;
73 return UINTPTR_MAX;
74 }
75
76 uintptr_t ZMemoryManager::alloc_from_back(size_t size) {
77 ZListReverseIterator<ZMemory> iter(&_freelist);
78 for (ZMemory* area; iter.next(&area);) {
79 if (area->size() >= size) {
80 if (area->size() == size) {
81 // Exact match, remove area
82 const uintptr_t start = area->start();
83 _freelist.remove(area);
84 delete area;
85 return start;
86 } else {
87 // Larger than requested, shrink area
88 area->shrink_from_back(size);
89 return area->end();
90 }
91 }
92 }
93
94 // Out of memory
95 return UINTPTR_MAX;
96 }
97
98 uintptr_t ZMemoryManager::alloc_from_back_at_most(size_t size, size_t* allocated) {
99 ZMemory* area = _freelist.last();
100 if (area != NULL) {
101 if (area->size() <= size) {
102 // Smaller than or equal to requested, remove area
103 const uintptr_t start = area->start();
104 *allocated = area->size();
105 _freelist.remove(area);
106 delete area;
107 return start;
108 } else {
109 // Larger than requested, shrink area
110 area->shrink_from_back(size);
111 *allocated = size;
112 return area->end();
113 }
114 }
115
116 // Out of memory
117 *allocated = 0;
118 return UINTPTR_MAX;
119 }
120
121 void ZMemoryManager::free(uintptr_t start, size_t size) {
122 assert(start != UINTPTR_MAX, "Invalid address");
123 const uintptr_t end = start + size;
124
125 ZListIterator<ZMemory> iter(&_freelist);
126 for (ZMemory* area; iter.next(&area);) {
127 if (start < area->start()) {
128 ZMemory* const prev = _freelist.prev(area);
129 if (prev != NULL && start == prev->end()) {
130 if (end == area->start()) {
131 // Merge with prev and current area
132 prev->grow_from_back(size + area->size());
133 _freelist.remove(area);
134 delete area;
135 } else {
136 // Merge with prev area
137 prev->grow_from_back(size);
|