From 9ceaf50e09365ce501e679e4420bbc0c1cd2b4de Mon Sep 17 00:00:00 2001 From: milang Date: Tue, 15 Oct 2019 19:35:26 +0200 Subject: [PATCH] minor changes on dynamic line gestion --- liblog.c | 65 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/liblog.c b/liblog.c index c1f5deb..443bb6a 100644 --- a/liblog.c +++ b/liblog.c @@ -9,12 +9,14 @@ #ifdef FX9860G +static int number_max_messages=4096; #define MAX_LENGHT 22 #define WIDTH 8 #endif #ifdef FXCG50 +static int number_max_messages=10000; #define MAX_LENGHT 132 #define WIDTH 17 @@ -24,12 +26,7 @@ typedef struct log_line log_line; typedef struct log_line { void* previous; -#ifdef FXCG50 char text[MAX_LENGHT]; -#endif -#ifdef FX9860G - char text[MAX_LENGHT]; -#endif void* next; int cursor; } log_line; @@ -38,20 +35,53 @@ typedef struct log_line static log_line first_line= { 0, - "You are at the top !", + "", 0, 0 }; static log_line* current_line=&first_line; /// Change the following line to custom ram weight -static int number_max_messages=2048; // 1Ko max + // 1Ko max static int number_messages=0; static int state=1; +static log_line* cut_line() +{ + log_line* maillon = first_line.next; + log_line* maillon_suivant = maillon->next; + + first_line.next=maillon_suivant; + if (maillon_suivant) + maillon_suivant->previous=&first_line; + + return maillon; +} + +static void optimize() +{ + while (number_messages*sizeof(log_line)>=number_max_messages) + { + log_line* line = cut_line(); + free(line); + number_messages--; + } +} + + +void ll_set_size(int s) +{ + number_messages=s; + optimize(); +} + + +int ll_get_size(); + + void ll_set_state(int s) { state = s; @@ -62,16 +92,7 @@ int ll_get_state() return state; } -static log_line* cut_line() -{ - log_line* maillon = first_line.next; - log_line* maillon_suivant = maillon->next; - first_line.next=maillon_suivant; - maillon_suivant->previous=&first_line; - - return maillon; -} static void link_line(log_line* line) { @@ -93,21 +114,23 @@ static log_line* add_line() int malloc_fail=0; log_line* line; - + int test=(number_messages*sizeof(log_line)>=number_max_messages); if (!test) { - number_messages++; line = malloc(sizeof(log_line)); if (0==line) malloc_fail=1; + else + number_messages++; } - + if (test || malloc_fail) // fail du malloc ou dépassement de la limite de ram imposée lors du build line=cut_line(); link_line(line); clear_line(line); + optimize(); return current_line; } @@ -143,7 +166,7 @@ static void append_character(char c) if (c!='\0') { current_line->cursor ++; - + if (current_line->cursor == MAX_LENGHT-1) add_line(); @@ -226,7 +249,7 @@ void ll_pause() line=linet; } if (key==KEY_EXIT) - break; + break; } }