aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto I <roberto@inf.puc-rio.br>2025-10-17 13:53:35 -0300
committerRoberto I <roberto@inf.puc-rio.br>2025-10-17 13:53:35 -0300
commitb352217b8498a5ed8f6c954b3da365fcbb89751f (patch)
tree4a2a7feb7bcf9d0144af6923807904b9bc0e4037
parent9c66903cc55388006a833f0f3911ea81fa86edea (diff)
downloadlua-b352217b8498a5ed8f6c954b3da365fcbb89751f.tar.gz
lua-b352217b8498a5ed8f6c954b3da365fcbb89751f.tar.bz2
lua-b352217b8498a5ed8f6c954b3da365fcbb89751f.zip
Standard allocator function added to the API
That makes easier to redefine luaL_newstate.
-rw-r--r--lauxlib.c12
-rw-r--r--lauxlib.h3
-rw-r--r--manual/manual.of28
3 files changed, 28 insertions, 15 deletions
diff --git a/lauxlib.c b/lauxlib.c
index adb3851e..1bb41bb1 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -742,7 +742,7 @@ typedef struct LoadF {
742 742
743static const char *getF (lua_State *L, void *ud, size_t *size) { 743static const char *getF (lua_State *L, void *ud, size_t *size) {
744 LoadF *lf = (LoadF *)ud; 744 LoadF *lf = (LoadF *)ud;
745 (void)L; /* not used */ 745 UNUSED(L);
746 if (lf->n > 0) { /* are there pre-read characters to be read? */ 746 if (lf->n > 0) { /* are there pre-read characters to be read? */
747 *size = lf->n; /* return them (chars already in buffer) */ 747 *size = lf->n; /* return them (chars already in buffer) */
748 lf->n = 0; /* no more pre-read characters */ 748 lf->n = 0; /* no more pre-read characters */
@@ -856,7 +856,7 @@ typedef struct LoadS {
856 856
857static const char *getS (lua_State *L, void *ud, size_t *size) { 857static const char *getS (lua_State *L, void *ud, size_t *size) {
858 LoadS *ls = (LoadS *)ud; 858 LoadS *ls = (LoadS *)ud;
859 (void)L; /* not used */ 859 UNUSED(L);
860 if (ls->size == 0) return NULL; 860 if (ls->size == 0) return NULL;
861 *size = ls->size; 861 *size = ls->size;
862 ls->size = 0; 862 ls->size = 0;
@@ -1046,8 +1046,8 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s,
1046} 1046}
1047 1047
1048 1048
1049static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { 1049void *luaL_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
1050 (void)ud; (void)osize; /* not used */ 1050 UNUSED(ud); UNUSED(osize);
1051 if (nsize == 0) { 1051 if (nsize == 0) {
1052 free(ptr); 1052 free(ptr);
1053 return NULL; 1053 return NULL;
@@ -1172,7 +1172,7 @@ static unsigned int luai_makeseed (void) {
1172 1172
1173 1173
1174LUALIB_API unsigned int luaL_makeseed (lua_State *L) { 1174LUALIB_API unsigned int luaL_makeseed (lua_State *L) {
1175 (void)L; /* unused */ 1175 UNUSED(L);
1176 return luai_makeseed(); 1176 return luai_makeseed();
1177} 1177}
1178 1178
@@ -1182,7 +1182,7 @@ LUALIB_API unsigned int luaL_makeseed (lua_State *L) {
1182** as a macro. 1182** as a macro.
1183*/ 1183*/
1184LUALIB_API lua_State *(luaL_newstate) (void) { 1184LUALIB_API lua_State *(luaL_newstate) (void) {
1185 lua_State *L = lua_newstate(l_alloc, NULL, luai_makeseed()); 1185 lua_State *L = lua_newstate(luaL_alloc, NULL, luaL_makeseed(NULL));
1186 if (l_likely(L)) { 1186 if (l_likely(L)) {
1187 lua_atpanic(L, &panic); 1187 lua_atpanic(L, &panic);
1188 lua_setwarnf(L, warnfoff, L); /* default is warnings off */ 1188 lua_setwarnf(L, warnfoff, L); /* default is warnings off */
diff --git a/lauxlib.h b/lauxlib.h
index d8522098..7f1d3ca1 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -81,6 +81,9 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def,
81LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); 81LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
82LUALIB_API int (luaL_execresult) (lua_State *L, int stat); 82LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
83 83
84LUALIB_API void *luaL_alloc (void *ud, void *ptr, size_t osize,
85 size_t nsize);
86
84 87
85/* predefined references */ 88/* predefined references */
86#define LUA_NOREF (-2) 89#define LUA_NOREF (-2)
diff --git a/manual/manual.of b/manual/manual.of
index beea41f9..ad273d62 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -3079,11 +3079,12 @@ the allocator must behave like @id{realloc}.
3079In particular, the allocator returns @id{NULL} 3079In particular, the allocator returns @id{NULL}
3080if and only if it cannot fulfill the request. 3080if and only if it cannot fulfill the request.
3081 3081
3082Here is a simple implementation for the @x{allocator function}. 3082Here is a simple implementation for the @x{allocator function},
3083It is used in the auxiliary library by @Lid{luaL_newstate}. 3083corresponding to the function @Lid{luaL_alloc} from the
3084auxiliary library.
3084@verbatim{ 3085@verbatim{
3085static void *l_alloc (void *ud, void *ptr, size_t osize, 3086void *luaL_alloc (void *ud, void *ptr, size_t osize,
3086 size_t nsize) { 3087 size_t nsize) {
3087 (void)ud; (void)osize; /* not used */ 3088 (void)ud; (void)osize; /* not used */
3088 if (nsize == 0) { 3089 if (nsize == 0) {
3089 free(ptr); 3090 free(ptr);
@@ -5988,9 +5989,8 @@ it does not run it.
5988@apii{0,0,-} 5989@apii{0,0,-}
5989 5990
5990Returns a value with a weak attempt for randomness. 5991Returns a value with a weak attempt for randomness.
5991(It produces that value based on the current date and time 5992The parameter @id{L} can be @id{NULL}
5992and the address of an internal variable, 5993if there is no Lua state available.
5993in case the machine has Address Space Layout Randomization.)
5994 5994
5995} 5995}
5996 5996
@@ -6046,8 +6046,9 @@ with @id{tname} in the registry.
6046@apii{0,0,-} 6046@apii{0,0,-}
6047 6047
6048Creates a new Lua state. 6048Creates a new Lua state.
6049It calls @Lid{lua_newstate} with an 6049It calls @Lid{lua_newstate} with @Lid{luaL_alloc} as
6050allocator based on the @N{ISO C} allocation functions 6050the allocator function and the result of @T{luaL_makeseed(NULL)}
6051as the seed,
6051and then sets a warning function and a panic function @see{C-error} 6052and then sets a warning function and a panic function @see{C-error}
6052that print messages to the standard error output. 6053that print messages to the standard error output.
6053 6054
@@ -6272,6 +6273,15 @@ in the registry @seeC{luaL_newmetatable}.
6272} 6273}
6273 6274
6274@APIEntry{ 6275@APIEntry{
6276void *luaL_alloc (void *ud, void *ptr, size_t osize, size_t nsize);|
6277
6278A standard allocator function for Lua @seeF{lua_Alloc},
6279built on top of the C functions @id{realloc} and @id{free}.
6280
6281}
6282
6283
6284@APIEntry{
6275typedef struct luaL_Stream { 6285typedef struct luaL_Stream {
6276 FILE *f; 6286 FILE *f;
6277 lua_CFunction closef; 6287 lua_CFunction closef;