Skip to content

Commit 9341e8d

Browse files
committed
Merge remote-tracking branch 'Xphalnos/arm64' into arm64
Made freeing memory from our arena allocator safer. Replaced free() with munmap().
2 parents 7aac83b + 194b15b commit 9341e8d

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/host/memory/arena.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifdef WIN32
66
#include <Windows.h>
77
#else
8+
#include <unistd.h>
89
#include <sys/mman.h>
910
#endif
1011

@@ -35,7 +36,6 @@ arena_t arena_init(size_t capacity)
3536
};
3637
return arena;
3738
}
38-
// new more memsafe code (ownedbywuigi) (i give up on windows compatibility for now, will stick to the old unsafe code)
3939

4040
void* arena_allocate(memory::arena_t* arena, const std::size_t size)
4141
{
@@ -54,15 +54,29 @@ void arena_reset(memory::arena_t* arena)
5454
}
5555
void arena_free(memory::arena_t* arena)
5656
{
57-
PVM_ASSERT(arena != nullptr);
58-
arena->capacity = 0;
59-
arena->size = 0;
57+
PVM_ASSERT(nullptr != arena);
58+
PVM_ASSERT(nullptr != arena->data);
6059

61-
// TODO(GloriousTaco:memory): Replace free with a memory safe alternative.
6260
#ifdef WIN32
63-
VirtualFree(arena->data, 0, MEM_RELEASE);
61+
size_t size = 0;
62+
const int return_val = VirtualFree(arena->data, size, MEM_RELEASE);
63+
if (0 == return_val)
64+
{
65+
PVM_ASSERT_MSG(false, "Failed to free arena memory");
66+
}
6467
#else
65-
free(arena->data);
68+
long page_size = sysconf(_SC_PAGESIZE);
69+
PVM_ASSERT(page_size > 0);
70+
PVM_ASSERT(arena->capacity > 0);
71+
PVM_ASSERT(0 == ((uintptr_t)arena->data % (size_t)page_size));
72+
int return_val = munmap(arena->data, arena->capacity);
73+
if (-1 == return_val)
74+
{
75+
PVM_ASSERT_MSG(false, "Failed to free arena memory");
76+
}
6677
#endif
78+
79+
arena->capacity = 0;
80+
arena->size = 0;
6781
}
6882
} // namespace pound::host::memory

0 commit comments

Comments
 (0)