xref: /minix3/external/mit/lua/dist/src/lmem.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: lmem.c,v 1.4 2015/10/08 13:21:00 mbalmer Exp $	*/
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*
4*0a6a1f1dSLionel Sambuc ** Id: lmem.c,v 1.91 2015/03/06 19:45:54 roberto Exp
511be35a1SLionel Sambuc ** Interface to Memory Manager
611be35a1SLionel Sambuc ** See Copyright Notice in lua.h
711be35a1SLionel Sambuc */
811be35a1SLionel Sambuc 
911be35a1SLionel Sambuc #define lmem_c
1011be35a1SLionel Sambuc #define LUA_CORE
1111be35a1SLionel Sambuc 
12*0a6a1f1dSLionel Sambuc #include "lprefix.h"
13*0a6a1f1dSLionel Sambuc 
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
16*0a6a1f1dSLionel Sambuc #include <stddef.h>
17*0a6a1f1dSLionel Sambuc #endif
18*0a6a1f1dSLionel Sambuc 
1911be35a1SLionel Sambuc #include "lua.h"
2011be35a1SLionel Sambuc 
2111be35a1SLionel Sambuc #include "ldebug.h"
2211be35a1SLionel Sambuc #include "ldo.h"
23*0a6a1f1dSLionel Sambuc #include "lgc.h"
2411be35a1SLionel Sambuc #include "lmem.h"
2511be35a1SLionel Sambuc #include "lobject.h"
2611be35a1SLionel Sambuc #include "lstate.h"
2711be35a1SLionel Sambuc 
2811be35a1SLionel Sambuc 
2911be35a1SLionel Sambuc 
3011be35a1SLionel Sambuc /*
3111be35a1SLionel Sambuc ** About the realloc function:
3211be35a1SLionel Sambuc ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
33*0a6a1f1dSLionel Sambuc ** ('osize' is the old size, 'nsize' is the new size)
3411be35a1SLionel Sambuc **
35*0a6a1f1dSLionel Sambuc ** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no
36*0a6a1f1dSLionel Sambuc ** matter 'x').
3711be35a1SLionel Sambuc **
38*0a6a1f1dSLionel Sambuc ** * frealloc(ud, p, x, 0) frees the block 'p'
39*0a6a1f1dSLionel Sambuc ** (in this specific case, frealloc must return NULL);
4011be35a1SLionel Sambuc ** particularly, frealloc(ud, NULL, 0, 0) does nothing
41*0a6a1f1dSLionel Sambuc ** (which is equivalent to free(NULL) in ISO C)
4211be35a1SLionel Sambuc **
4311be35a1SLionel Sambuc ** frealloc returns NULL if it cannot create or reallocate the area
4411be35a1SLionel Sambuc ** (any reallocation to an equal or smaller size cannot fail!)
4511be35a1SLionel Sambuc */
4611be35a1SLionel Sambuc 
4711be35a1SLionel Sambuc 
4811be35a1SLionel Sambuc 
4911be35a1SLionel Sambuc #define MINSIZEARRAY	4
5011be35a1SLionel Sambuc 
5111be35a1SLionel Sambuc 
luaM_growaux_(lua_State * L,void * block,int * size,size_t size_elems,int limit,const char * what)5211be35a1SLionel Sambuc void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
53*0a6a1f1dSLionel Sambuc                      int limit, const char *what) {
5411be35a1SLionel Sambuc   void *newblock;
5511be35a1SLionel Sambuc   int newsize;
5611be35a1SLionel Sambuc   if (*size >= limit/2) {  /* cannot double it? */
5711be35a1SLionel Sambuc     if (*size >= limit)  /* cannot grow even a little? */
58*0a6a1f1dSLionel Sambuc       luaG_runerror(L, "too many %s (limit is %d)", what, limit);
5911be35a1SLionel Sambuc     newsize = limit;  /* still have at least one free place */
6011be35a1SLionel Sambuc   }
6111be35a1SLionel Sambuc   else {
6211be35a1SLionel Sambuc     newsize = (*size)*2;
6311be35a1SLionel Sambuc     if (newsize < MINSIZEARRAY)
6411be35a1SLionel Sambuc       newsize = MINSIZEARRAY;  /* minimum size */
6511be35a1SLionel Sambuc   }
6611be35a1SLionel Sambuc   newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
6711be35a1SLionel Sambuc   *size = newsize;  /* update only when everything else is OK */
6811be35a1SLionel Sambuc   return newblock;
6911be35a1SLionel Sambuc }
7011be35a1SLionel Sambuc 
7111be35a1SLionel Sambuc 
luaM_toobig(lua_State * L)72*0a6a1f1dSLionel Sambuc l_noret luaM_toobig (lua_State *L) {
7311be35a1SLionel Sambuc   luaG_runerror(L, "memory allocation error: block too big");
7411be35a1SLionel Sambuc }
7511be35a1SLionel Sambuc 
7611be35a1SLionel Sambuc 
7711be35a1SLionel Sambuc 
7811be35a1SLionel Sambuc /*
7911be35a1SLionel Sambuc ** generic allocation routine.
8011be35a1SLionel Sambuc */
luaM_realloc_(lua_State * L,void * block,size_t osize,size_t nsize)8111be35a1SLionel Sambuc void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
82*0a6a1f1dSLionel Sambuc   void *newblock;
8311be35a1SLionel Sambuc   global_State *g = G(L);
84*0a6a1f1dSLionel Sambuc   size_t realosize = (block) ? osize : 0;
85*0a6a1f1dSLionel Sambuc   lua_assert((realosize == 0) == (block == NULL));
86*0a6a1f1dSLionel Sambuc #if defined(HARDMEMTESTS)
87*0a6a1f1dSLionel Sambuc   if (nsize > realosize && g->gcrunning)
88*0a6a1f1dSLionel Sambuc     luaC_fullgc(L, 1);  /* force a GC whenever possible */
89*0a6a1f1dSLionel Sambuc #endif
90*0a6a1f1dSLionel Sambuc   newblock = (*g->frealloc)(g->ud, block, osize, nsize);
91*0a6a1f1dSLionel Sambuc   if (newblock == NULL && nsize > 0) {
92*0a6a1f1dSLionel Sambuc     lua_assert(nsize > realosize);  /* cannot fail when shrinking a block */
93*0a6a1f1dSLionel Sambuc     if (g->version) {  /* is state fully built? */
94*0a6a1f1dSLionel Sambuc       luaC_fullgc(L, 1);  /* try to free some memory... */
95*0a6a1f1dSLionel Sambuc       newblock = (*g->frealloc)(g->ud, block, osize, nsize);  /* try again */
96*0a6a1f1dSLionel Sambuc     }
97*0a6a1f1dSLionel Sambuc     if (newblock == NULL)
9811be35a1SLionel Sambuc       luaD_throw(L, LUA_ERRMEM);
99*0a6a1f1dSLionel Sambuc   }
100*0a6a1f1dSLionel Sambuc   lua_assert((nsize == 0) == (newblock == NULL));
101*0a6a1f1dSLionel Sambuc   g->GCdebt = (g->GCdebt + nsize) - realosize;
102*0a6a1f1dSLionel Sambuc   return newblock;
10311be35a1SLionel Sambuc }
10411be35a1SLionel Sambuc 
105