/*1** $Id: lmem.c $2** Interface to Memory Manager3** See Copyright Notice in lua.h4*/56#define lmem_c7#define LUA_CORE89#include "lprefix.h"101112#include <stddef.h>1314#include "lua.h"1516#include "ldebug.h"17#include "ldo.h"18#include "lgc.h"19#include "lmem.h"20#include "lobject.h"21#include "lstate.h"22232425/*26** About the realloc function:27** void *frealloc (void *ud, void *ptr, size_t osize, size_t nsize);28** ('osize' is the old size, 'nsize' is the new size)29**30** - frealloc(ud, p, x, 0) frees the block 'p' and returns NULL.31** Particularly, frealloc(ud, NULL, 0, 0) does nothing,32** which is equivalent to free(NULL) in ISO C.33**34** - frealloc(ud, NULL, x, s) creates a new block of size 's'35** (no matter 'x'). Returns NULL if it cannot create the new block.36**37** - otherwise, frealloc(ud, b, x, y) reallocates the block 'b' from38** size 'x' to size 'y'. Returns NULL if it cannot reallocate the39** block to the new size.40*/414243/*44** Macro to call the allocation function.45*/46#define callfrealloc(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns))474849/*50** When an allocation fails, it will try again after an emergency51** collection, except when it cannot run a collection. The GC should52** not be called while the state is not fully built, as the collector53** is not yet fully initialized. Also, it should not be called when54** 'gcstopem' is true, because then the interpreter is in the middle of55** a collection step.56*/57#define cantryagain(g) (completestate(g) && !g->gcstopem)5859606162#if defined(EMERGENCYGCTESTS)63/*64** First allocation will fail except when freeing a block (frees never65** fail) and when it cannot try again; this fail will trigger 'tryagain'66** and a full GC cycle at every allocation.67*/68static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {69if (ns > 0 && cantryagain(g))70return NULL; /* fail */71else /* normal allocation */72return callfrealloc(g, block, os, ns);73}74#else75#define firsttry(g,block,os,ns) callfrealloc(g, block, os, ns)76#endif777879808182/*83** {==================================================================84** Functions to allocate/deallocate arrays for the Parser85** ===================================================================86*/8788/*89** Minimum size for arrays during parsing, to avoid overhead of90** reallocating to size 1, then 2, and then 4. All these arrays91** will be reallocated to exact sizes or erased when parsing ends.92*/93#define MINSIZEARRAY 4949596void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,97int size_elems, int limit, const char *what) {98void *newblock;99int size = *psize;100if (nelems + 1 <= size) /* does one extra element still fit? */101return block; /* nothing to be done */102if (size >= limit / 2) { /* cannot double it? */103if (l_unlikely(size >= limit)) /* cannot grow even a little? */104luaG_runerror(L, "too many %s (limit is %d)", what, limit);105size = limit; /* still have at least one free place */106}107else {108size *= 2;109if (size < MINSIZEARRAY)110size = MINSIZEARRAY; /* minimum size */111}112lua_assert(nelems + 1 <= size && size <= limit);113/* 'limit' ensures that multiplication will not overflow */114newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems,115cast_sizet(size) * size_elems);116*psize = size; /* update only when everything else is OK */117return newblock;118}119120121/*122** In prototypes, the size of the array is also its number of123** elements (to save memory). So, if it cannot shrink an array124** to its number of elements, the only option is to raise an125** error.126*/127void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,128int final_n, int size_elem) {129void *newblock;130size_t oldsize = cast_sizet((*size) * size_elem);131size_t newsize = cast_sizet(final_n * size_elem);132lua_assert(newsize <= oldsize);133newblock = luaM_saferealloc_(L, block, oldsize, newsize);134*size = final_n;135return newblock;136}137138/* }================================================================== */139140141l_noret luaM_toobig (lua_State *L) {142luaG_runerror(L, "memory allocation error: block too big");143}144145146/*147** Free memory148*/149void luaM_free_ (lua_State *L, void *block, size_t osize) {150global_State *g = G(L);151lua_assert((osize == 0) == (block == NULL));152callfrealloc(g, block, osize, 0);153g->GCdebt -= osize;154}155156157/*158** In case of allocation fail, this function will do an emergency159** collection to free some memory and then try the allocation again.160*/161static void *tryagain (lua_State *L, void *block,162size_t osize, size_t nsize) {163global_State *g = G(L);164if (cantryagain(g)) {165luaC_fullgc(L, 1); /* try to free some memory... */166return callfrealloc(g, block, osize, nsize); /* try again */167}168else return NULL; /* cannot run an emergency collection */169}170171172/*173** Generic allocation routine.174*/175void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {176void *newblock;177global_State *g = G(L);178lua_assert((osize == 0) == (block == NULL));179newblock = firsttry(g, block, osize, nsize);180if (l_unlikely(newblock == NULL && nsize > 0)) {181newblock = tryagain(L, block, osize, nsize);182if (newblock == NULL) /* still no memory? */183return NULL; /* do not update 'GCdebt' */184}185lua_assert((nsize == 0) == (newblock == NULL));186g->GCdebt = (g->GCdebt + nsize) - osize;187return newblock;188}189190191void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize,192size_t nsize) {193void *newblock = luaM_realloc_(L, block, osize, nsize);194if (l_unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */195luaM_error(L);196return newblock;197}198199200void *luaM_malloc_ (lua_State *L, size_t size, int tag) {201if (size == 0)202return NULL; /* that's all */203else {204global_State *g = G(L);205void *newblock = firsttry(g, NULL, tag, size);206if (l_unlikely(newblock == NULL)) {207newblock = tryagain(L, NULL, tag, size);208if (newblock == NULL)209luaM_error(L);210}211g->GCdebt += size;212return newblock;213}214}215216217