define pst_alloc & pst_free macro to use global allocator

This commit is contained in:
2020-02-01 17:57:43 +04:00
parent e27f0c5c18
commit 7c13dfa9e1
8 changed files with 50 additions and 46 deletions
-12
View File
@@ -63,15 +63,3 @@ void pst_alloc_fini(pst_allocator* alloc)
alloc->size = 0; 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;
}
+2 -4
View File
@@ -20,11 +20,11 @@
TYPE NAME; CAT2(TYPE, _init) (&NAME, __VA_ARGS__); TYPE NAME; CAT2(TYPE, _init) (&NAME, __VA_ARGS__);
// allocation with initialization // allocation with initialization
#define pst_alloc(TYPE, NAME, ...) \ #define pst_new(TYPE, NAME, ...) \
TYPE* NAME; NAME = CAT2(TYPE, _new) (__VA_ARGS__); TYPE* NAME; NAME = CAT2(TYPE, _new) (__VA_ARGS__);
// de-initialization and deletion if was previously allocated // de-initialization and deletion if was previously allocated
#define pst_free(TYPE, NAME) \ #define pst_fini(TYPE, NAME) \
CAT2(TYPE, _fini) (NAME); CAT2(TYPE, _fini) (NAME);
typedef struct __pst_allocator pst_allocator; typedef struct __pst_allocator pst_allocator;
@@ -53,6 +53,4 @@ void pst_alloc_init(pst_allocator* alloc);
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);
void pst_alloc_fini(pst_allocator* alloc); void pst_alloc_fini(pst_allocator* alloc);
char* pst_dup(pst_allocator* alloc, const char* str);
#endif /* PST_ALLOCATOR_H_ */ #endif /* PST_ALLOCATOR_H_ */
+14 -1
View File
@@ -152,7 +152,7 @@ bool print_expr_block (pst_context* ctx, Dwarf_Op *exprs, int exprlen, Dwarf_Att
return true; return true;
} }
void pst_context_init(pst_context* ctx, pst_allocator* alloc, ucontext_t* hctx) void pst_context_init(pst_context* ctx, ucontext_t* hctx)
{ {
// global // global
pst_alloc_init(&allocator); pst_alloc_init(&allocator);
@@ -194,3 +194,16 @@ void pst_context_fini(pst_context* ctx)
pst_log_fini(&logger); pst_log_fini(&logger);
pst_alloc_fini(&allocator); pst_alloc_fini(&allocator);
} }
char* pst_dup(const char* str)
{
pst_assert(str);
uint32_t len = strlen(str);
char* dst = (char*)allocator.alloc(len + 1);
memcpy(dst, str, len);
dst[len] = 0;
return dst;
}
+6
View File
@@ -20,6 +20,12 @@
extern pst_log logger; // logger for whole PST library extern pst_log logger; // logger for whole PST library
extern pst_allocator allocator; // custom allocator for PS library extern pst_allocator allocator; // custom allocator for PS library
#define pst_alloc(TYPE) (TYPE*)allocator.alloc(&allocator, sizeof(TYPE))
#define pst_free(NAME) allocator.free(&allocator, NAME)
#define pst_log(SEVERITY, FORMAT, ...) logger.log(&logger, SEVERITY, FORMAT, ##__VA_ARGS__)
char* pst_dup(const char* str);
typedef struct __pst_context { typedef struct __pst_context {
// methods // methods
void (*clean_print) (__pst_context* ctx); void (*clean_print) (__pst_context* ctx);
+6 -6
View File
@@ -33,7 +33,7 @@ void pst_call_site_param_init(pst_call_site_param* param)
pst_call_site_param* pst_call_site_param_new() pst_call_site_param* pst_call_site_param_new()
{ {
pst_call_site_param* param = (pst_call_site_param*)allocator.alloc(&allocator, sizeof(pst_call_site_param)); pst_call_site_param* param = pst_alloc(pst_call_site_param);
if(param) { if(param) {
pst_call_site_param_init(param); pst_call_site_param_init(param);
param->allocated = true; param->allocated = true;
@@ -45,12 +45,12 @@ pst_call_site_param* pst_call_site_param_new()
void pst_call_site_param_fini(pst_call_site_param* param) void pst_call_site_param_fini(pst_call_site_param* param)
{ {
if(param->name) { if(param->name) {
allocator.free(&allocator, param->name); pst_free(param->name);
param->name = NULL; param->name = NULL;
} }
if(param->allocated) { if(param->allocated) {
allocator.free(&allocator, param); pst_free(param);
} }
} }
@@ -165,7 +165,7 @@ void pst_call_site_init(pst_call_site* site, pst_context* c, uint64_t tgt, const
pst_call_site* pst_call_site_new(pst_context* c, uint64_t tgt, const char* orn) pst_call_site* pst_call_site_new(pst_context* c, uint64_t tgt, const char* orn)
{ {
pst_call_site* nc = (pst_call_site*)allocator.alloc(&allocator, sizeof(pst_call_site)); pst_call_site* nc = pst_alloc(pst_call_site);
if(nc) { if(nc) {
pst_call_site_init(nc, c, tgt, orn); pst_call_site_init(nc, c, tgt, orn);
nc->allocated = true; nc->allocated = true;
@@ -177,12 +177,12 @@ pst_call_site* pst_call_site_new(pst_context* c, uint64_t tgt, const char* orn)
void pst_call_site_fini(pst_call_site* site) void pst_call_site_fini(pst_call_site* site)
{ {
if(site->origin) { if(site->origin) {
allocator.free(&allocator, site->origin); pst_free(site->origin);
site->origin = NULL; site->origin = NULL;
} }
if(site->allocated) { if(site->allocated) {
allocator.free(&allocator, site); pst_free(site);
} }
} }
+6 -8
View File
@@ -26,11 +26,9 @@ void pst_dwarf_op_init(pst_dwarf_op* dwop, uint8_t op, uint64_t a1, uint64_t a2)
dwop->allocated = false; dwop->allocated = false;
} }
pst_dwarf_op* pst_dwarf_op_new(pst_allocator* alloc, uint8_t op, uint64_t a1, uint64_t a2) pst_dwarf_op* pst_dwarf_op_new(uint8_t op, uint64_t a1, uint64_t a2)
{ {
pst_assert(alloc); pst_dwarf_op* nop = pst_alloc(pst_dwarf_op);
pst_dwarf_op* nop = (pst_dwarf_op*)alloc->alloc(alloc, sizeof(pst_dwarf_op));
if(nop) { if(nop) {
pst_dwarf_op_init(nop, op, a1, a2); pst_dwarf_op_init(nop, op, a1, a2);
nop->allocated = true; nop->allocated = true;
@@ -44,7 +42,7 @@ void pst_dwarf_op_fini(pst_dwarf_op* dwop)
assert(dwop); assert(dwop);
if(dwop->allocated) { if(dwop->allocated) {
allocator.free(&allocator, dwop); pst_free(dwop);
} else { } else {
dwop->operation = 0; dwop->operation = 0;
dwop->arg1 = 0; dwop->arg1 = 0;
@@ -76,7 +74,7 @@ bool expr_is_equal(pst_dwarf_expr* lhs, pst_dwarf_expr* rhs)
pst_dwarf_op* expr_add_op(pst_dwarf_expr* expr, uint8_t operation, uint64_t arg1, uint64_t arg2) pst_dwarf_op* expr_add_op(pst_dwarf_expr* expr, uint8_t operation, uint64_t arg1, uint64_t arg2)
{ {
pst_alloc(pst_dwarf_op, op, operation, arg1, arg2); pst_new(pst_dwarf_op, op, operation, arg1, arg2);
list_add_bottom(&expr->operations, &op->node); list_add_bottom(&expr->operations, &op->node);
return op; return op;
@@ -158,7 +156,7 @@ void pst_dwarf_expr_init(pst_dwarf_expr* expr)
pst_dwarf_expr* pst_dwarf_expr_new() pst_dwarf_expr* pst_dwarf_expr_new()
{ {
pst_dwarf_expr* ne = allocator.alloc(sizeof(pst_dwarf_expr)); pst_dwarf_expr* ne = pst_alloc(pst_dwarf_expr);
if(ne) { if(ne) {
pst_dwarf_expr_init(ne); pst_dwarf_expr_init(ne);
ne->allocated = true; ne->allocated = true;
@@ -171,6 +169,6 @@ void pst_dwarf_expr_fini(pst_dwarf_expr* expr)
{ {
expr->clean(); expr->clean();
if(expr->allocated) { if(expr->allocated) {
allocator.free(expr); pst_free(expr);
} }
} }
+10 -9
View File
@@ -10,6 +10,7 @@
#include "dwarf_parameter.h" #include "dwarf_parameter.h"
#include "dwarf_utils.h" #include "dwarf_utils.h"
#include "context.h"
// //
// pst_type // pst_type
@@ -17,14 +18,14 @@
void pst_type_init(pst_type* t, const char* name, uint32_t type) void pst_type_init(pst_type* t, const char* name, uint32_t type)
{ {
list_node_init(&t->node); list_node_init(&t->node);
t->name = pst_dup(&allocator, name); t->name = pst_dup(name);
t->type = type; t->type = type;
t->allocated = false; t->allocated = false;
} }
pst_type* pst_type_new(const char* name, uint32_t type) pst_type* pst_type_new(const char* name, uint32_t type)
{ {
pst_type* nt = (pst_type*)allocator.alloc(&allocator, sizeof(pst_type)); pst_type* nt = pst_alloc(pst_type);
if(nt) { if(nt) {
pst_type_init(nt, name, type); pst_type_init(nt, name, type);
nt->allocated = true; nt->allocated = true;
@@ -35,9 +36,9 @@ pst_type* pst_type_new(const char* name, uint32_t type)
void pst_type_fini(pst_type* t) void pst_type_fini(pst_type* t)
{ {
allocator.free(&allocator, t->name); pst_free(t->name);
if(t->allocated) { if(t->allocated) {
allocator.free(&allocator, t); pst_free(t);
} }
} }
@@ -148,7 +149,7 @@ bool param_handle_type(pst_parameter* param, Dwarf_Attribute* base)
pst_type* param_add_type(pst_parameter* param, const char* name, int type) pst_type* param_add_type(pst_parameter* param, const char* name, int type)
{ {
pst_alloc(pst_type, t, name, type); pst_new(pst_type, t, name, type);
list_add_bottom(&param->types, &t->node); list_add_bottom(&param->types, &t->node);
return t; return t;
@@ -182,7 +183,7 @@ bool param_handle_dwarf(pst_parameter* param, Dwarf_Die* result, pst_function* f
Dwarf_Attribute attr_mem; Dwarf_Attribute attr_mem;
Dwarf_Attribute* attr; Dwarf_Attribute* attr;
param->name = pst_dup(&allocator, dwarf_diename(result)); param->name = pst_dup(dwarf_diename(result));
param->is_variable = (dwarf_tag(result) == DW_TAG_variable); param->is_variable = (dwarf_tag(result) == DW_TAG_variable);
dwarf_decl_line(result, (int*)&param->line); dwarf_decl_line(result, (int*)&param->line);
@@ -284,7 +285,7 @@ void pst_parameter_init(pst_parameter*param, pst_context* ctx)
pst_parameter* pst_parameter_new(pst_context* ctx) pst_parameter* pst_parameter_new(pst_context* ctx)
{ {
pst_parameter* param = (pst_parameter*)allocator.alloc(&allocator, sizeof(pst_parameter)); pst_parameter* param = pst_alloc(pst_parameter);
if(param) { if(param) {
pst_parameter_init(param, ctx); pst_parameter_init(param, ctx);
param->allocated = true; param->allocated = true;
@@ -296,10 +297,10 @@ void pst_parameter_fini(pst_parameter* param)
{ {
param->clear(); param->clear();
if(param->name) { if(param->name) {
allocator.free(param->name); pst_free(param->name);
} }
if(param->allocated) { if(param->allocated) {
allocator.free(&allocator, param); pst_free(param);
} }
} }
+6 -6
View File
@@ -78,7 +78,7 @@ pst_dwarf_value* pst_dwarf_value_new(char* v, uint32_t s, int t)
{ {
pst_assert(v); pst_assert(v);
pst_dwarf_value* nv = (pst_dwarf_value*)allocator.alloc(&allocator, sizeof(pst_dwarf_value)); pst_dwarf_value* nv = pst_alloc(pst_dwarf_value);
if(nv) { if(nv) {
pst_dwarf_value_init(nv, v, s, t); pst_dwarf_value_init(nv, v, s, t);
nv->allocated = true; nv->allocated = true;
@@ -93,7 +93,7 @@ void pst_dwarf_value_fini(pst_dwarf_value* value)
pst_assert(value); pst_assert(value);
if(value->allocated) { if(value->allocated) {
allocator.free(&allocator, value); pst_free(value);
} else { } else {
value->type = DWARF_TYPE_INVALID; value->type = DWARF_TYPE_INVALID;
value->value.uint64 = 0; value->value.uint64 = 0;
@@ -105,7 +105,7 @@ void pst_dwarf_value_fini(pst_dwarf_value* value)
// DWARF stack // DWARF stack
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
void stack_push(pst_dwarf_stack* st, void* v, uint32_t s, int t) { void stack_push(pst_dwarf_stack* st, void* v, uint32_t s, int t) {
pst_alloc(pst_dwarf_value, value, (char*)v, s, t); pst_new(pst_dwarf_value, value, (char*)v, s, t);
list_add_head(&st->values, &value->node); list_add_head(&st->values, &value->node);
} }
@@ -187,7 +187,7 @@ bool stack_calc(pst_dwarf_stack* st, Dwarf_Op *exprs, int expr_len, Dwarf_Attrib
return false; return false;
} }
pst_alloc(pst_dwarf_op, op, exprs[i].atom, exprs[i].number, exprs[i].number2); pst_new(pst_dwarf_op, op, exprs[i].atom, exprs[i].number, exprs[i].number2);
list_add_bottom(&st->expr, &op->node); list_add_bottom(&st->expr, &op->node);
pst_dwarf_value* v = st->get(st, 0); pst_dwarf_value* v = st->get(st, 0);
@@ -280,7 +280,7 @@ pst_dwarf_stack* pst_dwarf_stack_new(pst_context* ctx)
{ {
pst_assert(ctx); pst_assert(ctx);
pst_alloc(pst_dwarf_stack, ns, ctx); pst_new(pst_dwarf_stack, ns, ctx);
if(ns) { if(ns) {
pst_dwarf_stack_init(ns, ctx); pst_dwarf_stack_init(ns, ctx);
ns->allocated = true; ns->allocated = true;
@@ -295,7 +295,7 @@ void pst_dwarf_stack_fini(pst_dwarf_stack* st)
st->clear(st); st->clear(st);
if(st->allocated) { if(st->allocated) {
allocator.free(&allocator, st); pst_free(st);
} else { } else {
st->ctx = NULL; st->ctx = NULL;
} }