History

Migrate static STRING_MAX_SIZE buffers to CString instances for large macros expansion

Vlad Vissoultchev committed 10 years ago

Commit 32755db1 parent d715ebd1 file · +23 −14Browse files

Unified · Split

tccpp.c+23−14
@@ -277,16 +277,16 @@ ST_FUNC TokenSym *tok_alloc(const char *str, int len) · 276 unmodified lines
277277 /* XXX: float tokens */
278278 ST_FUNC const char *get_tok_str(int v, CValue *cv)
279279 {
280− static char buf[STRING_MAX_SIZE + 1];
281280 static CString cstr_buf;
282281 char *p;
283282 int i, len;
284283
285− /* NOTE: to go faster, we give a fixed buffer for small strings */
286− cstr_reset(&cstr_buf);
287− cstr_buf.data = buf;
288− cstr_buf.size_allocated = sizeof(buf);
289− p = buf;
284+ /* first time preallocate static cstr_buf, next time only reset position to start */
285+ if (!cstr_buf.data_allocated)
286+ cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
287+ else
288+ cstr_buf.size = 0;
289+ p = cstr_buf.data;
290290
291291 switch(v) {
292292 case TOK_CINT:
@@ -333,17 +333,20 @@ ST_FUNC const char *get_tok_str(int v, CValue *cv) · 40 unmodified lines
333333
334334 case TOK_CFLOAT:
335335 cstr_cat(&cstr_buf, "<float>");
336+ cstr_ccat(&cstr_buf, '\0');
336337 break;
337338 case TOK_CDOUBLE:
338339 cstr_cat(&cstr_buf, "<double>");
340+ cstr_ccat(&cstr_buf, '\0');
339341 break;
340342 case TOK_CLDOUBLE:
341343 cstr_cat(&cstr_buf, "<long double>");
344+ cstr_ccat(&cstr_buf, '\0');
342345 break;
343346 case TOK_LINENUM:
344347 cstr_cat(&cstr_buf, "<linenumber>");
348+ cstr_ccat(&cstr_buf, '\0');
345349 break;
346− //return NULL; /* should not happen */
347350
348351 /* above tokens have value, the ones below don't */
349352
@@ -368,13 +371,13 @@ ST_FUNC const char *get_tok_str(int v, CValue *cv) · 18 unmodified lines
368371 *p++ = q[0];
369372 *p++ = q[1];
370373 *p = '\0';
371− return buf;
374+ return cstr_buf.data;
372375 }
373376 q += 3;
374377 }
375378 if (v >= 127) {
376− sprintf(buf, "<%02x>", v);
377− return buf;
379+ sprintf(cstr_buf.data, "<%02x>", v);
380+ return cstr_buf.data;
378381 }
379382 addv:
380383 *p++ = v;
@@ -1048,14 +1051,20 @@ static int tok_last(const int *str0, const int *str1) · 667 unmodified lines
10481051
10491052 static int macro_is_equal(const int *a, const int *b)
10501053 {
1051− char buf[STRING_MAX_SIZE + 1];
1054+ static CString cstr_buf;
10521055 CValue cv;
10531056 int t;
10541057 while (*a && *b) {
1058+ /* first time preallocate static cstr_buf, next time only reset position to start */
1059+ if (!cstr_buf.data_allocated)
1060+ cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
1061+ else
1062+ cstr_buf.size = 0;
10551063 TOK_GET(&t, &a, &cv);
1056− pstrcpy(buf, sizeof buf, get_tok_str(t, &cv));
1064+ cstr_cat(&cstr_buf, get_tok_str(t, &cv));
1065+ cstr_ccat(&cstr_buf, '\0');
10571066 TOK_GET(&t, &b, &cv);
1058− if (strcmp(buf, get_tok_str(t, &cv)))
1067+ if (strcmp(cstr_buf.data, get_tok_str(t, &cv)))
10591068 return 0;
10601069 }
10611070 return !(*a || *b);
@@ -2785,7 +2794,7 @@ static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args) · 1723 unmodified lines
27852794 && 0 == check_space(t, &spc)) {
27862795 const char *s = get_tok_str(t, &cval);
27872796 while (*s) {
2788− if (t == TOK_PPSTR && *s != '\'')
2797+ if (/*t == TOK_PPSTR &&*/ *s != '\'')
27892798 add_char(&cstr, *s);
27902799 else
27912800 cstr_ccat(&cstr, *s);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t