File tree Expand file tree Collapse file tree 1 file changed +21
-7
lines changed
Expand file tree Collapse file tree 1 file changed +21
-7
lines changed Original file line number Diff line number Diff line change 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
4040void * arena_allocate (memory::arena_t * arena, const std::size_t size)
4141{
@@ -54,15 +54,29 @@ void arena_reset(memory::arena_t* arena)
5454}
5555void 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
You can’t perform that action at this time.
0 commit comments