Standard allocator function added to the API

That makes easier to redefine luaL_newstate.
This commit is contained in:
Roberto I 2025-10-17 13:53:35 -03:00
parent 9c66903cc5
commit b352217b84
3 changed files with 28 additions and 15 deletions

View File

@ -742,7 +742,7 @@ typedef struct LoadF {
static const char *getF (lua_State *L, void *ud, size_t *size) {
LoadF *lf = (LoadF *)ud;
(void)L; /* not used */
UNUSED(L);
if (lf->n > 0) { /* are there pre-read characters to be read? */
*size = lf->n; /* return them (chars already in buffer) */
lf->n = 0; /* no more pre-read characters */
@ -856,7 +856,7 @@ typedef struct LoadS {
static const char *getS (lua_State *L, void *ud, size_t *size) {
LoadS *ls = (LoadS *)ud;
(void)L; /* not used */
UNUSED(L);
if (ls->size == 0) return NULL;
*size = ls->size;
ls->size = 0;
@ -1046,8 +1046,8 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s,
}
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
(void)ud; (void)osize; /* not used */
void *luaL_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
UNUSED(ud); UNUSED(osize);
if (nsize == 0) {
free(ptr);
return NULL;
@ -1172,7 +1172,7 @@ static unsigned int luai_makeseed (void) {
LUALIB_API unsigned int luaL_makeseed (lua_State *L) {
(void)L; /* unused */
UNUSED(L);
return luai_makeseed();
}
@ -1182,7 +1182,7 @@ LUALIB_API unsigned int luaL_makeseed (lua_State *L) {
** as a macro.
*/
LUALIB_API lua_State *(luaL_newstate) (void) {
lua_State *L = lua_newstate(l_alloc, NULL, luai_makeseed());
lua_State *L = lua_newstate(luaL_alloc, NULL, luaL_makeseed(NULL));
if (l_likely(L)) {
lua_atpanic(L, &panic);
lua_setwarnf(L, warnfoff, L); /* default is warnings off */

View File

@ -81,6 +81,9 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def,
LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
LUALIB_API void *luaL_alloc (void *ud, void *ptr, size_t osize,
size_t nsize);
/* predefined references */
#define LUA_NOREF (-2)

View File

@ -3079,11 +3079,12 @@ the allocator must behave like @id{realloc}.
In particular, the allocator returns @id{NULL}
if and only if it cannot fulfill the request.
Here is a simple implementation for the @x{allocator function}.
It is used in the auxiliary library by @Lid{luaL_newstate}.
Here is a simple implementation for the @x{allocator function},
corresponding to the function @Lid{luaL_alloc} from the
auxiliary library.
@verbatim{
static void *l_alloc (void *ud, void *ptr, size_t osize,
size_t nsize) {
void *luaL_alloc (void *ud, void *ptr, size_t osize,
size_t nsize) {
(void)ud; (void)osize; /* not used */
if (nsize == 0) {
free(ptr);
@ -5988,9 +5989,8 @@ it does not run it.
@apii{0,0,-}
Returns a value with a weak attempt for randomness.
(It produces that value based on the current date and time
and the address of an internal variable,
in case the machine has Address Space Layout Randomization.)
The parameter @id{L} can be @id{NULL}
if there is no Lua state available.
}
@ -6046,8 +6046,9 @@ with @id{tname} in the registry.
@apii{0,0,-}
Creates a new Lua state.
It calls @Lid{lua_newstate} with an
allocator based on the @N{ISO C} allocation functions
It calls @Lid{lua_newstate} with @Lid{luaL_alloc} as
the allocator function and the result of @T{luaL_makeseed(NULL)}
as the seed,
and then sets a warning function and a panic function @see{C-error}
that print messages to the standard error output.
@ -6271,6 +6272,15 @@ in the registry @seeC{luaL_newmetatable}.
}
@APIEntry{
void *luaL_alloc (void *ud, void *ptr, size_t osize, size_t nsize);|
A standard allocator function for Lua @seeF{lua_Alloc},
built on top of the C functions @id{realloc} and @id{free}.
}
@APIEntry{
typedef struct luaL_Stream {
FILE *f;