The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lmem.cpp
Go to the documentation of this file.
1 /*
2 ** Interface to Memory Manager
3 ** See Copyright Notice in lua.h
4 */
5 
6 
7 #include <stddef.h>
8 
9 #define lmem_c
10 #define LUA_CORE
11 
12 #include "lua.h"
13 
14 #include "ldebug.h"
15 #include "ldo.h"
16 #include "lgc.h"
17 #include "lmem.h"
18 #include "lobject.h"
19 #include "lstate.h"
20 
21 
22 
23 /*
24 ** About the realloc function:
25 ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
26 ** (`osize' is the old size, `nsize' is the new size)
27 **
28 ** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no
29 ** matter 'x').
30 **
31 ** * frealloc(ud, p, x, 0) frees the block `p'
32 ** (in this specific case, frealloc must return NULL);
33 ** particularly, frealloc(ud, NULL, 0, 0) does nothing
34 ** (which is equivalent to free(NULL) in ANSI C)
35 **
36 ** frealloc returns NULL if it cannot create or reallocate the area
37 ** (any reallocation to an equal or smaller size cannot fail!)
38 */
39 
40 
41 
42 #define MINSIZEARRAY 4
43 
44 
45 void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
46  int limit, const char *what) {
47  void *newblock;
48  int newsize;
49  if (*size >= limit/2) { /* cannot double it? */
50  if (*size >= limit) /* cannot grow even a little? */
51  luaG_runerror(L, "too many %s (limit is %d)", what, limit);
52  newsize = limit; /* still have at least one free place */
53  }
54  else {
55  newsize = (*size)*2;
56  if (newsize < MINSIZEARRAY)
57  newsize = MINSIZEARRAY; /* minimum size */
58  }
59  newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
60  *size = newsize; /* update only when everything else is OK */
61  return newblock;
62 }
63 
64 
66  luaG_runerror(L, "memory allocation error: block too big");
67 }
68 
69 
70 
71 /*
72 ** generic allocation routine.
73 */
74 void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
75  void *newblock;
76  global_State *g = G(L);
77  size_t realosize = (block) ? osize : 0;
78  lua_assert((realosize == 0) == (block == NULL));
79 #if defined(HARDMEMTESTS)
80  if (nsize > realosize && g->gcrunning)
81  luaC_fullgc(L, 1); /* force a GC whenever possible */
82 #endif
83  newblock = (*g->frealloc)(g->ud, block, osize, nsize);
84  if (newblock == NULL && nsize > 0) {
85  api_check(L, nsize > realosize,
86  "realloc cannot fail when shrinking a block");
87  if (g->gcrunning) {
88  luaC_fullgc(L, 1); /* try to free some memory... */
89  newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
90  }
91  if (newblock == NULL)
93  }
94  lua_assert((nsize == 0) == (newblock == NULL));
95  g->GCdebt = (g->GCdebt + nsize) - realosize;
96  return newblock;
97 }
98 
l_mem GCdebt
Definition: lstate.h:115
l_noret luaG_runerror(lua_State *L, const char *fmt,...)
Definition: ldebug.cpp:585
#define luaM_reallocv(L, b, on, n, e)
Definition: lmem.h:23
lua_Alloc frealloc
Definition: lstate.h:112
#define l_noret
Definition: llimits.h:108
#define G(L)
Definition: lstate.h:178
const char * what() const
GLboolean GLboolean g
Definition: glew.h:7319
void * luaM_realloc_(lua_State *L, void *block, size_t osize, size_t nsize)
Definition: lmem.cpp:74
void * ud
Definition: lstate.h:113
GLint limit
Definition: glew.h:10112
#define MINSIZEARRAY
Definition: lmem.cpp:42
void * luaM_growaux_(lua_State *L, void *block, int *size, size_t size_elems, int limit, const char *what)
Definition: lmem.cpp:45
l_noret luaD_throw(lua_State *L, int errcode)
Definition: ldo.cpp:125
static void block(LexState *ls)
Definition: lparser.cpp:1081
#define lua_assert(c)
Definition: llimits.h:65
#define LUA_ERRMEM
Definition: lua.h:48
GLsizeiptr size
Definition: glew.h:1649
#define api_check(l, e, msg)
Definition: llimits.h:84
void luaC_fullgc(lua_State *L, int isemergency)
Definition: lgc.cpp:1188
lu_byte gcrunning
Definition: lstate.h:124
l_noret luaM_toobig(lua_State *L)
Definition: lmem.cpp:65