fixed ram management bug

This commit is contained in:
Milang 2020-02-03 11:12:54 +01:00
parent 54ab77daed
commit 7ff674bc39
8 changed files with 59 additions and 46 deletions

Binary file not shown.

View File

@ -9,4 +9,8 @@ int min(const int x, const int y);
int sgn(const int x);
void* malloc_prof(int size);
void free_prof(void * p);
#endif

View File

@ -1,4 +1,6 @@
#include <base.h>
#include <gint/std/stdlib.h>
#include <liblog.h>
int max(const int x, const int y)
{
@ -18,4 +20,31 @@ int sgn(const int x)
return -1;
else
return 0;
}
static int ram_used=0;
void* malloc_prof(int size)
{
void* p=malloc(size);
if (p)
{
ll_sendp(LEVEL_INFO, "\n[std] malloc %d OK", size);
ram_used++;
ll_sendp(LEVEL_INFO, "\n[std] number of zones %d", ram_used);
return p;
}
if (p)
{
ll_sendp(LEVEL_CRITICAL, "\n[std] malloc %d FAILED", size);
return p;
}
}
void free_prof(void * p)
{
free(p);
ll_sendp(LEVEL_INFO, "\n[std] free called");
ram_used--;
ll_sendp(LEVEL_INFO, "\n[std] number of zones %d", ram_used);
}

View File

@ -377,18 +377,18 @@ void display_ennemi_table()
void init_ennemi(ennemi_t * table, int s)
{
ennemis_global_size=s;
ennemis_global_size=0;
if (ennemis_global)
{
free(ennemis_global);
free_prof(ennemis_global);
ennemis_global=0;
}
if (0==ennemis_global_size)
if (0==s)
return;
int size=sizeof(ennemi_t)*ennemis_global_size;
ennemis_global=malloc(size);
int size=sizeof(ennemi_t)*s;
ennemis_global=malloc_prof(size);
if (ennemis_global==0)
malloc_error();
ennemis_global_size=s;
memcpy(ennemis_global, table, size);
}

View File

