all code except of pst_function & pst_handler rewritten on C language.

Logger nad Allocator decided to be a global for all library
This commit is contained in:
2020-02-01 15:19:55 +04:00
parent fcdb15ba95
commit 8251ba526f
30 changed files with 2073 additions and 2068 deletions
+35 -19
View File
@@ -7,55 +7,71 @@
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include "common.h"
#include "allocator.h"
void heap_free(pst_allocator& alloc, void* buff)
void heap_free(pst_allocator* alloc, void* buff)
{
alloc.size -= malloc_usable_size(buff);
alloc->size -= malloc_usable_size(buff);
free(buff);
}
void* heap_alloc(pst_allocator& alloc, uint32_t size)
void* heap_alloc(pst_allocator* alloc, uint32_t size)
{
void* buff = malloc(size);
alloc.size += malloc_usable_size(buff);
alloc->size += malloc_usable_size(buff);
return buff;
}
void* heap_realloc(pst_allocator& alloc, void* buff, uint32_t new_size)
void* heap_realloc(pst_allocator* alloc, void* buff, uint32_t new_size)
{
alloc.size -= malloc_usable_size(buff);
alloc->size -= malloc_usable_size(buff);
void* new_buff = realloc(buff, new_size);
alloc.size += malloc_usable_size(new_buff);
alloc->size += malloc_usable_size(new_buff);
return new_buff;
}
void pst_alloc_init(pst_allocator& alloc)
void pst_alloc_init(pst_allocator* alloc)
{
alloc.type = ALLOC_HEAP;
alloc.base = NULL;
alloc.size = 0;
alloc->type = ALLOC_HEAP;
alloc->base = NULL;
alloc->size = 0;
alloc.alloc = heap_alloc;
alloc.free = heap_free;
alloc.realloc = heap_realloc;
alloc->alloc = heap_alloc;
alloc->free = heap_free;
alloc->realloc = heap_realloc;
return;
}
void pst_alloc_init_custom(pst_allocator& alloc, void* buff, uint32_t size)
void pst_alloc_init_custom(pst_allocator* alloc, void* buff, uint32_t size)
{
// TBD to implement custom allocator
pst_alloc_init(alloc);
return;
}
void pst_alloc_fini(pst_allocator& alloc)
void pst_alloc_fini(pst_allocator* alloc)
{
alloc.type = ALLOC_NONE;
alloc.base = NULL;
alloc.size = 0;
if(alloc->type == ALLOC_HEAP || alloc->type == ALLOC_CUSTOM) {
alloc->type = ALLOC_NONE;
alloc->base = NULL;
alloc->size = 0;
}
}
char* pst_dup(pst_allocator* alloc, const char* str)
{
pst_assert(alloc && str);
uint32_t len = strlen(str);
char* dst = (char*)alloc->alloc(len + 1);
memcpy(dst, str, len);
dst[len] = 0;
return dst;
}