lmem.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include <stddef.h>
00009
00010 #define lmem_c
00011 #define LUA_CORE
00012
00013 #include "lua.h"
00014
00015 #include "ldebug.h"
00016 #include "ldo.h"
00017 #include "lmem.h"
00018 #include "lobject.h"
00019 #include "lstate.h"
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #define MINSIZEARRAY 4
00044
00045
00046 void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
00047 int limit, const char *errormsg) {
00048 void *newblock;
00049 int newsize;
00050 if (*size >= limit/2) {
00051 if (*size >= limit)
00052 luaG_runerror(L, errormsg);
00053 newsize = limit;
00054 }
00055 else {
00056 newsize = (*size)*2;
00057 if (newsize < MINSIZEARRAY)
00058 newsize = MINSIZEARRAY;
00059 }
00060 newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
00061 *size = newsize;
00062 return newblock;
00063 }
00064
00065
00066 void *luaM_toobig (lua_State *L) {
00067 luaG_runerror(L, "memory allocation error: block too big");
00068 return NULL;
00069 }
00070
00071
00072
00073
00074
00075
00076 void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
00077 global_State *g = G(L);
00078 lua_assert((osize == 0) == (block == NULL));
00079 block = (*g->frealloc)(g->ud, block, osize, nsize);
00080 if (block == NULL && nsize > 0)
00081 luaD_throw(L, LUA_ERRMEM);
00082 lua_assert((nsize == 0) == (block == NULL));
00083 g->totalbytes = (g->totalbytes - osize) + nsize;
00084 return block;
00085 }