Skip to content

Commit

Permalink
Fixed implementation of Memory-related functionalities.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed May 21, 2024
1 parent 4e1ad58 commit 8856159
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
3 changes: 1 addition & 2 deletions sdk/librishka/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class Memory final {
*/
static any realloc(any ptr, usize size);


/**
* @brief Free memory.
*
Expand All @@ -93,7 +92,7 @@ class Memory final {
* @param n The number of bytes to set.
* @return Pointer to the memory block.
*/
static any set(any dest, i32 c, usize n);
static any set(any dest, u8 c, usize n);
};

#endif /* LIBRISHKA_MEM_H */
4 changes: 1 addition & 3 deletions sdk/librishka_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,7 @@ static inline rune rt_strpass() {
}

static inline string get_rt_string(u32 len) {
string str;
Memory::set(str, 0, len + 1);

string str = (string) Memory::alloc(len + 1);
for(u32 i = 0; i < len; i++)
str[i] = rt_strpass();

Expand Down
53 changes: 30 additions & 23 deletions sdk/librishka_mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
#include "librishka.h"
#include "librishka_impl.hpp"

#define ALIGN_8BIT(x) (((((x) - 1) >> 3) << 3) + 8)
#define MEMORY_POOL_SIZE 2048 * 2048
#define MEMORY_POOL_SIZE 524288U

typedef struct memory_pool {
u64 size;
Expand All @@ -30,8 +29,11 @@ typedef struct memory_pool {
static u8 memory_pool_block[MEMORY_POOL_SIZE];
static memory_pool* free_list = (memory_pool*) nil;

inline void split_block(memory_pool* block, u64 size) {
memory_pool* new_block = (memory_pool*)((u32*) block + sizeof(memory_pool) + size);
#define ALIGN8(x) (((((x) - 1) >> 3) << 3) + 8)

static void split_block(memory_pool* block, u64 size) {
memory_pool* new_block = (memory_pool*)
((u32*) block + sizeof(memory_pool) + size);

new_block->size = block->size - size - sizeof(memory_pool);
new_block->free = 1;
Expand All @@ -42,7 +44,7 @@ inline void split_block(memory_pool* block, u64 size) {
block->next = new_block;
}

inline void merge_blocks() {
static void merge_blocks() {
memory_pool* current = free_list;

while(current != nil && current->next != nil) {
Expand All @@ -57,14 +59,13 @@ inline void merge_blocks() {

void Memory::initialize() {
free_list = (memory_pool*) memory_pool_block;

free_list->size = MEMORY_POOL_SIZE - sizeof(memory_pool);
free_list->free = 1;
free_list->next = (memory_pool*) nil;
}

any Memory::alloc(usize size) {
size = ALIGN_8BIT(size);
size = ALIGN8(size);

memory_pool* current = free_list;
memory_pool* previous = (memory_pool*) nil;
Expand Down Expand Up @@ -95,39 +96,45 @@ void Memory::free(any ptr) {
merge_blocks();
}

any Memory::set(any ptr, i32 count, usize num) {
u8* p = (u8*) ptr;

while(num--)
*p++ = (u8) count;
return ptr;
}

any Memory::calloc(usize num, usize size) {
usize total_size = num * size;
any ptr = Memory::alloc(total_size);

if(ptr)
if(ptr != nil)
Memory::set(ptr, 0, total_size);
return ptr;
}

any Memory::realloc(void* ptr, usize size) {
any Memory::realloc(any ptr, usize size) {
if(ptr == nil)
return Memory::alloc(size);

if(size == 0) {
Memory::free(ptr);
return nil;
}

memory_pool* block = (memory_pool*)((u8*) ptr - sizeof(memory_pool));
if(block->size >= size)
return ptr;

any new_ptr = Memory::alloc(size);
if(new_ptr) {
usize copy_size = block->size < size ? block->size : size;
for(usize i = 0; i < copy_size; i++)
((char*)new_ptr)[i] = ((char*)ptr)[i];
if(new_ptr == nil)
return nil;

Memory::free(ptr);
}
usize copy_size = (block->size < size) ? block->size : size;
for(usize i = 0; i < copy_size; i++)
((rune*) new_ptr)[i] = ((rune*) ptr)[i];

Memory::free(ptr);
return new_ptr;
}

__attribute__((optimize("no-tree-loop-distribute-patterns")))
any Memory::set(any ptr, u8 value, usize num) {
u8* p = (u8*) ptr;

while(num--)
*p++ = value;
return ptr;
}

0 comments on commit 8856159

Please sign in to comment.