lmem.c

Go to the documentation of this file.
00001 /*
00002 ** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 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 "lmem.h"
00018 #include "lobject.h"
00019 #include "lstate.h"
00020 
00021 
00022 
00023 /*
00024 ** About the realloc function:
00025 ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
00026 ** (`osize' is the old size, `nsize' is the new size)
00027 **
00028 ** Lua ensures that (ptr == NULL) iff (osize == 0).
00029 **
00030 ** * frealloc(ud, NULL, 0, x) creates a new block of size `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 *errormsg) {
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_1(L, errormsg);
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 void *luaM_toobig (lua_State *L) {
00067   luaG_runerror_1(L, "memory allocation error: block too big");
00068   return NULL;  /* to avoid warnings */
00069 }
00070 
00071 
00072 
00073 /*
00074 ** generic allocation routine.
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 }
00086 

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:54 2011 by Doxygen 1.6.1