Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
lmem.c File Reference
#include "lprefix.h"
#include <stddef.h>
#include "lua.h"
#include "ldebug.h"
#include "ldo.h"
#include "lgc.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"

Go to the source code of this file.

Macros

#define lmem_c
 
#define LUA_CORE
 
#define firsttry(g, block, os, ns)   ((*g->frealloc)(g->ud, block, os, ns))
 
#define MINSIZEARRAY   4
 

Functions

void * luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, int size_elems, int limit, const char *what)
 
void * luaM_shrinkvector_ (lua_State *L, void *block, int *size, int final_n, int size_elem)
 
l_noret luaM_toobig (lua_State *L)
 
void luaM_free_ (lua_State *L, void *block, size_t osize)
 
static void * tryagain (lua_State *L, void *block, size_t osize, size_t nsize)
 
void * luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize)
 
void * luaM_saferealloc_ (lua_State *L, void *block, size_t osize, size_t nsize)
 
void * luaM_malloc_ (lua_State *L, size_t size, int tag)
 

Macro Definition Documentation

◆ firsttry

#define firsttry ( g,
block,
os,
ns )   ((*g->frealloc)(g->ud, block, os, ns))

Definition at line 38 of file lua-5.4.3/src/lmem.c.

Referenced by luaM_malloc_(), and luaM_realloc_().

◆ lmem_c

#define lmem_c

Definition at line 7 of file lua-5.4.3/src/lmem.c.

◆ LUA_CORE

#define LUA_CORE

Definition at line 8 of file lua-5.4.3/src/lmem.c.

◆ MINSIZEARRAY

#define MINSIZEARRAY   4

Definition at line 76 of file lua-5.4.3/src/lmem.c.

Referenced by luaM_growaux_().

Function Documentation

◆ luaM_free_()

void luaM_free_ ( lua_State * L,
void * block,
size_t osize )

Definition at line 132 of file lua-5.4.3/src/lmem.c.

132 {
133 global_State *g = G(L);
134 lua_assert((osize == 0) == (block == NULL));
135 (*g->frealloc)(g->ud, block, osize, 0);
136 g->GCdebt -= osize;
137}
#define NULL
Definition gmacros.h:924
#define lua_assert(c)
static void block(LexState *ls)
#define G(L)

References block(), global_State::frealloc, G, global_State::GCdebt, lua_assert, NULL, and global_State::ud.

◆ luaM_growaux_()

void * luaM_growaux_ ( lua_State * L,
void * block,
int nelems,
int * psize,
int size_elems,
int limit,
const char * what )

Definition at line 79 of file lua-5.4.3/src/lmem.c.

80 {
81 void *newblock;
82 int size = *psize;
83 if (nelems + 1 <= size) /* does one extra element still fit? */
84 return block; /* nothing to be done */
85 if (size >= limit / 2) { /* cannot double it? */
86 if (l_unlikely(size >= limit)) /* cannot grow even a little? */
87 luaG_runerror(L, "too many %s (limit is %d)", what, limit);
88 size = limit; /* still have at least one free place */
89 }
90 else {
91 size *= 2;
92 if (size < MINSIZEARRAY)
93 size = MINSIZEARRAY; /* minimum size */
94 }
95 lua_assert(nelems + 1 <= size && size <= limit);
96 /* 'limit' ensures that multiplication will not overflow */
97 newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems,
98 cast_sizet(size) * size_elems);
99 *psize = size; /* update only when everything else is OK */
100 return newblock;
101}
void luaG_runerror(lua_State *L, const char *fmt,...)
#define cast_sizet(i)
void * luaM_saferealloc_(lua_State *L, void *block, size_t osize, size_t nsize)
#define MINSIZEARRAY

References block(), cast_sizet, lua_assert, luaG_runerror(), luaM_saferealloc_(), and MINSIZEARRAY.

◆ luaM_malloc_()

void * luaM_malloc_ ( lua_State * L,
size_t size,
int tag )

