diff options
author | Homer Hsing <homer.xing@intel.com> | 2012-09-14 15:27:19 +0800 |
---|---|---|
committer | Xiang, Haihao <haihao.xiang@intel.com> | 2012-09-26 23:26:59 -0400 |
commit | 1ef4bcb42107bb7e6227a9620e2359ada8c91baf (patch) | |
tree | 5c192fe51ac9a2bbba1f7958a9c44d1b0b582999 | |
parent | 9d654318d7f4cbee3ca59c2eeb66bfcf9e57d02a (diff) |
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.
-rw-r--r-- | src/main.c | 68 |
1 files changed, 28 insertions, 40 deletions
@@ -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)) { |