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 "lgc.h"
00018 #include "lmem.h"
00019 #include "lobject.h"
00020 #include "lstate.h"
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 *what) {
00048 void *newblock;
00049 int newsize;
00050 if (*size >= limit/2) {
00051 if (*size >= limit)
00052 luaG_runerror(L, "too many %s (limit is %d)", what, limit);
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 l_noret luaM_toobig (lua_State *L) {
00067 luaG_runerror(L, "memory allocation error: block too big");
00068 }
00069
00070
00071
00072
00073
00074
00075 void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
00076 void *newblock;
00077 global_State *g = G(L);
00078 size_t realosize = (block) ? osize : 0;
00079 lua_assert((realosize == 0) == (block == NULL));
00080 #if defined(HARDMEMTESTS)
00081 if (nsize > realosize && g->gcrunning)
00082 luaC_fullgc(L, 1);
00083 #endif
00084 newblock = (*g->frealloc)(g->ud, block, osize, nsize);
00085 if (newblock == NULL && nsize > 0) {
00086 api_check(L, nsize > realosize,
00087 "realloc cannot fail when shrinking a block");
00088 if (g->gcrunning) {
00089 luaC_fullgc(L, 1);
00090 newblock = (*g->frealloc)(g->ud, block, osize, nsize);
00091 }
00092 if (newblock == NULL)
00093 luaD_throw(L, LUA_ERRMEM);
00094 }
00095 lua_assert((nsize == 0) == (newblock == NULL));
00096 g->GCdebt = (g->GCdebt + nsize) - realosize;
00097 #if defined(TRACEMEM)
00098 {
00099
00100
00101
00102 static unsigned long total = 0;
00103 static FILE *f = NULL;
00104 total++;
00105 if ((total % 200) == 0) {
00106 if (f == NULL) f = fopen(TRACEMEM, "w");
00107 fprintf(f, "%lu %u %d %d\n", total,
00108 gettotalbytes(g), g->GCdebt, g->gcstate * 10000);
00109 }
00110 }
00111 #endif
00112
00113 return newblock;
00114 }
00115