8232649: ZGC: Add callbacks to ZMemoryManager

Reviewed-by: pliden, eosterlund
This commit is contained in:
Stefan Karlsson 2019-10-28 11:24:11 +01:00
parent 8df718fa82
commit 17a041119f
2 changed files with 101 additions and 14 deletions

View File

@ -26,6 +26,65 @@
#include "gc/z/zMemory.inline.hpp"
#include "memory/allocation.inline.hpp"
ZMemory* ZMemoryManager::create(uintptr_t start, size_t size) {
ZMemory* const area = new ZMemory(start, size);
if (_callbacks._create != NULL) {
_callbacks._create(area);
}
return area;
}
void ZMemoryManager::destroy(ZMemory* area) {
if (_callbacks._destroy != NULL) {
_callbacks._destroy(area);
}
delete area;
}
void ZMemoryManager::shrink_from_front(ZMemory* area, size_t size) {
if (_callbacks._shrink_from_front != NULL) {
_callbacks._shrink_from_front(area, size);
}
area->shrink_from_front(size);
}
void ZMemoryManager::shrink_from_back(ZMemory* area, size_t size) {
if (_callbacks._shrink_from_back != NULL) {
_callbacks._shrink_from_back(area, size);
}
area->shrink_from_back(size);
}
void ZMemoryManager::grow_from_front(ZMemory* area, size_t size) {
if (_callbacks._grow_from_front != NULL) {
_callbacks._grow_from_front(area, size);
}
area->grow_from_front(size);
}
void ZMemoryManager::grow_from_back(ZMemory* area, size_t size) {
if (_callbacks._grow_from_back != NULL) {
_callbacks._grow_from_back(area, size);
}
area->grow_from_back(size);
}
ZMemoryManager::Callbacks::Callbacks() :
_create(NULL),
_destroy(NULL),
_shrink_from_front(NULL),
_shrink_from_back(NULL),
_grow_from_front(NULL),
_grow_from_back(NULL) {}
ZMemoryManager::ZMemoryManager() :
_freelist(),
_callbacks() {}
void ZMemoryManager::register_callbacks(const Callbacks& callbacks) {
_callbacks = callbacks;
}
uintptr_t ZMemoryManager::alloc_from_front(size_t size) {
ZListIterator<ZMemory> iter(&_freelist);
for (ZMemory* area; iter.next(&area);) {
@ -34,12 +93,12 @@ uintptr_t ZMemoryManager::alloc_from_front(size_t size) {
// Exact match, remove area
const uintptr_t start = area->start();
_freelist.remove(area);
delete area;
destroy(area);
return start;
} else {
// Larger than requested, shrink area
const uintptr_t start = area->start();
area->shrink_from_front(size);
shrink_from_front(area, size);
return start;
}
}
@ -57,12 +116,12 @@ uintptr_t ZMemoryManager::alloc_from_front_at_most(size_t size, size_t* allocate
const uintptr_t start = area->start();
*allocated = area->size();
_freelist.remove(area);
delete area;
destroy(area);
return start;
} else {
// Larger than requested, shrink area
const uintptr_t start = area->start();
area->shrink_from_front(size);
shrink_from_front(area, size);
*allocated = size;
return start;
}
@ -81,11 +140,11 @@ uintptr_t ZMemoryManager::alloc_from_back(size_t size) {
// Exact match, remove area
const uintptr_t start = area->start();
_freelist.remove(area);
delete area;
destroy(area);
return start;
} else {
// Larger than requested, shrink area
area->shrink_from_back(size);
shrink_from_back(area, size);
return area->end();
}
}
@ -103,11 +162,11 @@ uintptr_t ZMemoryManager::alloc_from_back_at_most(size_t size, size_t* allocated
const uintptr_t start = area->start();
*allocated = area->size();
_freelist.remove(area);
delete area;
destroy(area);
return start;
} else {
// Larger than requested, shrink area
area->shrink_from_back(size);
shrink_from_back(area, size);
*allocated = size;
return area->end();
}
@ -129,20 +188,20 @@ void ZMemoryManager::free(uintptr_t start, size_t size) {
if (prev != NULL && start == prev->end()) {
if (end == area->start()) {
// Merge with prev and current area
prev->grow_from_back(size + area->size());
grow_from_back(prev, size + area->size());
_freelist.remove(area);
delete area;
} else {
// Merge with prev area
prev->grow_from_back(size);
grow_from_back(prev, size);
}
} else if (end == area->start()) {
// Merge with current area
area->grow_from_front(size);
grow_from_front(area, size);
} else {
// Insert new area before current area
assert(end < area->start(), "Areas must not overlap");
ZMemory* new_area = new ZMemory(start, size);
ZMemory* const new_area = create(start, size);
_freelist.insert_before(area, new_area);
}
@ -155,10 +214,10 @@ void ZMemoryManager::free(uintptr_t start, size_t size) {
ZMemory* const last = _freelist.last();
if (last != NULL && start == last->end()) {
// Merge with last area
last->grow_from_back(size);
grow_from_back(last, size);
} else {
// Insert new area last
ZMemory* new_area = new ZMemory(start, size);
ZMemory* const new_area = create(start, size);
_freelist.insert_last(new_area);
}
}

View File

@ -49,14 +49,42 @@ public:
};
class ZMemoryManager {
public:
typedef void (*CreateDestroyCallback)(const ZMemory* area);
typedef void (*ResizeCallback)(const ZMemory* area, size_t size);
struct Callbacks {
CreateDestroyCallback _create;
CreateDestroyCallback _destroy;
ResizeCallback _shrink_from_front;
ResizeCallback _shrink_from_back;
ResizeCallback _grow_from_front;
ResizeCallback _grow_from_back;
Callbacks();
};
private:
ZList<ZMemory> _freelist;
Callbacks _callbacks;
ZMemory* create(uintptr_t start, size_t size);
void destroy(ZMemory* area);
void shrink_from_front(ZMemory* area, size_t size);
void shrink_from_back(ZMemory* area, size_t size);
void grow_from_front(ZMemory* area, size_t size);
void grow_from_back(ZMemory* area, size_t size);
public:
ZMemoryManager();
void register_callbacks(const Callbacks& callbacks);
uintptr_t alloc_from_front(size_t size);
uintptr_t alloc_from_front_at_most(size_t size, size_t* allocated);
uintptr_t alloc_from_back(size_t size);
uintptr_t alloc_from_back_at_most(size_t size, size_t* allocated);
void free(uintptr_t start, size_t size);
};