diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-05-19 12:42:20 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-05-19 12:42:20 -0300 |
commit | 9514abc2da3525ef4314a8fcf70982ad07319e51 (patch) | |
tree | f3d30b28767cf1f3aedff7521ed1b83c9443c7cf | |
parent | 0be57b9b6d1a4ea8d41c9c9b9b434b4749ccbb27 (diff) | |
download | lua-9514abc2da3525ef4314a8fcf70982ad07319e51.tar.gz lua-9514abc2da3525ef4314a8fcf70982ad07319e51.tar.bz2 lua-9514abc2da3525ef4314a8fcf70982ad07319e51.zip |
Cleaner definition for 'TString'
Use a variable-sized array to store string contents at the end of a
structure 'TString', instead of raw memory.
-rw-r--r-- | lobject.h | 7 | ||||
-rw-r--r-- | lstring.h | 6 |
2 files changed, 8 insertions, 5 deletions
@@ -356,7 +356,7 @@ typedef struct GCObject { | |||
356 | 356 | ||
357 | 357 | ||
358 | /* | 358 | /* |
359 | ** Header for string value; string bytes follow the end of this structure. | 359 | ** Header for a string value. |
360 | */ | 360 | */ |
361 | typedef struct TString { | 361 | typedef struct TString { |
362 | CommonHeader; | 362 | CommonHeader; |
@@ -367,16 +367,15 @@ typedef struct TString { | |||
367 | size_t lnglen; /* length for long strings */ | 367 | size_t lnglen; /* length for long strings */ |
368 | struct TString *hnext; /* linked list for hash table */ | 368 | struct TString *hnext; /* linked list for hash table */ |
369 | } u; | 369 | } u; |
370 | char contents[1]; | ||
370 | } TString; | 371 | } TString; |
371 | 372 | ||
372 | 373 | ||
373 | 374 | ||
374 | /* | 375 | /* |
375 | ** Get the actual string (array of bytes) from a 'TString'. | 376 | ** Get the actual string (array of bytes) from a 'TString'. |
376 | ** (Access to 'extra' ensures that value is really a 'TString'.) | ||
377 | */ | 377 | */ |
378 | #define getstr(ts) \ | 378 | #define getstr(ts) ((ts)->contents) |
379 | check_exp(sizeof((ts)->extra), cast_charp((ts)) + sizeof(TString)) | ||
380 | 379 | ||
381 | 380 | ||
382 | /* get the actual string (array of bytes) from a Lua value */ | 381 | /* get the actual string (array of bytes) from a Lua value */ |
@@ -19,7 +19,11 @@ | |||
19 | #define MEMERRMSG "not enough memory" | 19 | #define MEMERRMSG "not enough memory" |
20 | 20 | ||
21 | 21 | ||
22 | #define sizelstring(l) (sizeof(TString) + ((l) + 1) * sizeof(char)) | 22 | /* |
23 | ** Size of a TString: Size of the header plus space for the string | ||
24 | ** itself (including final '\0'). | ||
25 | */ | ||
26 | #define sizelstring(l) (offsetof(TString, contents) + ((l) + 1) * sizeof(char)) | ||
23 | 27 | ||
24 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ | 28 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ |
25 | (sizeof(s)/sizeof(char))-1)) | 29 | (sizeof(s)/sizeof(char))-1)) |