From 7de65b2a9d7b22913c5539db1ab8382b36acd573 Mon Sep 17 00:00:00 2001 From: xolatile Date: Tue, 21 Nov 2023 11:58:48 -0500 Subject: [PATCH] Implemented memory pool... --- chapter/chapter_5.c | 127 +++++++++++++++++++++++++++++++++++++++------------- chapter/chapter_5.h | 1 + 2 files changed, 97 insertions(+), 31 deletions(-) diff --git a/chapter/chapter_5.c b/chapter/chapter_5.c index f9a1723..a9ec835 100644 --- a/chapter/chapter_5.c +++ b/chapter/chapter_5.c @@ -24,6 +24,45 @@ static number_t game_set_screen_height (game_t * game, number_t height) { return game_t game; player_t player; +memory_t memory_pool; + +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; + + /* Show an example of incremental reallocation with for loop from libhl early prototype. */ + + fatal_failure (size >= memory_block_size, "memorize: You're trying to re/allocate huge chunk of memory."); + + if (memory_used + size >= memory_block_used * memory_block_size) { + memory_pool = reallocate (memory_pool, ++memory_block_used * memory_block_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); +} static void move_player (void) { ++player.x; } @@ -54,17 +93,22 @@ skill_t * game_skill (string_t name, bundle_t * points, ...) { va_start (list, points); - skill = allocate ((int) sizeof (* skill)); + //~skill = allocate ((int) sizeof (* skill)); + skill = memorize (NULL, (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 = 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)); 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 = reallocate (skill->positive, skill->positive_count * (int) sizeof (action)); + skill->positive = memorize (skill->positive, skill->positive_count * (int) sizeof (action)); skill->positive [skill->positive_count - 1] = (action_t) action; } else break; } @@ -81,21 +125,27 @@ attribute_t * game_attribute (string_t name, bundle_t * points, ...) { va_start (list, points); - attribute = allocate ((int) sizeof (* attribute)); + //~attribute = allocate ((int) sizeof (* attribute)); + attribute = memorize (NULL, (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 = 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)); 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 = reallocate (attribute->positive, attribute->positive_count * (int) sizeof (action)); + attribute->positive = memorize (attribute->positive, attribute->positive_count * (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 = 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; } else { break; @@ -108,9 +158,9 @@ attribute_t * game_attribute (string_t name, bundle_t * points, ...) { } void game_render_skill (skill_t * skill, number_t x, number_t y) { - curses_render_string (skill->name, COLOUR_BLUE, EFFECT_NORMAL, x, y); + curses_render_string (skill->name, COLOUR_CYAN, EFFECT_NORMAL, x, y); - curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 20, y); + curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 18, y); curses_render_string (format_to_string (skill->points->minimum, 0, 10, 4, ' '), COLOUR_RED, EFFECT_NORMAL, x = 22, y); curses_render_string (format_to_string (skill->points->current, 0, 10, 4, ' '), COLOUR_WHITE, EFFECT_NORMAL, x = 26, y); curses_render_string (format_to_string (skill->points->maximum, 0, 10, 4, ' '), COLOUR_GREEN, EFFECT_NORMAL, x = 30, y); @@ -118,9 +168,9 @@ void game_render_skill (skill_t * skill, number_t x, number_t y) { } void game_render_attribute (attribute_t * attribute, number_t x, number_t y) { - curses_render_string (attribute->name, COLOUR_BLUE, EFFECT_NORMAL, x, y); + curses_render_string (attribute->name, COLOUR_CYAN, EFFECT_NORMAL, x, y); - curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 20, y); + curses_render_string ("[ ", COLOUR_GREY, EFFECT_BOLD, x + 18, y); curses_render_string (format_to_string (attribute->points->minimum, 0, 10, 4, ' '), COLOUR_RED, EFFECT_NORMAL, x = 22, y); curses_render_string (format_to_string (attribute->points->current, 0, 10, 4, ' '), COLOUR_WHITE, EFFECT_NORMAL, x = 26, y); curses_render_string (format_to_string (attribute->points->maximum, 0, 10, 4, ' '), COLOUR_GREEN, EFFECT_NORMAL, x = 30, y); @@ -135,8 +185,8 @@ void play_game (void) { attribute_t * strength, * edurance, * wisdom, * agility; strength = game_attribute ("Strength", & attribute_points, GAME_ACTION_SWING_BLADE, GAME_ACTION_SWING_AXE, -GAME_ACTION_CAMP, 0); - edurance = game_attribute ("Edurance", & attribute_points, GAME_ACTION_SWING_BLADE, GAME_ACTION_SWING_AXE, -GAME_ACTION_REST, 0); - wisdom = game_attribute ("Wisdom", & attribute_points, GAME_ACTION_SWING_BLADE, GAME_ACTION_SWING_AXE, -GAME_ACTION_WALK, 0); + edurance = game_attribute ("Edurance", & attribute_points, GAME_ACTION_WALK, GAME_ACTION_CAMP, -GAME_ACTION_REST, 0); + wisdom = game_attribute ("Wisdom", & attribute_points, GAME_ACTION_CITE_RUNE, GAME_ACTION_CAST_CHARM, -GAME_ACTION_WALK, 0); agility = game_attribute ("Agility", & attribute_points, GAME_ACTION_SHOOT_ARROW, GAME_ACTION_THROW_SPEAR, -GAME_ACTION_WAIT, 0); blades = game_skill ("Blades", & skill_points, GAME_ACTION_SWING_BLADE, 0); @@ -153,10 +203,23 @@ void play_game (void) { while (curses_active) { curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL); - game_render_attribute (strength, 0, 0); - game_render_attribute (edurance, 0, 1); - game_render_attribute (wisdom, 0, 2); - game_render_attribute (agility, 0, 3); + curses_render_string ("Attributes:", COLOUR_WHITE, EFFECT_BOLD, 0, 0); + + game_render_attribute (strength, 2, 1); + game_render_attribute (edurance, 2, 2); + game_render_attribute (wisdom, 2, 3); + game_render_attribute (agility, 2, 4); + + curses_render_string ("Skills:", COLOUR_WHITE, EFFECT_BOLD, 0, 5); + + game_render_skill (blades, 2, 6); + game_render_skill (axes, 2, 7); + game_render_skill (bows, 2, 8); + game_render_skill (spears, 2, 9); + game_render_skill (puppet_magic, 2, 10); + game_render_skill (nature_magic, 2, 11); + game_render_skill (rune_magic, 2, 12); + game_render_skill (charm_magic, 2, 13); curses_render_character ('@', COLOUR_CYAN, EFFECT_BOLD, player.x, player.y); @@ -165,19 +228,21 @@ 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); + //~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); + //~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); } #endif diff --git a/chapter/chapter_5.h b/chapter/chapter_5.h index baa3888..5088af8 100644 --- a/chapter/chapter_5.h +++ b/chapter/chapter_5.h @@ -69,6 +69,7 @@ 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;