@ -46,12 +46,10 @@ static uint8_t pack_access(packed_level_t const * const p, unsigned int x, unsig
{
if (x<p->width && y<p->height)
{
ll_sendp(LEVEL_INFO, "\n[i]pack access at %d,%d",x,y);
return p->data[x*p->height+y];
}
else
{
ll_sendp(LEVEL_WARNING, "\n[w]pack access out of range at %d,%d",x,y);
return PACKED_EMPTY;
}
}
@ -70,28 +68,27 @@ static void unpack_level(packed_level_t * p)
ll_sendp(LEVEL_INFO, "\nUnpacking level %dx%d", w,h);
cell_t * c = (cell_t *) malloc(sizeof(cell_t) * w * h);
cell_t * c = (cell_t *) malloc_prof(sizeof(cell_t) * w * h);
ll_sendp(LEVEL_INFO, "\n[i]malloc %d", sizeof(cell_t) * w * h);
if (c==0)
{ // overriding parameters
w = 232;//p->width;
h = 13;//p->height;
ll_sendp(LEVEL_CRITICAL, "\n[e]malloc failed !\n[i]Trying defaults:\n %dx%d", w,h);
ll_sendp(LEVEL_CRITICAL, "\n[e]malloc %d failed !\n[i]Trying defaults:\n %dx%d",sizeof(cell_t) * w * h, w,h);
ll_sendp(LEVEL_WARNING, "\n[w]writing into pack new coordinates:\n %dx%d", w,h);
p->width=w;
p->height=h;
cell_t * c = (cell_t *) malloc(sizeof(cell_t) * w * h);
cell_t * c = (cell_t *) malloc_prof(sizeof(cell_t) * w * h);
if (c==0)
malloc_error();
}
ll_set_level(LEVEL_WARNING);
//ll_set_level(LEVEL_WARNING);
int sx=0, sy=p->height; // Mario start coords
ll_sendp(LEVEL_INFO, "\n[i]Converting...", w,h);
ennemi_t ennemis[20]={0};
int nombre_ennemis=0;
@ -283,9 +280,8 @@ static void unpack_level(packed_level_t * p)
cell = t;
}
ll_sendp(LEVEL_INFO, "\n[i]Converted cell %d,%d\n {%d,%p}\n[i]Writing cell...", x,y, cell.type, cell.data);
cell_set(c,w,h,x,y,cell);
ll_sendp(LEVEL_INFO, "\n[i]Wrote cell", x,y, cell.type, cell.data);
@ -318,7 +314,7 @@ static void unpack_level(packed_level_t * p)
ll_sendp(LEVEL_INFO, "\n[i]Converted!\n[i]Sending to level zone...", w,h);
world_set(w, h, sx, sy, c);
ll_sendp(LEVEL_INFO, "\n[i]Achieved unpacking", w,h);
free(c);
free_prof(c);
init_ennemi(ennemis, nombre_ennemis);
@ -328,13 +324,9 @@ void set_level(int w, int l)
{
new_level();
set_teleporteurs(0,0);
if (map_current)
{
free(map_current);
map_current=0;
}
ennemis_global_size=0;
plateforme_table_size=0;
world_set(0,0,0,0,0);
init_ennemi(0, 0);
init_plateformes(0, 0);
if (w+1==1 && l+1==1) // 1-2
{

View File

@ -28,6 +28,7 @@ int main(void)
sleep_ms(3,1200);
//gint_panic_set(system_error);
ll_set_panic();
//ll_set_level(LEVEL_QUIET);
launch_ui();

View File

@ -8,22 +8,11 @@
#include <level.h>
#include <gint/std/string.h>
#include <gint/std/stdlib.h>
#include <base.h>
int plateforme_table_size=0;
plateforme_t* plateformes=0;
static int sgn(int x)
{
if (x==0)
return 0;
else if (x>0)
return 1;
return -1;
}
void reset_plateforme(plateforme_t* p)
{
p->x=p->xinit;
@ -136,18 +125,18 @@ int plateforme_check_collide(int x, int y)
void init_plateformes(plateforme_t * table, int s)
{
plateforme_table_size=s;
plateforme_table_size=0;
if (plateformes)
{
free(plateformes);
free_prof(plateformes);
plateformes=0;
}
if (0==table)
return;
int size=sizeof(plateforme_t)*plateforme_table_size;
plateformes=malloc(size);
plateformes=malloc_prof(size);
if (plateformes==0)
malloc_error();
plateforme_table_size=s;
memcpy(plateformes, table, size);
}

View File

@ -204,19 +204,14 @@ void world_draw()
void world_move()
{
mkb_update();
ll_sendp(LEVEL_INFO,"\n[I;%d] Refresh wrld",frame_id);
for (int i=0; i<ennemis_global_size; i++)
{
move_ennemi(&ennemis_global[i]);
}
ll_sendp(LEVEL_INFO,"\n > Moved ennemies");
bonus_move();
ll_sendp(LEVEL_INFO,"\n > Moved bonus");
bullet_move();
ll_sendp(LEVEL_INFO,"\n > Moved bullets");
move_plateformes();
mario_move();
ll_sendp(LEVEL_INFO,"\n > Moved mario !\n");
teleport_active();
}
@ -228,7 +223,10 @@ void world_set(int w, int h, int x, int y, cell_t const * a)
// Free the previous map
if (map_current)
free(map_current);
{
free_prof(map_current);
map_current=0;
}
// If the new map size is null => invalid map, return
if (0==w*h)
@ -236,7 +234,7 @@ void world_set(int w, int h, int x, int y, cell_t const * a)
// Calculates the new struct size
int size= 4*sizeof(int) + sizeof(cell_t)*w*h;
map_current=(map_t*)malloc(size);
map_current=(map_t*)malloc_prof(size);
if (map_current==0)
malloc_error();