feature(arena): Added and documented string-clone function.

This commit is contained in:
Eric-Paul Ickhorn 2024-05-23 00:56:59 +02:00
parent f8749ba238
commit ee22b5df5c
1 changed files with 14 additions and 0 deletions

View File

@ -56,9 +56,16 @@ void ufn_free_arena(ufn_arena_s *arena);
/// @return Pointer to the newly allocated block. /// @return Pointer to the newly allocated block.
void * ufn_arena_alloc(ufn_arena_s *arena, uint32_t size); void * ufn_arena_alloc(ufn_arena_s *arena, uint32_t size);
/// @brief Clones a string into the arena.
/// @param arena Arena to clone string into.
/// @param string String to clone.
/// @return Copied string. The size is immutable.
char * ufn_arena_clone_string(ufn_arena_s *arena, const char *string);
// END OF API, START OF IMPLEMENTATION. // END OF API, START OF IMPLEMENTATION.
#ifdef UFN_IMPLEMENTATION #ifdef UFN_IMPLEMENTATION
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
ufn_arena_s * ufn_new_dynamic_arena(uint32_t capacity, uint32_t max_tree_size) ufn_arena_s * ufn_new_dynamic_arena(uint32_t capacity, uint32_t max_tree_size)
{ {
@ -120,6 +127,13 @@ void * ufn_arena_alloc(ufn_arena_s *arena, uint32_t size)
return block; return block;
} }
char * ufn_arena_clone_string(ufn_arena_s *arena, const char *string)
{
uint32_t len_string = strlen(string);
char *string_copy = ufn_arena_alloc(arena, len_string + 1);
memcpy(string_copy, string, len_string + 1);
return string_copy;
}
#endif // UFN_IMPLEMENTATION #endif // UFN_IMPLEMENTATION