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 */ 23 24 #include "precompiled.hpp" 25 #include "gc/z/zList.inline.hpp" 26 #include "gc/z/zMemory.inline.hpp" 27 #include "memory/allocation.inline.hpp" 28 29 uintptr_t ZMemoryManager::alloc_from_front(size_t size) { 30 ZListIterator<ZMemory> iter(&_freelist); 31 for (ZMemory* area; iter.next(&area);) { 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); 138 } 139 } else if (end == area->start()) { 140 // Merge with current area 141 area->grow_from_front(size); 142 } else { 143 // Insert new area before current area 144 assert(end < area->start(), "Areas must not overlap"); 145 ZMemory* new_area = new ZMemory(start, size); 146 _freelist.insert_before(area, new_area); 147 } 148 149 // Done 150 return; 151 } 152 } 153 154 // Insert last 155 ZMemory* const last = _freelist.last(); 156 if (last != NULL && start == last->end()) { 157 // Merge with last area 158 last->grow_from_back(size); 159 } else { 160 // Insert new area last 161 ZMemory* new_area = new ZMemory(start, size); 162 _freelist.insert_last(new_area); 163 } 164 }