lua/lmem.c

Go to the documentation of this file.
00001 /*
00002 ** $Id: lmem.c,v 1.83 2011/11/30 12:42:49 roberto Exp $
00003 ** Interface to Memory Manager
00004 ** See Copyright Notice in lua.h
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 ** About the realloc function:
00026 ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
00027 ** (`osize' is the old size, `nsize' is the new size)
00028 **
00029 ** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no
00030 ** matter 'x').
00031 **
00032 ** * frealloc(ud, p, x, 0) frees the block `p'
00033 ** (in this specific case, frealloc must return NULL);
00034 ** particularly, frealloc(ud, NULL, 0, 0) does nothing
00035 ** (which is equivalent to free(NULL) in ANSI C)
00036 **
00037 ** frealloc returns NULL if it cannot create or reallocate the area
00038 ** (any reallocation to an equal or smaller size cannot fail!)
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) {  /* cannot double it? */
00051     if (*size >= limit)  /* cannot grow even a little? */
00052       luaG_runerror(L, "too many %s (limit is %d)", what, limit);
00053     newsize = limit;  /* still have at least one free place */
00054   }
00055   else {
00056     newsize = (*size)*2;
00057     if (newsize < MINSIZEARRAY)
00058       newsize = MINSIZEARRAY;  /* minimum size */
00059   }
00060   newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
00061   *size = newsize;  /* update only when everything else is OK */
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 ** generic allocation routine.
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);  /* force a GC whenever possible */
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);  /* try to free some memory... */
00090       newblock = (*g->frealloc)(g->ud, block, osize, nsize);  /* try again */
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   { /* auxiliary patch to monitor garbage collection.
00099     ** To plot, gnuplot with following command:
00100     ** plot TRACEMEM using 1:2 with lines, TRACEMEM using 1:3 with lines
00101     */
00102     static unsigned long total = 0;  /* our "time" */
00103     static FILE *f = NULL;  /* output file */
00104     total++;  /* "time" always grows */
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 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:04 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs