Made memory pool braindead simple, we will make complex one in further chapters...

This commit is contained in:
Ognjen Milan Robovic 2023-11-21 14:21:35 -05:00
parent 7de65b2a9d
commit 469e117e78
2 changed files with 31 additions and 71 deletions

View File

@ -22,46 +22,18 @@ static number_t game_set_screen_height (game_t * game, number_t height) { return
@
*/
game_t game;
player_t player;
memory_t memory_pool;
#define MEMORY_LIMIT (1024 * 1024)
static memory_t memorize (memory_t data, number_t size) {
static memory_t memory_data = NULL;
static number_t memory_used = 0;
static number_t memory_block_size = 4096;
static number_t memory_block_used = 0;
static string_t memory_store [MEMORY_LIMIT];
/* Show an example of incremental reallocation with for loop from libhl early prototype. */
static memory_t memorize (number_t size) {
static number_t memory_count = 0;
fatal_failure (size >= memory_block_size, "memorize: You're trying to re/allocate huge chunk of memory.");
fatal_failure (memory_count + size >= MEMORY_LIMIT, "memorize: You have reached the 1 MiB memory limit.");
if (memory_used + size >= memory_block_used * memory_block_size) {
memory_pool = reallocate (memory_pool, ++memory_block_used * memory_block_size);
memory_count += size;
for (number_t offset = 0; offset != memory_block_size; ++offset) {
string_t cast = (string_t) memory_pool + (memory_block_used - 1) * memory_block_size + offset;
* cast = '\0';
}
}
if (data == NULL) {
memory_data = (string_t) memory_pool + memory_used;
memory_used += size;
} else {
memory_data = data;
memory_used += size;
for (number_t offset = 0; offset != size; ++offset) {
string_t cast_0 = (string_t) memory_pool + memory_used - 1 - offset;
string_t cast_1 = (string_t) memory_pool + memory_used - size - 1 - offset;
* cast_0 = * cast_1;
}
}
return (memory_data);
return ((memory_t) ((string_t) memory_store + memory_count - size));
}
static void move_player (void) { ++player.x; }
@ -86,6 +58,9 @@ static void game_synchronize (void) {
return;
}
game_t game;
player_t player;
skill_t * game_skill (string_t name, bundle_t * points, ...) {
skill_t * skill;
va_list list;
@ -93,22 +68,17 @@ skill_t * game_skill (string_t name, bundle_t * points, ...) {
va_start (list, points);
//~skill = allocate ((int) sizeof (* skill));
skill = memorize (NULL, (int) sizeof (* skill));
skill = memorize ((int) sizeof (* skill));
//~string_copy ((skill->name = allocate (string_length (name) + 1)), name);
//~memory_copy ((skill->points = allocate ((int) sizeof (* skill->points))), points, (int) sizeof (* points));
string_copy ((skill->name = memorize (NULL, string_length (name) + 1)), name);
memory_copy ((skill->points = memorize (NULL, (int) sizeof (* skill->points))), points, (int) sizeof (* points));
string_copy ((skill->name = memorize (string_length (name) + 1)), name);
memory_copy ((skill->points = memorize ((int) sizeof (* skill->points))), points, (int) sizeof (* points));
for (;;) {
action = (number_t) va_arg (list, int);
if (action > 0) {
skill->positive_count += 1;
//~skill->positive = reallocate (skill->positive, skill->positive_count * (int) sizeof (action));
skill->positive = memorize (skill->positive, skill->positive_count * (int) sizeof (action));
skill->positive = memorize ((int) sizeof (action));
skill->positive [skill->positive_count - 1] = (action_t) action;
} else break;
}
@ -125,28 +95,23 @@ attribute_t * game_attribute (string_t name, bundle_t * points, ...) {
va_start (list, points);
//~attribute = allocate ((int) sizeof (* attribute));
attribute = memorize (NULL, (int) sizeof (* attribute));
attribute = memorize ((int) sizeof (* attribute));
//~string_copy ((attribute->name = allocate (string_length (name) + 1)), name);
//~memory_copy ((attribute->points = allocate ((int) sizeof (* attribute->points))), points, (int) sizeof (* points));
string_copy ((attribute->name = memorize (NULL, string_length (name) + 1)), name);
memory_copy ((attribute->points = memorize (NULL, (int) sizeof (* attribute->points))), points, (int) sizeof (* points));
string_copy ((attribute->name = memorize (string_length (name) + 1)), name);
memory_copy ((attribute->points = memorize ((int) sizeof (* attribute->points))), points, (int) sizeof (* points));
for (;;) {
action = (number_t) va_arg (list, int);
if (action > 0) {
attribute->positive_count += 1;
//~attribute->positive = reallocate (attribute->positive, attribute->positive_count * (int) sizeof (action));
attribute->positive = memorize (attribute->positive, attribute->positive_count * (int) sizeof (action));
attribute->positive = memorize ((int) sizeof (action));
attribute->positive [attribute->positive_count - 1] = (action_t) action;
} else if (action < 0) {
attribute->negative_count += 1;
//~attribute->negative = reallocate (attribute->negative, attribute->negative_count * (int) sizeof (action));
attribute->negative = memorize (attribute->negative, attribute->negative_count * (int) sizeof (action));
attribute->negative [attribute->negative_count - 1] = (action_t) -action;
attribute->negative = memorize ((int) sizeof (action));
attribute->negative [attribute->negative_count - 1] = (action_t) action;
} else {
break;
}
@ -228,21 +193,17 @@ void play_game (void) {
game_synchronize ();
}
//~strength->name = deallocate (strength->name); strength->points = deallocate (strength->points);strength->positive = deallocate (strength->positive); strength->negative = deallocate (strength->negative); strength = deallocate (strength);
//~edurance->name = deallocate (edurance->name); edurance->points = deallocate (edurance->points);edurance->positive = deallocate (edurance->positive); edurance->negative = deallocate (edurance->negative); edurance = deallocate (edurance);
//~wisdom->name = deallocate (wisdom->name); wisdom->points = deallocate (wisdom->points); wisdom->positive = deallocate (wisdom->positive); wisdom->negative = deallocate (wisdom->negative); wisdom = deallocate (wisdom);
//~agility->name = deallocate (agility->name); agility->points = deallocate (agility->points); agility->positive = deallocate (agility->positive); agility->negative = deallocate (agility->negative); agility = deallocate (agility);
//~blades->name = deallocate (blades->name); blades->points = deallocate (blades->points);blades->positive = deallocate (blades->positive); blades = deallocate (blades);
//~axes->name = deallocate (axes->name); axes->points = deallocate (axes->points);axes->positive = deallocate (axes->positive); axes = deallocate (axes);
//~bows->name = deallocate (bows->name); bows->points = deallocate (bows->points); bows->positive = deallocate (bows->positive); bows = deallocate (bows);
//~spears->name = deallocate (spears->name); spears->points = deallocate (spears->points); spears->positive = deallocate (spears->positive); spears = deallocate (spears);
//~puppet_magic->name = deallocate (puppet_magic->name); puppet_magic->points = deallocate (puppet_magic->points);puppet_magic->positive = deallocate (puppet_magic->positive); puppet_magic = deallocate (puppet_magic);
//~nature_magic->name = deallocate (nature_magic->name); nature_magic->points = deallocate (nature_magic->points);nature_magic->positive = deallocate (nature_magic->positive); nature_magic = deallocate (nature_magic);
//~rune_magic->name = deallocate (rune_magic->name); rune_magic->points = deallocate (rune_magic->points); rune_magic->positive = deallocate (rune_magic->positive); rune_magic = deallocate (rune_magic);
//~charm_magic->name = deallocate (charm_magic->name); charm_magic->points = deallocate (charm_magic->points); charm_magic->positive = deallocate (charm_magic->positive); charm_magic = deallocate (charm_magic);
memory_pool = deallocate (memory_pool);
//~char*a,*b,*c;
//~a=memorize(12); string_copy(a,"heyo world\n"); terminal_colour(1,1); echo(a); terminal_cancel();
//~b=memorize( 3); string_copy(b,"!\n"); terminal_colour(2,1); echo(b); terminal_cancel();
//~c=memorize(12); string_copy(c,"cyaa world\n"); terminal_colour(3,1); echo(c); terminal_cancel();
//~out(memory_store,512);
//~terminal_colour(1,1); echo(number_to_string((int)a)); terminal_cancel();
//~terminal_colour(2,1); echo(number_to_string((int)b)); terminal_cancel();
//~terminal_colour(3,1); echo(number_to_string((int)c)); terminal_cancel(); echo("\n");
//~terminal_colour(1,1); echo(a); terminal_cancel();
//~terminal_colour(2,1); echo(b); terminal_cancel();
//~terminal_colour(3,1); echo(c); terminal_cancel();
}
#endif

View File

@ -69,7 +69,6 @@ typedef struct player_t {
skill_t puppet_magic, nature_magic, rune_magic, charm_magic;
} player_t;
extern memory_t memory_pool;
extern game_t game;
extern player_t player;