2023-12-01 15:36:36 +00:00
|
|
|
#include <librr/memory.h>
|
2024-01-18 20:46:29 +00:00
|
|
|
#include <librr/strutil.h>
|
|
|
|
#include <stdlib.h>
|
2023-12-01 15:36:36 +00:00
|
|
|
|
|
|
|
bool_t rr_mem_overlap(void *block1, usz_t length1, void *block2, usz_t length2)
|
|
|
|
{
|
|
|
|
// Make block1 always be before block2
|
|
|
|
if(block1 > block2)
|
|
|
|
{
|
|
|
|
void *block_backup = block2;
|
|
|
|
usz_t length_backup = length2;
|
|
|
|
block2 = block1;
|
|
|
|
length2 = length1;
|
|
|
|
block1 = block_backup;
|
|
|
|
length1 = length_backup;
|
|
|
|
}
|
|
|
|
usz_t start1 = (usz_t) block1;
|
|
|
|
usz_t start2 = (usz_t) block2;
|
2023-12-12 22:33:26 +00:00
|
|
|
usz_t end1 = start1 + length1;
|
2023-12-01 15:36:36 +00:00
|
|
|
usz_t end2 = start2 + length2;
|
|
|
|
|
|
|
|
if(end2 > start1)
|
|
|
|
{
|
2023-12-12 22:33:26 +00:00
|
|
|
if(start2 > end1) return FALSE;
|
2023-12-01 15:36:36 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool_t rr_memequ(void *block1, void *block2, usz_t count)
|
|
|
|
{
|
|
|
|
u8_t *block1_8 = block1;
|
|
|
|
u8_t *block2_8 = block2;
|
|
|
|
for(usz_t index = 0; index < count; ++index)
|
|
|
|
if(block1_8[index] != block2_8[index]) return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rr_memset(void *destination, usz_t count, u8_t value)
|
|
|
|
{
|
|
|
|
u8_t *destination_8 = destination;
|
|
|
|
for(usz_t index = 0; index < count; ++index)
|
|
|
|
destination_8[index] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
usz_t rr_memrep(void *destination, usz_t num_bytes, void *sequence, usz_t len_sequence)
|
|
|
|
{
|
|
|
|
if(rr_mem_overlap(destination, num_bytes, sequence, len_sequence)) return 0;
|
|
|
|
u8_t *destination_8 = destination;
|
|
|
|
u8_t *sequence_8 = sequence;
|
|
|
|
for(usz_t index = 0; index < num_bytes; ++index)
|
|
|
|
destination_8[index] = sequence_8[index % len_sequence];
|
|
|
|
return (num_bytes / len_sequence) // How many full repetitions are performed
|
|
|
|
+ ((num_bytes % len_sequence) != 0); // One more if a partial repetiton is done
|
|
|
|
}
|
2023-12-02 16:11:33 +00:00
|
|
|
|
|
|
|
void rr_memcopy(void *destination, void *source, usz_t num_bytes)
|
|
|
|
{
|
|
|
|
if(rr_mem_overlap(destination, num_bytes, source, num_bytes)) return;
|
|
|
|
u8_t *destination_8 = destination;
|
|
|
|
u8_t *source_8 = source;
|
|
|
|
for(usz_t index = 0; index < num_bytes; ++index)
|
|
|
|
destination_8[index] = source_8[index];
|
|
|
|
}
|
2024-01-18 20:46:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rr_arena_s rr_new_arena(usz_t capacity)
|
|
|
|
{
|
|
|
|
rr_arena_s arena;
|
|
|
|
arena.capacity = capacity;
|
|
|
|
arena.offset = 0;
|
|
|
|
arena.allocation = malloc(capacity);
|
|
|
|
return arena;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rr_delete_arena(rr_arena_s *arena)
|
|
|
|
{
|
|
|
|
if(arena->allocation == NULL) return;
|
|
|
|
free(arena->allocation);
|
|
|
|
arena->allocation = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void * rr_arena_alloc(rr_arena_s *arena, usz_t length)
|
|
|
|
{
|
|
|
|
if((arena->offset + length) > arena->capacity)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
void *block = &arena->allocation[arena->offset];
|
|
|
|
arena->offset += length;
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
char * rr_arena_clone_string(rr_arena_s *arena, char *string)
|
|
|
|
{
|
|
|
|
usz_t len_string = rr_measure_string(string);
|
|
|
|
char *cloned_string = rr_arena_alloc(arena, len_string+1);
|
|
|
|
rr_memcopy(cloned_string, string, len_string);
|
|
|
|
cloned_string[len_string] = 0x00;
|
|
|
|
|
|
|
|
return cloned_string;
|
|
|
|
}
|