summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-05-19 12:42:20 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-05-19 12:42:20 -0300
commit9514abc2da3525ef4314a8fcf70982ad07319e51 (patch)
treef3d30b28767cf1f3aedff7521ed1b83c9443c7cf
parent0be57b9b6d1a4ea8d41c9c9b9b434b4749ccbb27 (diff)
downloadlua-github-9514abc2da3525ef4314a8fcf70982ad07319e51.tar.gz
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.h7
-rw-r--r--lstring.h6
2 files changed, 8 insertions, 5 deletions
diff --git a/lobject.h b/lobject.h
index 2d63c001..04a81d3d 100644
--- a/lobject.h
+++ b/lobject.h
@@ -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 {
CommonHeader;
@@ -367,16 +367,15 @@ typedef struct TString {
size_t lnglen; /* length for long strings */
struct TString *hnext; /* linked list for hash table */
} u;
+ char contents[1];
} TString;
/*
** Get the actual string (array of bytes) from a 'TString'.
-** (Access to 'extra' ensures that value is really a 'TString'.)
*/
-#define getstr(ts) \
- check_exp(sizeof((ts)->extra), cast_charp((ts)) + sizeof(TString))
+#define getstr(ts) ((ts)->contents)
/* get the actual string (array of bytes) from a Lua value */
diff --git a/lstring.h b/lstring.h
index 56896867..a413a9d3 100644
--- a/lstring.h
+++ b/lstring.h
@@ -19,7 +19,11 @@
#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, \
(sizeof(s)/sizeof(char))-1))