summaryrefslogtreecommitdiff
path: root/lmem.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2013-04-12 15:48:47 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2013-04-12 15:48:47 -0300
commit2ca518141350667e38941eac35b76980095d2fdc (patch)
tree82595f0dfd57df5782905621298c8aae68b46389 /lmem.c
parentf5ae26ec6c2eb69f03f316e59c1e1977c52c7c23 (diff)
downloadlua-2ca518141350667e38941eac35b76980095d2fdc.tar.gz
lua-2ca518141350667e38941eac35b76980095d2fdc.tar.bz2
lua-2ca518141350667e38941eac35b76980095d2fdc.zip
Branch 5.2 - new releases for 5.2 go from here, main trunk goes
for next version
Diffstat (limited to 'lmem.c')
-rw-r--r--lmem.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/lmem.c b/lmem.c
new file mode 100644
index 00000000..3f88496e
--- /dev/null
+++ b/lmem.c
@@ -0,0 +1,99 @@
1/*
2** $Id: lmem.c,v 1.84 2012/05/23 15:41:53 roberto Exp $
3** Interface to Memory Manager
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stddef.h>
9
10#define lmem_c
11#define LUA_CORE
12
13#include "lua.h"
14
15#include "ldebug.h"
16#include "ldo.h"
17#include "lgc.h"
18#include "lmem.h"
19#include "lobject.h"
20#include "lstate.h"
21
22
23
24/*
25** About the realloc function:
26** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
27** (`osize' is the old size, `nsize' is the new size)
28**
29** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no
30** matter 'x').
31**
32** * frealloc(ud, p, x, 0) frees the block `p'
33** (in this specific case, frealloc must return NULL);
34** particularly, frealloc(ud, NULL, 0, 0) does nothing
35** (which is equivalent to free(NULL) in ANSI C)
36**
37** frealloc returns NULL if it cannot create or reallocate the area
38** (any reallocation to an equal or smaller size cannot fail!)
39*/
40
41
42
43#define MINSIZEARRAY 4
44
45
46void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
47 int limit, const char *what) {
48 void *newblock;
49 int newsize;
50 if (*size >= limit/2) { /* cannot double it? */
51 if (*size >= limit) /* cannot grow even a little? */
52 luaG_runerror(L, "too many %s (limit is %d)", what, limit);
53 newsize = limit; /* still have at least one free place */
54 }
55 else {
56 newsize = (*size)*2;
57 if (newsize < MINSIZEARRAY)
58 newsize = MINSIZEARRAY; /* minimum size */
59 }
60 newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
61 *size = newsize; /* update only when everything else is OK */
62 return newblock;
63}
64
65
66l_noret luaM_toobig (lua_State *L) {
67 luaG_runerror(L, "memory allocation error: block too big");
68}
69
70
71
72/*
73** generic allocation routine.
74*/
75void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
76 void *newblock;
77 global_State *g = G(L);
78 size_t realosize = (block) ? osize : 0;
79 lua_assert((realosize == 0) == (block == NULL));
80#if defined(HARDMEMTESTS)
81 if (nsize > realosize && g->gcrunning)
82 luaC_fullgc(L, 1); /* force a GC whenever possible */
83#endif
84 newblock = (*g->frealloc)(g->ud, block, osize, nsize);
85 if (newblock == NULL && nsize > 0) {
86 api_check(L, nsize > realosize,
87 "realloc cannot fail when shrinking a block");
88 if (g->gcrunning) {
89 luaC_fullgc(L, 1); /* try to free some memory... */
90 newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
91 }
92 if (newblock == NULL)
93 luaD_throw(L, LUA_ERRMEM);
94 }
95 lua_assert((nsize == 0) == (newblock == NULL));
96 g->GCdebt = (g->GCdebt + nsize) - realosize;
97 return newblock;
98}
99