From dc13ca8bfa808f72ba7109a98f7013e59160152e Mon Sep 17 00:00:00 2001 From: Eric-Paul Ickhorn Date: Tue, 28 Nov 2023 19:23:57 +0100 Subject: [PATCH] Added and integrated own memory copy, set and zero-out functions --- code/inc/utility.h | 6 ++++++ code/src/ast.c | 12 ++++++------ code/src/linker.c | 2 +- code/src/tokenizer.c | 4 ++-- code/src/utility.c | 29 +++++++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 code/src/utility.c diff --git a/code/inc/utility.h b/code/inc/utility.h index 5fbe1af..6c0a804 100644 --- a/code/inc/utility.h +++ b/code/inc/utility.h @@ -41,4 +41,10 @@ bool_t pac_rune_is_digit (rune_t rune); bool_t pac_rune_is_blank (rune_t rune); bool_t pac_rune_is_sign (rune_t rune); + + +void pac_memory_copy (void *destination, void *source, usz_t length); +void pac_memory_fill (void *region, usz_t len_region, u8_t byte); +void pac_memory_zero (void *region, usz_t len_region); + #endif // Include Guard (TN_UTIL_TYPES_H) \ No newline at end of file diff --git a/code/src/ast.c b/code/src/ast.c index b542c28..d9dfec0 100644 --- a/code/src/ast.c +++ b/code/src/ast.c @@ -37,7 +37,7 @@ i32_t pac_grow_reference(pac_tlist_s *tlist, pac_ast_reference_s *reference) reference->len_name = len_name; reference->name = malloc(len_name+1); - memcpy(reference->name, name, len_name); + pac_memory_copy(reference->name, name, len_name); reference->name[len_name] = 0x00; return 3; @@ -46,13 +46,13 @@ i32_t pac_grow_reference(pac_tlist_s *tlist, pac_ast_reference_s *reference) i32_t pac_grow_item(pac_tlist_s *tlist, pac_ast_item_s *item) { item->type = PAC_AST_ITEM_INVALID; - memset(item, 0x00, sizeof(pac_ast_item_s)); + pac_memory_zero(item, sizeof(pac_ast_item_s)); if(CURRENT.type == PAC_TOKEN_LIT_STRING) { item->type = PAC_AST_ITEM_LITERAL; item->data.literal.length = CURRENT.length; item->data.literal.string = malloc(item->data.literal.length + 1); - memcpy(item->data.literal.string, CURRENT_STRING, CURRENT.length); + pac_memory_copy(item->data.literal.string, CURRENT_STRING, CURRENT.length); item->data.literal.string[item->data.literal.length] = 0x00; SKIP_TOKEN; return 1; @@ -86,7 +86,7 @@ i32_t pac_grow_variant(pac_tlist_s *tlist, pac_ast_variant_s *variant) { usz_t start_index = tlist->cursor; - memset(variant, 0x00, sizeof(pac_ast_variant_s)); + pac_memory_zero(variant, sizeof(pac_ast_variant_s)); usz_t items_capacity = 8; variant->items = calloc(sizeof(pac_ast_item_s), items_capacity); @@ -119,7 +119,7 @@ i32_t pac_grow_variant(pac_tlist_s *tlist, pac_ast_variant_s *variant) i32_t pac_grow_rule(pac_tlist_s *tlist, pac_ast_rule_s *rule) { - memset(rule, 0x00, sizeof(pac_ast_rule_s)); + pac_memory_zero(rule, sizeof(pac_ast_rule_s)); // Parse the header @@ -155,7 +155,7 @@ i32_t pac_grow_rule(pac_tlist_s *tlist, pac_ast_rule_s *rule) // Parse all variants rule->name = malloc(len_name + 1); - memcpy(rule->name, &tlist->source[name_start], len_name); + pac_memory_copy(rule->name, &tlist->source[name_start], len_name); rule->name[len_name] = 0; usz_t variants_capacity = 4; diff --git a/code/src/linker.c b/code/src/linker.c index 7f888a4..f4c4459 100644 --- a/code/src/linker.c +++ b/code/src/linker.c @@ -15,7 +15,7 @@ void pac_copy_empty_rules_from_ast_to_grammar(pac_grammar_s *grammar, pac_ast_s pac_rule_s *rule = &grammar->rules[rule_index]; usz_t len_rule_name = strlen(ast_rule.name); rule->name = malloc(len_rule_name + 1); - memcpy(rule->name, ast_rule.name, len_rule_name); + pac_memory_copy(rule->name, ast_rule.name, len_rule_name); rule->name[len_rule_name] = 0x00; } } diff --git a/code/src/tokenizer.c b/code/src/tokenizer.c index 6ddcd31..b275274 100644 --- a/code/src/tokenizer.c +++ b/code/src/tokenizer.c @@ -201,7 +201,7 @@ char * pac_stringify_token_type(pac_token_e type) char pac_spaces[256]; char * pac_create_spaces_for_indent(u8_t count) { - memset(pac_spaces, ' ', 256); + pac_memory_fill(pac_spaces, 256, ' '); pac_spaces[count] = 0x00; return &pac_spaces[0]; } @@ -214,7 +214,7 @@ void pac_display_tlist(pac_tlist_s list) pac_token_s token = list.tokens[index]; char content[token.length+1]; - memcpy(&content[0], &list.source[token.offset], token.length); + pac_memory_copy(&content[0], &list.source[token.offset], token.length); content[token.length] = 0; char *token_type_string = pac_stringify_token_type(token.type); diff --git a/code/src/utility.c b/code/src/utility.c new file mode 100644 index 0000000..c02de18 --- /dev/null +++ b/code/src/utility.c @@ -0,0 +1,29 @@ +#include + +void pac_memory_copy(void *destination, void *source, usz_t length) +{ + u8_t *source_8 = source; + u8_t *destination_8 = destination; + usz_t index = 0; + while(index < length) + { + destination_8[index] = source_8[index]; + ++index; + } +} + +void pac_memory_fill(void *region, usz_t len_region, u8_t byte) +{ + u8_t *region_8 = region; + usz_t index = 0; + while(index < len_region) + { + region_8[index] = byte; + ++index; + } +} + +void pac_memory_zero(void *region, usz_t len_region) +{ + pac_memory_fill(region, len_region, 0); +}