Skip to content

Commit

Permalink
Fixup memory allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
jmpews committed Mar 1, 2024
1 parent f62f446 commit a513555
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
12 changes: 6 additions & 6 deletions source/MemoryAllocator/MemoryAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ using CodeMemBlock = MemBlock;
using DataMemBlock = MemBlock;

struct MemoryAllocator {
stl::vector<simple_linear_allocator_t> code_page_allocators;
stl::vector<simple_linear_allocator_t> data_page_allocators;
stl::vector<simple_linear_allocator_t *> code_page_allocators;
stl::vector<simple_linear_allocator_t *> data_page_allocators;

inline static MemoryAllocator *Shared();

Expand All @@ -72,8 +72,8 @@ struct MemoryAllocator {

uint8_t *result = nullptr;
auto allocators = is_exec ? code_page_allocators : data_page_allocators;
for (auto &allocator : allocators) {
result = (uint8_t *)allocator.alloc(in_size);
for (auto allocator : allocators) {
result = (uint8_t *)allocator->alloc(in_size);
if (result)
break;
}
Expand All @@ -82,13 +82,13 @@ struct MemoryAllocator {
{
auto page = OSMemory::Allocate(OSMemory::PageSize(), kNoAccess);
OSMemory::SetPermission(page, OSMemory::PageSize(), is_exec ? kReadExecute : kReadWrite);
auto page_allocator = simple_linear_allocator_t((uint8_t *)page, OSMemory::PageSize());
auto page_allocator = new simple_linear_allocator_t((uint8_t *)page, OSMemory::PageSize());
if (is_exec)
code_page_allocators.push_back(page_allocator);
else
data_page_allocators.push_back(page_allocator);
}
auto allocator = is_exec ? &code_page_allocators.back() : &data_page_allocators.back();
auto allocator = is_exec ? code_page_allocators.back() : data_page_allocators.back();
result = (uint8_t *)allocator->alloc(in_size);
}
return MemBlock((addr_t)result, in_size);
Expand Down
20 changes: 10 additions & 10 deletions source/MemoryAllocator/NearMemoryAllocator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "dobby/common.h"
#include "MemoryAllocator.h"
#include "Memoryallocator.h"
#include "PlatformUtil/ProcessRuntime.h"
#include <stdint.h>

Expand Down Expand Up @@ -39,8 +39,8 @@ PUBLIC inline void dobby_register_alloc_near_code_callback(dobby_alloc_near_code
}

struct NearMemoryAllocator {
stl::vector<simple_linear_allocator_t> code_page_allocators;
stl::vector<simple_linear_allocator_t> data_page_allocators;
stl::vector<simple_linear_allocator_t*> code_page_allocators;
stl::vector<simple_linear_allocator_t*> data_page_allocators;

inline static NearMemoryAllocator *Shared();

Expand All @@ -63,21 +63,21 @@ struct NearMemoryAllocator {

MemBlock allocNearBlock(uint32_t in_size, MemRange search_range, bool is_exec = true) {
// step-1: search from allocators first
auto allocators = is_exec ? &code_page_allocators : &data_page_allocators;
for (auto &allocator : *allocators) {
auto cursor = allocator.cursor();
auto unused_size = allocator.capacity - allocator.size;
auto allocators = is_exec ? code_page_allocators : data_page_allocators;
for (auto allocator : allocators) {
auto cursor = allocator->cursor();
auto unused_size = allocator->capacity - allocator->size;
auto unused_range = MemRange((addr_t)cursor, unused_size);
auto intersect = search_range.intersect(unused_range);
if (intersect.size < in_size)
continue;

auto gap_size = intersect.addr() - (addr_t)cursor;
if (gap_size) {
allocator.alloc(gap_size);
allocator->alloc(gap_size);
}

auto result = allocator.alloc(in_size);
auto result = allocator->alloc(in_size);
DEBUG_LOG("step-1 allocator: %p, size: %d", (void *)result, in_size);
return {(addr_t)result, (size_t)in_size};
}
Expand Down Expand Up @@ -106,7 +106,7 @@ struct NearMemoryAllocator {
}
OSMemory::SetPermission(unused_page, OSMemory::PageSize(), is_exec ? kReadExecute : kReadWrite);
DEBUG_LOG("step-2 unused page: %p", unused_page);
auto page_allocator = simple_linear_allocator_t((uint8_t *)unused_page, OSMemory::PageSize());
auto page_allocator = new simple_linear_allocator_t((uint8_t *)unused_page, OSMemory::PageSize());
if (is_exec)
code_page_allocators.push_back(page_allocator);
else
Expand Down

0 comments on commit a513555

Please sign in to comment.