From eb8b3e4eb4404667af667f712b9c078347af00cc Mon Sep 17 00:00:00 2001 From: Jeremiah Orians Date: Thu, 26 Jul 2018 19:51:44 -0400 Subject: [PATCH] Reduced Memory usage by another 50% --- CHANGELOG.org | 1 + cc.c | 1 + cc.h | 2 ++ cc_reader.c | 1 - cc_strings.c | 54 +++++++++++++++++++++------------------ test/test.answers | 2 +- test/test100/proof.answer | 2 +- 7 files changed, 35 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.org b/CHANGELOG.org index 2fdf49c..b73e4ac 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -34,6 +34,7 @@ Brought back common recursion Reorged logic tree to reduce complexity Simplified expression Changed EOF detection logic to deal with unsigned bits +Reduced Memory usage down from 50MB to 2MB ** Fixed Correct bug in how \" is treated diff --git a/cc.c b/cc.c index d97c7f8..bd98bc2 100644 --- a/cc.c +++ b/cc.c @@ -104,6 +104,7 @@ int main(int argc, char** argv) global_token = reverse_list(global_token); initialize_types(); + reset_hold_string(); struct token_list* output_list = program(NULL); /* Output the program we have compiled */ diff --git a/cc.h b/cc.h index 8fbc1f4..c3c4e30 100644 --- a/cc.h +++ b/cc.h @@ -31,6 +31,7 @@ void file_print(char* s, FILE* f); int match(char* a, char* b); char* copy_string(char* target, char* source); +void reset_hold_string(); struct type { @@ -79,3 +80,4 @@ struct token_list* globals_list; /* Make our string collection more efficient */ char* hold_string; +int string_index; diff --git a/cc_reader.c b/cc_reader.c index 08a0706..557daf5 100644 --- a/cc_reader.c +++ b/cc_reader.c @@ -32,7 +32,6 @@ int clearWhiteSpace(int c) return c; } -int string_index; int consume_byte(int c) { hold_string[string_index] = c; diff --git a/cc_strings.c b/cc_strings.c index 775fb40..0c6c94a 100644 --- a/cc_strings.c +++ b/cc_strings.c @@ -127,34 +127,36 @@ int escape_lookup(char* c) char* collect_regular_string(char* string) { int j = 0; - int i = 0; - char* message = calloc(MAX_STRING, sizeof(char)); + string_index = 0; /* 34 == " */ - message[0] = 34; + hold_string[0] = 34; while(string[j] != 0) { if((string[j] == '\\') & (string[j + 1] == 'x')) { - message[i] = escape_lookup(string + j); + hold_string[string_index] = escape_lookup(string + j); j = j + 4; } else if(string[j] == '\\') { - message[i] = escape_lookup(string + j); + hold_string[string_index] = escape_lookup(string + j); j = j + 2; } else { - message[i] = string[j]; + hold_string[string_index] = string[j]; j = j + 1; } - i = i + 1; + string_index = string_index + 1; } - message[i] = 34; - message[i + 1] = LF; + char* message = calloc(string_index + 3, sizeof(char)); + copy_string(message, hold_string); + reset_hold_string(); + message[string_index] = 34; + message[string_index + 1] = LF; return message; } @@ -162,45 +164,47 @@ char* collect_regular_string(char* string) char* collect_weird_string(char* string) { int j = 1; - int k = 1; + string_index = 1; int temp; char* table = "0123456789ABCDEF"; - char* hold = calloc(MAX_STRING, sizeof(char)); /* 39 == ' */ - hold[0] = 39; + hold_string[0] = 39; while(string[j] != 0) { - hold[k] = ' '; + hold_string[string_index] = ' '; if((string[j] == '\\') & (string[j + 1] == 'x')) { - hold[k + 1] = upcase(string[j + 2]); - hold[k + 2] = upcase(string[j + 3]); + hold_string[string_index + 1] = upcase(string[j + 2]); + hold_string[string_index + 2] = upcase(string[j + 3]); j = j + 4; } else if(string[j] == '\\') { temp = escape_lookup(string + j); - hold[k + 1] = table[(temp >> 4)]; - hold[k + 2] = table[(temp & 15)]; + hold_string[string_index + 1] = table[(temp >> 4)]; + hold_string[string_index + 2] = table[(temp & 15)]; j = j + 2; } else { - hold[k + 1] = table[(string[j] >> 4)]; - hold[k + 2] = table[(string[j] & 15)]; + hold_string[string_index + 1] = table[(string[j] >> 4)]; + hold_string[string_index + 2] = table[(string[j] & 15)]; j = j + 1; } - k = k + 3; + string_index = string_index + 3; } - hold[k] = ' '; - hold[k + 1] = '0'; - hold[k + 2] = '0'; - hold[k + 3] = 39; - hold[k + 4] = LF; + char* hold = calloc(string_index + 6, sizeof(char)); + copy_string(hold, hold_string); + reset_hold_string(); + hold[string_index] = ' '; + hold[string_index + 1] = '0'; + hold[string_index + 2] = '0'; + hold[string_index + 3] = 39; + hold[string_index + 4] = LF; return hold; } diff --git a/test/test.answers b/test/test.answers index 39f7eb5..9b2c38b 100644 --- a/test/test.answers +++ b/test/test.answers @@ -9,7 +9,7 @@ b45fae655b7f848b28ebdb8eb2e30ae789fbcf7920bc315395d53986bb1adae4 test/results/t d511db73158a9544a5b5f828a79751e3de8a04b81c143fd0c146fc22c938aa9f test/results/test08-binary 6831ba0c4e01cea5fb524d811e75542875512fb417baa03d2515278d5b0ee6a5 test/results/test09-binary ef179cd359ba1d61d45089e314cd4ac2069c8dc4dd7494d7c766344ea3c8cf88 test/results/test10-binary -c512c1c2bbe3e6fa5e727f41be286f95a0906d8d9c05165f70c0046918150c2b test/results/test100-binary +c526b47458dd06a47fee8d83533d71f48c51867b1c358e43f69da970f7d06338 test/results/test100-binary 5aaf399fe706d4a8c85c121c75ada29a65c293b57c98e8999961a2ef0bab0d62 test/results/test11-binary 4f8111e73e07255ae203963438c82ea8bcff7474e1594b52b426c58a03cb30eb test/results/test12-binary dd74dabfdce8657ff440c1eef531cbf67a64854f2020d4d6bcb65c9cc2d199cb test/results/test13-binary diff --git a/test/test100/proof.answer b/test/test100/proof.answer index 0e5b472..b32d9fb 100644 --- a/test/test100/proof.answer +++ b/test/test100/proof.answer @@ -1 +1 @@ -9fb571e4b35e0a169a2a4730a714f17b1695d93775c26763337b804607fe5193 test/test100/proof +5f65ea0157c4526f8d6f5887a045c6fcbb809787feb45ee3317699de3e480012 test/test100/proof