Standard allocator function added to the API
That makes easier to redefine luaL_newstate.
This commit is contained in:
12
lauxlib.c
12
lauxlib.c
@@ -742,7 +742,7 @@ typedef struct LoadF {
|
|||||||
|
|
||||||
static const char *getF (lua_State *L, void *ud, size_t *size) {
|
static const char *getF (lua_State *L, void *ud, size_t *size) {
|
||||||
LoadF *lf = (LoadF *)ud;
|
LoadF *lf = (LoadF *)ud;
|
||||||
(void)L; /* not used */
|
UNUSED(L);
|
||||||
if (lf->n > 0) { /* are there pre-read characters to be read? */
|
if (lf->n > 0) { /* are there pre-read characters to be read? */
|
||||||
*size = lf->n; /* return them (chars already in buffer) */
|
*size = lf->n; /* return them (chars already in buffer) */
|
||||||
lf->n = 0; /* no more pre-read characters */
|
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) {
|
static const char *getS (lua_State *L, void *ud, size_t *size) {
|
||||||
LoadS *ls = (LoadS *)ud;
|
LoadS *ls = (LoadS *)ud;
|
||||||
(void)L; /* not used */
|
UNUSED(L);
|
||||||
if (ls->size == 0) return NULL;
|
if (ls->size == 0) return NULL;
|
||||||
*size = ls->size;
|
*size = ls->size;
|
||||||
ls->size = 0;
|
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 *luaL_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||||
(void)ud; (void)osize; /* not used */
|
UNUSED(ud); UNUSED(osize);
|
||||||
if (nsize == 0) {
|
if (nsize == 0) {
|
||||||
free(ptr);
|
free(ptr);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -1172,7 +1172,7 @@ static unsigned int luai_makeseed (void) {
|
|||||||
|
|
||||||
|
|
||||||
LUALIB_API unsigned int luaL_makeseed (lua_State *L) {
|
LUALIB_API unsigned int luaL_makeseed (lua_State *L) {
|
||||||
(void)L; /* unused */
|
UNUSED(L);
|
||||||
return luai_makeseed();
|
return luai_makeseed();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1182,7 +1182,7 @@ LUALIB_API unsigned int luaL_makeseed (lua_State *L) {
|
|||||||
** as a macro.
|
** as a macro.
|
||||||
*/
|
*/
|
||||||
LUALIB_API lua_State *(luaL_newstate) (void) {
|
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)) {
|
if (l_likely(L)) {
|
||||||
lua_atpanic(L, &panic);
|
lua_atpanic(L, &panic);
|
||||||
lua_setwarnf(L, warnfoff, L); /* default is warnings off */
|
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,
|
|||||||
LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
|
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 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 */
|
/* predefined references */
|
||||||
#define LUA_NOREF (-2)
|
#define LUA_NOREF (-2)
|
||||||
|
|||||||
@@ -3079,11 +3079,12 @@ the allocator must behave like @id{realloc}.
|
|||||||
In particular, the allocator returns @id{NULL}
|
In particular, the allocator returns @id{NULL}
|
||||||
if and only if it cannot fulfill the request.
|
if and only if it cannot fulfill the request.
|
||||||
|
|
||||||
Here is a simple implementation for the @x{allocator function}.
|
Here is a simple implementation for the @x{allocator function},
|
||||||
It is used in the auxiliary library by @Lid{luaL_newstate}.
|
corresponding to the function @Lid{luaL_alloc} from the
|
||||||
|
auxiliary library.
|
||||||
@verbatim{
|
@verbatim{
|
||||||
static void *l_alloc (void *ud, void *ptr, size_t osize,
|
void *luaL_alloc (void *ud, void *ptr, size_t osize,
|
||||||
size_t nsize) {
|
size_t nsize) {
|
||||||
(void)ud; (void)osize; /* not used */
|
(void)ud; (void)osize; /* not used */
|
||||||
if (nsize == 0) {
|
if (nsize == 0) {
|
||||||
free(ptr);
|
free(ptr);
|
||||||
@@ -5988,9 +5989,8 @@ it does not run it.
|
|||||||
@apii{0,0,-}
|
@apii{0,0,-}
|
||||||
|
|
||||||
Returns a value with a weak attempt for randomness.
|
Returns a value with a weak attempt for randomness.
|
||||||
(It produces that value based on the current date and time
|
The parameter @id{L} can be @id{NULL}
|
||||||
and the address of an internal variable,
|
if there is no Lua state available.
|
||||||
in case the machine has Address Space Layout Randomization.)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6046,8 +6046,9 @@ with @id{tname} in the registry.
|
|||||||
@apii{0,0,-}
|
@apii{0,0,-}
|
||||||
|
|
||||||
Creates a new Lua state.
|
Creates a new Lua state.
|
||||||
It calls @Lid{lua_newstate} with an
|
It calls @Lid{lua_newstate} with @Lid{luaL_alloc} as
|
||||||
allocator based on the @N{ISO C} allocation functions
|
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}
|
and then sets a warning function and a panic function @see{C-error}
|
||||||
that print messages to the standard error output.
|
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{
|
@APIEntry{
|
||||||
typedef struct luaL_Stream {
|
typedef struct luaL_Stream {
|
||||||
FILE *f;
|
FILE *f;
|
||||||
|
|||||||
Reference in New Issue
Block a user