]>
git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/lua-5.3.1/src/lmem.c
2 ** $Id: lmem.c,v 1.91 2015/03/06 19:45:54 roberto Exp $
3 ** Interface to Memory Manager
4 ** See Copyright Notice in lua.h
27 ** About the realloc function:
28 ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
29 ** ('osize' is the old size, 'nsize' is the new size)
31 ** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no
34 ** * frealloc(ud, p, x, 0) frees the block 'p'
35 ** (in this specific case, frealloc must return NULL);
36 ** particularly, frealloc(ud, NULL, 0, 0) does nothing
37 ** (which is equivalent to free(NULL) in ISO C)
39 ** frealloc returns NULL if it cannot create or reallocate the area
40 ** (any reallocation to an equal or smaller size cannot fail!)
45 #define MINSIZEARRAY 4
48 void *luaM_growaux_ (lua_State
*L
, void *block
, int *size
, size_t size_elems
,
49 int limit
, const char *what
) {
52 if (*size
>= limit
/2) { /* cannot double it? */
53 if (*size
>= limit
) /* cannot grow even a little? */
54 luaG_runerror(L
, "too many %s (limit is %d)", what
, limit
);
55 newsize
= limit
; /* still have at least one free place */
59 if (newsize
< MINSIZEARRAY
)
60 newsize
= MINSIZEARRAY
; /* minimum size */
62 newblock
= luaM_reallocv(L
, block
, *size
, newsize
, size_elems
);
63 *size
= newsize
; /* update only when everything else is OK */
68 l_noret
luaM_toobig (lua_State
*L
) {
69 luaG_runerror(L
, "memory allocation error: block too big");
75 ** generic allocation routine.
77 void *luaM_realloc_ (lua_State
*L
, void *block
, size_t osize
, size_t nsize
) {
79 global_State
*g
= G(L
);
80 size_t realosize
= (block
) ? osize
: 0;
81 lua_assert((realosize
== 0) == (block
== NULL
));
82 #if defined(HARDMEMTESTS)
83 if (nsize
> realosize
&& g
->gcrunning
)
84 luaC_fullgc(L
, 1); /* force a GC whenever possible */
86 newblock
= (*g
->frealloc
)(g
->ud
, block
, osize
, nsize
);
87 if (newblock
== NULL
&& nsize
> 0) {
88 lua_assert(nsize
> realosize
); /* cannot fail when shrinking a block */
89 if (g
->version
) { /* is state fully built? */
90 luaC_fullgc(L
, 1); /* try to free some memory... */
91 newblock
= (*g
->frealloc
)(g
->ud
, block
, osize
, nsize
); /* try again */
94 luaD_throw(L
, LUA_ERRMEM
);
96 lua_assert((nsize
== 0) == (newblock
== NULL
));
97 g
->GCdebt
= (g
->GCdebt
+ nsize
) - realosize
;