From: Homer Hsing Date: Fri, 14 Sep 2012 07:27:19 +0000 (+0800) Subject: Reduce memory cost in entry_table X-Git-Tag: intel-gpu-tools-1.4~606 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c19f8338d726fd9d4fcec7596118228f515f8dc1;p=profile%2Fextras%2Fintel-gpu-tools.git Reduce memory cost in entry_table Original code double entry table space if there is no space. It may waste 50% memory of the entry table. Now we use a link list to store entry items. --- diff --git a/assembler/src/main.c b/assembler/src/main.c index 1da9fe7..49f201c 100644 --- a/assembler/src/main.c +++ b/assembler/src/main.c @@ -116,47 +116,27 @@ static void free_register_table(void) } } -char **entry_point_table = NULL; -int entry_point_table_length = 0; +struct entry_point_item { + char *str; + struct entry_point_item *next; +} *entry_point_table; -#define DEFAULTBUFSIZE 800 static int read_entry_file(char *fn) { FILE *entry_table_file; - char buf[DEFAULTBUFSIZE]; - char *ptr; - int curr_table_length = 80; - if (!fn) { + char buf[2048]; + struct entry_point_item **p = &entry_point_table; + if (!fn) return 0; - } - if ((entry_point_table = - malloc(curr_table_length*sizeof(char*))) == NULL) { - return 1; - } - - entry_table_file = fopen(fn, "r"); - if (!entry_table_file) { - return 1; - } - - while (1) { - int size = DEFAULTBUFSIZE; - if((ptr = fgets(buf, size, entry_table_file))==NULL) - { - break; - } - buf[strlen(buf)-1] = '\0'; - if(entry_point_table_length == curr_table_length) - { - if ((entry_point_table = realloc(entry_point_table, - curr_table_length*2*sizeof(char*))) == NULL) { - return 1; - } - curr_table_length *= 2; - } - entry_point_table[entry_point_table_length] = strdup(buf); - entry_point_table_length ++; - + if ((entry_table_file = fopen(fn, "r")) == NULL) + return -1; + while (fgets(buf, sizeof(buf)-1, entry_table_file) != NULL) { + // drop the final char '\n' + if(buf[strlen(buf)-1] == '\n') + buf[strlen(buf)-1] = 0; + *p = calloc(1, sizeof(struct entry_point_item)); + (*p)->str = strdup(buf); + p = &((*p)->next); } fclose(entry_table_file); return 0; @@ -164,15 +144,22 @@ static int read_entry_file(char *fn) static int is_entry_point(char *s) { - int i; - for (i = 0; i < entry_point_table_length; i++) { - if (strcmp(entry_point_table[i], s) == 0) { + struct entry_point_item *p; + for (p = entry_point_table; p; p = p->next) { + if (strcmp(p->str, s) == 0) return 1; - } } return 0; } +static void free_entry_point_table(struct entry_point_item *p) { + if (p) { + free_entry_point_table(p->next); + free(p->str); + free(p); + } +} + static void print_instruction(FILE *output, struct brw_program_instruction *entry) { @@ -379,6 +366,7 @@ int main(int argc, char **argv) if (binary_like_output) fprintf(output, "};"); + free_entry_point_table(entry_point_table); free_register_table(); fflush (output); if (ferror (output)) {