Definition at line 187 of file lua-5.4.3/src/lmem.c.

187 {
188 if (size == 0)
189 return NULL; /* that's all */
190 else {
191 global_State *g = G(L);
192 void *newblock = firsttry(g, NULL, tag, size);
193 if (l_unlikely(newblock == NULL)) {
194 newblock = tryagain(L, NULL, tag, size);
195 if (newblock == NULL)
196 luaM_error(L);
197 }
198 g->GCdebt += size;
199 return newblock;
200 }
201}
#define firsttry(g, block, os, ns)
static void * tryagain(lua_State *L, void *block, size_t osize, size_t nsize)
#define luaM_error(L)

References firsttry, G, global_State::GCdebt, luaM_error, NULL, and tryagain().

◆ luaM_realloc_()

void * luaM_realloc_ ( lua_State * L,
void * block,
size_t osize,
size_t nsize )

Definition at line 162 of file lua-5.4.3/src/lmem.c.

162 {
163 void *newblock;
164 global_State *g = G(L);
165 lua_assert((osize == 0) == (block == NULL));
166 newblock = firsttry(g, block, osize, nsize);
167 if (l_unlikely(newblock == NULL && nsize > 0)) {
168 newblock = tryagain(L, block, osize, nsize);
169 if (newblock == NULL) /* still no memory? */
170 return NULL; /* do not update 'GCdebt' */
171 }
172 lua_assert((nsize == 0) == (newblock == NULL));
173 g->GCdebt = (g->GCdebt + nsize) - osize;
174 return newblock;
175}

References block(), firsttry, G, global_State::GCdebt, lua_assert, NULL, and tryagain().

Referenced by luaM_saferealloc_().

◆ luaM_saferealloc_()

void * luaM_saferealloc_ ( lua_State * L,
void * block,
size_t osize,
size_t nsize )

Definition at line 178 of file lua-5.4.3/src/lmem.c.

179 {
180 void *newblock = luaM_realloc_(L, block, osize, nsize);
181 if (l_unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */
182 luaM_error(L);
183 return newblock;
184}
void * luaM_realloc_(lua_State *L, void *block, size_t osize, size_t nsize)

References block(), luaM_error, luaM_realloc_(), and NULL.

Referenced by luaM_growaux_(), and luaM_shrinkvector_().

◆ luaM_shrinkvector_()

void * luaM_shrinkvector_ ( lua_State * L,
void * block,
int * size,
int final_n,
int size_elem )

Definition at line 110 of file lua-5.4.3/src/lmem.c.

111 {
112 void *newblock;
113 size_t oldsize = cast_sizet((*size) * size_elem);
114 size_t newsize = cast_sizet(final_n * size_elem);
115 lua_assert(newsize <= oldsize);
116 newblock = luaM_saferealloc_(L, block, oldsize, newsize);
117 *size = final_n;
118 return newblock;
119}

References block(), cast_sizet, lua_assert, and luaM_saferealloc_().

◆ luaM_toobig()

l_noret luaM_toobig ( lua_State * L)

Definition at line 124 of file lua-5.4.3/src/lmem.c.

124 {
125 luaG_runerror(L, "memory allocation error: block too big");
126}

References luaG_runerror().

◆ tryagain()

static void * tryagain ( lua_State * L,
void * block,
size_t osize,
size_t nsize )
static

Definition at line 148 of file lua-5.4.3/src/lmem.c.

149 {
150 global_State *g = G(L);
151 if (completestate(g) && !g->gcstopem) {
152 luaC_fullgc(L, 1); /* try to free some memory... */
153 return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
154 }
155 else return NULL; /* cannot free any memory without a full state */
156}
void luaC_fullgc(lua_State *L)
#define completestate(g)

References block(), completestate, global_State::frealloc, G, global_State::gcstopem, luaC_fullgc(), NULL, and global_State::ud.

Referenced by luaM_malloc_(), and luaM_realloc_().