Added arena allocator for federal agent...

This commit is contained in:
Ognjen Milan Robovic 2025-04-05 22:15:01 +02:00
parent 7bce39f49b
commit 227b6baac2
2 changed files with 140 additions and 31 deletions

View File

@ -27,8 +27,8 @@ typedef struct {
uint screen_width;
uint screen_height;
char format [terminal_format_length + 1];
char cursor [terminal_cursor_length + 1];
char format [terminal_format_length + 1];
char cursor [terminal_cursor_length + 1];
bool active;
bool signal [signal_count];
@ -293,44 +293,44 @@ static void terminal_render_format (terminal_structure * terminal, const char *
for (; * format != character_null; ++format) {
switch (* format) {
case '\t': {
case ('\t'): {
offset_x += 8;
} break;
case '\n': {
case ('\n'): {
offset_x *= 0;
offset_y += 1;
} break;
case '\r': {
case ('\r'): {
offset_x *= 0;
} break;
case '%': {
case ('%'): {
++format;
switch (* format) {
case '%': {
case ('%'): {
terminal_render_character (terminal, '%', colour, effect, x + offset_x, y + offset_y);
++offset_x;
} break;
case 'i': {
case ('i'): {
char * number = number_to_string (va_arg (list, int));
terminal_render_string (terminal, number, colour, effect, x + offset_x, y + offset_y);
offset_x += string_length (number);
} break;
case 't': {
case ('t'): {
bool toggle = (bool) va_arg (list, int);
terminal_render_toggle (terminal, toggle, x + offset_x, y + offset_y);
offset_x += 3;
} break;
case 'b': {
case ('b'): {
bool boolean = (bool) va_arg (list, int);
terminal_render_string (terminal, (boolean == true) ? "true" : "false", colour, effect, x + offset_x, y + offset_y);
offset_x += (boolean == true) ? 4 : 5;
} break;
case 'c': {
case ('c'): {
char character = (char) va_arg (list, int);
terminal_render_character (terminal, character, colour, effect, x + offset_x, y + offset_y);
++offset_x;
} break;
case 's': {
case ('s'): {
char * string = va_arg (list, char *);
terminal_render_string (terminal, string, colour, effect, x + offset_x, y + offset_y);
offset_x += string_length (string);
@ -341,30 +341,30 @@ static void terminal_render_format (terminal_structure * terminal, const char *
} break;
}
} break;
case '/': {
case ('/'): {
++format;
switch (* format) {
case '/': {
case ('/'): {
terminal_render_character (terminal, '/', colour, effect, x + offset_x, y + offset_y);
++offset_x;
} break;
case 'A': effect = effect_normal; break;
case 'B': effect = effect_bold; break;
case 'C': effect = effect_italic; break;
case 'D': effect = effect_undefined_code; break;
case 'E': effect = effect_underline; break;
case 'F': effect = effect_blink; break;
case 'G': effect = effect_reverse; break;
case 'H': effect = effect_invisible_text; break;
case '0': colour = colour_grey; break;
case '1': colour = colour_red; break;
case '2': colour = colour_green; break;
case '3': colour = colour_yellow; break;
case '4': colour = colour_blue; break;
case '5': colour = colour_pink; break;
case '6': colour = colour_cyan; break;
case '7': colour = colour_white; break;
case '-': {
case ('A'): effect = effect_normal; break;
case ('B'): effect = effect_bold; break;
case ('C'): effect = effect_italic; break;
case ('D'): effect = effect_undefined_code; break;
case ('E'): effect = effect_underline; break;
case ('F'): effect = effect_blink; break;
case ('G'): effect = effect_reverse; break;
case ('H'): effect = effect_invisible_text; break;
case ('0'): colour = colour_grey; break;
case ('1'): colour = colour_red; break;
case ('2'): colour = colour_green; break;
case ('3'): colour = colour_yellow; break;
case ('4'): colour = colour_blue; break;
case ('5'): colour = colour_pink; break;
case ('6'): colour = colour_cyan; break;
case ('7'): colour = colour_white; break;
case ('-'): {
colour = colour_white;
effect = effect_normal;
} break;

109
xrena.h Executable file
View File

@ -0,0 +1,109 @@
/// __ ___ __ ___ _ __ __ _
/// \ \/ / '__/ _ \ '_ \ / _` |
/// > <| | | __/ | | | (_| |
/// /_/\_\_| \___|_| |_|\__,_|
///
/// Copyright (c) 1997 - Ognjen 'xolatile' Milan Robovic
///
/// xolatile@chud.cyou - xrena - Probably the most minimalistic arena allocator possible, and undoubtedly evil in implementation details.
///
/// This program is free software, free as in freedom and as in free beer, you can redistribute it and/or modify it under the terms of the GNU
/// General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version if you wish...
///
/// This program is distributed in the hope that it will be useful, but it is probably not, and without any warranty, without even the implied
/// warranty of merchantability or fitness for a particular purpose, because it is pointless. Please see the GNU (Geenoo) General Public License
/// for more details, if you dare, it is a lot of text that nobody wants to read...
#ifndef arena_block_limit
#define arena_block_limit (1024 * 1024)
#endif
static struct {
ulong block_count;
ulong block_limit;
struct {
ulong count;
ulong capacity;
char * buffer;
} * * block_array;
} * arena = null;
static void arena_initialize (void) {
ulong current = ++arena->block_count - 1;
arena->block_limit = arena_block_limit;
arena->block_array = reallocate (arena->block_array, arena->block_count * sizeof (* arena->block_array));
arena->block_array [current] = allocate (sizeof (* arena));
arena->block_array [current]->buffer = allocate (arena_block_limit);
arena->block_array [current]->count = 0;
arena->block_array [current]->capacity = arena_block_limit;
}
static void arena_deinitialize (void) {
for (ulong index = 0; index < arena->block_count; ++index) {
arena->block_array [index]->buffer = deallocate (arena->block_array [index]->buffer);
arena->block_array [index] = deallocate (arena->block_array [index]);
}
arena->block_array = deallocate (arena->block_array);
arena = deallocate (arena);
}
static void * arena_add (ulong size) {
ulong current = arena->block_count - 1;
if (arena == null) {
clean_up (arena_deinitialize);
arena = allocate (sizeof (* arena));
arena_initialize ();
}
fatal_failure (size > arena->block_limit, "arena_add: Block limit reached.");
if (arena->block_array [current]->count + size > arena->block_array [current]->capacity) {
arena_initialize ();
}
arena->block_array [current]->count += size;
return ((void*) & arena->block_array [current]->buffer [arena->block_array [current]->count - size]);
}
static char * arena_add_data (const void * data, ulong size) {
void * pointer = arena_add (size);
memory_copy (pointer, data, size);
return (pointer);
}
static char * arena_add_file (const char * path, uint flag, bool null_terminate) {
int file = -1;
ulong size = 0;
char * data = null;
file = file_open (path, flag);
size = file_size (path) + (ulong) null_terminate;
data = arena_add (size);
file_read (file, data, size - (ulong) null_terminate);
file = file_close (file);
return (data);
}
static ulong arena_usage (void) {
ulong usage = 0;
for (ulong block = 0; block < arena->block_count; ++block) {
usage += arena->block_array [block]->count;
}
return (usage);
}