瀏覽代碼

Redesigned entire memory management subsystem...

master
父節點
當前提交
ce38125428
共有 4 個文件被更改,包括 98 次插入47 次删除
  1. +88
    -40
      chapter/chapter_5.c
  2. +2
    -1
      chapter/chapter_5.h
  3. +6
    -4
      chapter/chapter_6.c
  4. +2
    -2
      compile.sh

+ 88
- 40
chapter/chapter_5.c 查看文件

@@ -17,15 +17,23 @@ So, what are actually getters and setters, and why you should never use them? Le

#define MEMORY_LIMIT (1024 * 1024)

memory_t memorize (number_t size) {
static string_t memory_store [MEMORY_LIMIT] = { 0 };
static number_t memory_count = 0;
static number_t game_memory_length = 0;
static memory_t * game_memory = NULL;

fatal_failure (memory_count + size >= MEMORY_LIMIT, "memorize: You have reached the 1 MiB memory limit.");
static procedure_t game_free_memory (memory_t memory) {
++game_memory_length;

memory_count += size;
game_memory = reallocate (game_memory, game_memory_length * (number_t) sizeof (* game_memory));

return ((memory_t) ((string_t) memory_store + memory_count - size));
game_memory [game_memory_length - 1] = memory;
}

procedure_t game_clean_up (procedure_t) {
for (number_t pointer = 0; pointer < game_memory_length; ++pointer) {
game_memory [pointer] = deallocate (game_memory [pointer]);
}

game_memory = deallocate (game_memory);
}

number_t randomize (number_t lower, number_t upper) {
@@ -33,41 +41,47 @@ number_t randomize (number_t lower, number_t upper) {
}

bundle_t * game_bundle (number_t minimum, number_t maximum, number_t current) {
bundle_t * bundle = memorize ((number_t) sizeof (* bundle));
bundle_t * bundle = allocate ((number_t) sizeof (* bundle));

bundle->minimum = minimum;
bundle->maximum = maximum;
bundle->current = current;

game_free_memory (bundle);

return (bundle);
}

symbol_t * game_symbol (number_t character, number_t colour, number_t effect) {
symbol_t * symbol = memorize ((number_t) sizeof (* symbol));
symbol_t * symbol = allocate ((number_t) sizeof (* symbol));

symbol->character = character;
symbol->colour = colour;
symbol->effect = effect;

game_free_memory (symbol);

return (symbol);
}

generator_t * game_generator (generate_t function) {
generator_t * generator = memorize ((number_t) sizeof (* generator));
generator_t * generator = allocate ((number_t) sizeof (* generator));

generator->generate = function;

game_free_memory (generator);

return (generator);
}

attribute_t * game_attribute (string_t name, bundle_t * points, ...) {
va_list list;

attribute_t * attribute = memorize ((number_t) sizeof (* attribute));
attribute_t * attribute = allocate ((number_t) sizeof (* attribute));

string_copy ((attribute->name = memorize (string_length (name) + 1)), name);
string_copy ((attribute->name = allocate (string_length (name) + 1)), name);

memory_copy ((attribute->points = memorize ((number_t) sizeof (* attribute->points))), points, (number_t) sizeof (* points));
memory_copy ((attribute->points = allocate ((number_t) sizeof (* attribute->points))), points, (number_t) sizeof (* points));

va_start (list, points);

@@ -76,58 +90,76 @@ attribute_t * game_attribute (string_t name, bundle_t * points, ...) {

if (action > 0) {
attribute->positive_count += 1;
attribute->positive = memorize ((number_t) sizeof (action));
attribute->positive = reallocate (attribute->positive, attribute->positive_count * (number_t) sizeof (action));
attribute->positive [attribute->positive_count - 1] = (action_t) action;
} else if (action < 0) {
attribute->negative_count += 1;
attribute->negative = memorize ((number_t) sizeof (action));
attribute->negative [attribute->negative_count - 1] = (action_t) action;
attribute->negative = reallocate (attribute->negative, attribute->negative_count * (number_t) sizeof (action));
attribute->negative [attribute->negative_count - 1] = (action_t) -action;
} else break;
}

va_end (list);

game_free_memory (attribute->name);
game_free_memory (attribute->points);
game_free_memory (attribute->positive);
game_free_memory (attribute->negative);
game_free_memory (attribute);

return (attribute);
}

skill_t * game_skill (string_t name, bundle_t * points, ...) {
va_list list;

skill_t * skill = memorize ((number_t) sizeof (* skill));
skill_t * skill = allocate ((number_t) sizeof (* skill));

string_copy ((skill->name = memorize (string_length (name) + 1)), name);
string_copy ((skill->name = allocate (string_length (name) + 1)), name);

memory_copy ((skill->points = memorize ((number_t) sizeof (* skill->points))), points, (number_t) sizeof (* points));
memory_copy ((skill->points = allocate ((number_t) sizeof (* skill->points))), points, (number_t) sizeof (* points));

va_start (list, points);

for (;;) {
number_t action = (number_t) va_arg (list, int);
action_t action = (action_t) va_arg (list, int);

if (action > 0) {
skill->positive_count += 1;
skill->positive = memorize ((number_t) sizeof (action));
skill->positive [skill->positive_count - 1] = (action_t) action;
skill->positive = reallocate (skill->positive, skill->positive_count * (number_t) sizeof (action));
skill->positive [skill->positive_count - 1] = action;
} else break;
}

va_end (list);

game_free_memory (skill->name);
game_free_memory (skill->points);
game_free_memory (skill->positive);
game_free_memory (skill);

return (skill);
}

bot_t * game_bot (string_t name, symbol_t * symbol, bundle_t * health, bundle_t * mana, bundle_t * stamina) {
bot_t * bot = memorize ((number_t) sizeof (* bot));
bot_t * bot = allocate ((number_t) sizeof (* bot));

string_copy ((bot->name = memorize (string_length (name) + 1)), name);
string_copy ((bot->name = allocate (string_length (name) + 1)), name);

bot->x = 0;
bot->y = 0;

memory_copy ((bot->symbol = memorize ((number_t) sizeof (* bot->symbol))), symbol, (number_t) sizeof (* symbol));
memory_copy ((bot->health = memorize ((number_t) sizeof (* bot->health))), health, (number_t) sizeof (* health));
memory_copy ((bot->mana = memorize ((number_t) sizeof (* bot->mana))), mana, (number_t) sizeof (* mana));
memory_copy ((bot->stamina = memorize ((number_t) sizeof (* bot->stamina))), stamina, (number_t) sizeof (* stamina));
memory_copy ((bot->symbol = allocate ((number_t) sizeof (* bot->symbol))), symbol, (number_t) sizeof (* symbol));
memory_copy ((bot->health = allocate ((number_t) sizeof (* bot->health))), health, (number_t) sizeof (* health));
memory_copy ((bot->mana = allocate ((number_t) sizeof (* bot->mana))), mana, (number_t) sizeof (* mana));
memory_copy ((bot->stamina = allocate ((number_t) sizeof (* bot->stamina))), stamina, (number_t) sizeof (* stamina));

game_free_memory (bot->name);
game_free_memory (bot->symbol);
game_free_memory (bot->health);
game_free_memory (bot->mana);
game_free_memory (bot->stamina);
game_free_memory (bot);

return (bot);
}
@@ -135,17 +167,17 @@ bot_t * game_bot (string_t name, symbol_t * symbol, bundle_t * health, bundle_t
player_t * game_player (string_t name, symbol_t * symbol, bundle_t * health, bundle_t * mana, bundle_t * stamina, ...) {
va_list list;

player_t * player = memorize ((number_t) sizeof (* player));
player_t * player = allocate ((number_t) sizeof (* player));

string_copy ((player->name = memorize (string_length (name) + 1)), name);
string_copy ((player->name = allocate (string_length (name) + 1)), name);

player->x = 3;
player->y = 3;

memory_copy ((player->symbol = memorize ((number_t) sizeof (* player->symbol))), symbol, (number_t) sizeof (* symbol));
memory_copy ((player->health = memorize ((number_t) sizeof (* player->health))), health, (number_t) sizeof (* health));
memory_copy ((player->mana = memorize ((number_t) sizeof (* player->mana))), mana, (number_t) sizeof (* mana));
memory_copy ((player->stamina = memorize ((number_t) sizeof (* player->stamina))), stamina, (number_t) sizeof (* stamina));
memory_copy ((player->symbol = allocate ((number_t) sizeof (* player->symbol))), symbol, (number_t) sizeof (* symbol));
memory_copy ((player->health = allocate ((number_t) sizeof (* player->health))), health, (number_t) sizeof (* health));
memory_copy ((player->mana = allocate ((number_t) sizeof (* player->mana))), mana, (number_t) sizeof (* mana));
memory_copy ((player->stamina = allocate ((number_t) sizeof (* player->stamina))), stamina, (number_t) sizeof (* stamina));

player->attribute_count = 0;
player->skill_count = 0;
@@ -157,7 +189,7 @@ player_t * game_player (string_t name, symbol_t * symbol, bundle_t * health, bun

if (attribute != NULL) {
player->attribute_count += 1;
player->attributes = memorize ((number_t) sizeof (attribute));
player->attributes = reallocate (player->attributes, player->attribute_count * (number_t) sizeof (* player->attributes));
player->attributes [player->attribute_count - 1] = attribute;
} else break;
}
@@ -167,38 +199,51 @@ player_t * game_player (string_t name, symbol_t * symbol, bundle_t * health, bun

if (skill != NULL) {
player->skill_count += 1;
player->skills = memorize ((number_t) sizeof (skill));
player->skills = reallocate (player->skills, player->skill_count * (number_t) sizeof (* player->skills));
player->skills [player->skill_count - 1] = skill;
} else break;
}

va_end (list);

game_free_memory (player->name);
game_free_memory (player->symbol);
game_free_memory (player->health);
game_free_memory (player->mana);
game_free_memory (player->stamina);
game_free_memory (player->attributes);
game_free_memory (player->skills);
game_free_memory (player);

return (player);
}

block_t * game_block (string_t name, symbol_t * symbol, number_t collision, number_t override) {
block_t * block = memorize ((number_t) sizeof (* block));
block_t * block = allocate ((number_t) sizeof (* block));

string_copy ((block->name = memorize (string_length (name) + 1)), name);
string_copy ((block->name = allocate (string_length (name) + 1)), name);

memory_copy ((block->symbol = memorize ((number_t) sizeof (* block->symbol))), symbol, (number_t) sizeof (* symbol));
memory_copy ((block->symbol = allocate ((number_t) sizeof (* block->symbol))), symbol, (number_t) sizeof (* symbol));

block->collision = collision;
block->override = override;

game_free_memory (block->name);
game_free_memory (block->symbol);
game_free_memory (block);

return (block);
}

world_t * game_world (number_t width, number_t height, ...) {
va_list list;

world_t * world = memorize ((number_t) sizeof (* world));
world_t * world = allocate ((number_t) sizeof (* world));

world->width = width;
world->height = height;

world->block = memorize (width * height * (number_t) sizeof (* world->block));
world->block = allocate (width * height * (number_t) sizeof (* world->block));

va_start (list, height);

@@ -218,6 +263,9 @@ world_t * game_world (number_t width, number_t height, ...) {

va_end (list);

game_free_memory (world->block);
game_free_memory (world);

return (world);
}



+ 2
- 1
chapter/chapter_5.h 查看文件

@@ -106,7 +106,8 @@ typedef struct generator_t {
generate_t generate;
} generator_t;

extern memory_t memorize (number_t size);
extern procedure_t game_clean_up (procedure_t);

extern number_t randomize (number_t lower, number_t upper);

extern bundle_t * game_bundle (number_t minimum, number_t maximum, number_t current);


+ 6
- 4
chapter/chapter_6.c 查看文件

@@ -88,10 +88,10 @@ procedure_t play_game (procedure_t) {
rectangle_line, 5, 9, 2, 4, stone_wall,
NULL);

//~player->attributes [0]->points->current = 7;
//~player->attributes [1]->points->current = 3;
//~player->attributes [2]->points->current = 5;
//~player->attributes [3]->points->current = 3;
player->attributes [0]->points->current = 7;
player->attributes [1]->points->current = 3;
player->attributes [2]->points->current = 5;
player->attributes [3]->points->current = 3;

curses_configure ();

@@ -124,6 +124,8 @@ procedure_t play_game (procedure_t) {
}

terminal_show_cursor (TRUE);

game_clean_up ();
}

#endif

+ 2
- 2
compile.sh 查看文件

@@ -32,8 +32,8 @@ gcc -o xhartae xhartae.o chapter/chapter_0.o chapter/chapter_1.o chapter/chapter
#~splint -weak -warnposix -retvalother -syntax -type chapter/chapter_6.c
#~splint -weak -warnposix -retvalother -syntax -type xhartae.c

valgrind --show-leak-kinds=all --leak-check=full --log-file=log.txt ./xhartae
#~valgrind --show-leak-kinds=all --leak-check=full --log-file=log.txt ./xhartae

xighlight -V < log.txt
#~xighlight -V < log.txt

exit

Loading…
取消
儲存