diff options
| author | Roberto I <roberto@inf.puc-rio.br> | 2025-10-17 13:53:35 -0300 |
|---|---|---|
| committer | Roberto I <roberto@inf.puc-rio.br> | 2025-10-17 13:53:35 -0300 |
| commit | b352217b8498a5ed8f6c954b3da365fcbb89751f (patch) | |
| tree | 4a2a7feb7bcf9d0144af6923807904b9bc0e4037 | |
| parent | 9c66903cc55388006a833f0f3911ea81fa86edea (diff) | |
| download | lua-b352217b8498a5ed8f6c954b3da365fcbb89751f.tar.gz lua-b352217b8498a5ed8f6c954b3da365fcbb89751f.tar.bz2 lua-b352217b8498a5ed8f6c954b3da365fcbb89751f.zip | |
Standard allocator function added to the API
That makes easier to redefine luaL_newstate.
Diffstat (limited to '')
| -rw-r--r-- | lauxlib.c | 12 | ||||
| -rw-r--r-- | lauxlib.h | 3 | ||||
| -rw-r--r-- | manual/manual.of | 28 |
3 files changed, 28 insertions, 15 deletions
| @@ -742,7 +742,7 @@ typedef struct LoadF { | |||
| 742 | 742 | ||
| 743 | static const char *getF (lua_State *L, void *ud, size_t *size) { | 743 | static 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 | ||
| 857 | static const char *getS (lua_State *L, void *ud, size_t *size) { | 857 | static 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 | ||
| 1049 | static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { | 1049 | void *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 | ||
| 1174 | LUALIB_API unsigned int luaL_makeseed (lua_State *L) { | 1174 | LUALIB_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 | */ |
| 1184 | LUALIB_API lua_State *(luaL_newstate) (void) { | 1184 | LUALIB_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 */ |
| @@ -81,6 +81,9 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, | |||
| 81 | LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); | 81 | LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); |
| 82 | LUALIB_API int (luaL_execresult) (lua_State *L, int stat); | 82 | LUALIB_API int (luaL_execresult) (lua_State *L, int stat); |
| 83 | 83 | ||
| 84 | LUALIB_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}. | |||
| 3079 | In particular, the allocator returns @id{NULL} | 3079 | In particular, the allocator returns @id{NULL} |
| 3080 | if and only if it cannot fulfill the request. | 3080 | if and only if it cannot fulfill the request. |
| 3081 | 3081 | ||
| 3082 | Here is a simple implementation for the @x{allocator function}. | 3082 | Here is a simple implementation for the @x{allocator function}, |
| 3083 | It is used in the auxiliary library by @Lid{luaL_newstate}. | 3083 | corresponding to the function @Lid{luaL_alloc} from the |
| 3084 | auxiliary library. | ||
| 3084 | @verbatim{ | 3085 | @verbatim{ |
| 3085 | static void *l_alloc (void *ud, void *ptr, size_t osize, | 3086 | void *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 | ||
| 5990 | Returns a value with a weak attempt for randomness. | 5991 | Returns a value with a weak attempt for randomness. |
| 5991 | (It produces that value based on the current date and time | 5992 | The parameter @id{L} can be @id{NULL} |
| 5992 | and the address of an internal variable, | 5993 | if there is no Lua state available. |
| 5993 | in 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 | ||
| 6048 | Creates a new Lua state. | 6048 | Creates a new Lua state. |
| 6049 | It calls @Lid{lua_newstate} with an | 6049 | It calls @Lid{lua_newstate} with @Lid{luaL_alloc} as |
| 6050 | allocator based on the @N{ISO C} allocation functions | 6050 | the allocator function and the result of @T{luaL_makeseed(NULL)} |
| 6051 | as the seed, | ||
| 6051 | and then sets a warning function and a panic function @see{C-error} | 6052 | and then sets a warning function and a panic function @see{C-error} |
| 6052 | that print messages to the standard error output. | 6053 | that 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{ |
| 6276 | void *luaL_alloc (void *ud, void *ptr, size_t osize, size_t nsize);| | ||
| 6277 | |||
| 6278 | A standard allocator function for Lua @seeF{lua_Alloc}, | ||
| 6279 | built on top of the C functions @id{realloc} and @id{free}. | ||
| 6280 | |||
| 6281 | } | ||
| 6282 | |||
| 6283 | |||
| 6284 | @APIEntry{ | ||
| 6275 | typedef struct luaL_Stream { | 6285 | typedef struct luaL_Stream { |
| 6276 | FILE *f; | 6286 | FILE *f; |
| 6277 | lua_CFunction closef; | 6287 | lua_CFunction closef; |
