Cleaner definition for 'TString'

Use a variable-sized array to store string contents at the end of a
structure 'TString', instead of raw memory.
This commit is contained in:
Roberto Ierusalimschy 2020-05-19 12:42:20 -03:00
parent 0be57b9b6d
commit 9514abc2da
2 changed files with 8 additions and 5 deletions

View File

@ -356,7 +356,7 @@ typedef struct GCObject {
/* /*
** Header for string value; string bytes follow the end of this structure. ** Header for a string value.
*/ */
typedef struct TString { typedef struct TString {
CommonHeader; CommonHeader;
@ -367,16 +367,15 @@ typedef struct TString {
size_t lnglen; /* length for long strings */ size_t lnglen; /* length for long strings */
struct TString *hnext; /* linked list for hash table */ struct TString *hnext; /* linked list for hash table */
} u; } u;
char contents[1];
} TString; } TString;
/* /*
** Get the actual string (array of bytes) from a 'TString'. ** Get the actual string (array of bytes) from a 'TString'.
** (Access to 'extra' ensures that value is really a 'TString'.)
*/ */
#define getstr(ts) \ #define getstr(ts) ((ts)->contents)
check_exp(sizeof((ts)->extra), cast_charp((ts)) + sizeof(TString))
/* get the actual string (array of bytes) from a Lua value */ /* get the actual string (array of bytes) from a Lua value */

View File

@ -19,7 +19,11 @@
#define MEMERRMSG "not enough memory" #define MEMERRMSG "not enough memory"
#define sizelstring(l) (sizeof(TString) + ((l) + 1) * sizeof(char)) /*
** Size of a TString: Size of the header plus space for the string
** itself (including final '\0').
*/
#define sizelstring(l) (offsetof(TString, contents) + ((l) + 1) * sizeof(char))
#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \
(sizeof(s)/sizeof(char))-1)) (sizeof(s)/sizeof(char))-1))