ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

lmem.cpp

Go to the documentation of this file.
00001 /*
00002 ** Interface to Memory Manager
00003 ** See Copyright Notice in lua.h
00004 */
00005 
00006 #define FORBIDDEN_SYMBOL_EXCEPTION_setjmp
00007 #define FORBIDDEN_SYMBOL_EXCEPTION_longjmp
00008 
00009 #include "engines/grim/lua/lmem.h"
00010 #include "engines/grim/lua/lstate.h"
00011 #include "engines/grim/lua/lua.h"
00012 
00013 namespace Grim {
00014 
00015 int32 luaM_growaux(void **block, int32 nelems, int32 size, const char *errormsg, int32 limit) {
00016     if (nelems >= limit)
00017         lua_error(errormsg);
00018     nelems = (nelems == 0) ? 32 : nelems * 2;
00019     if (nelems > limit)
00020         nelems = limit;
00021     *block = luaM_realloc(*block, nelems * size);
00022     return (int32)nelems;
00023 }
00024 
00025 #ifndef LUA_DEBUG
00026 
00027 /*
00028 ** generic allocation routine.
00029 ** real ANSI systems do not need some of these tests,
00030 ** since realloc(NULL, s)==malloc(s) and realloc(b, 0)==free(b).
00031 ** But some systems (e.g. Sun OS) are not that ANSI...
00032 */
00033 void *luaM_realloc(void *block, int32 size) {
00034     if (size == 0) {
00035         free(block);
00036         return nullptr;
00037     }
00038     block = block ? realloc(block, size) : malloc(size);
00039     if (!block)
00040         lua_error(memEM);
00041     return block;
00042 }
00043 
00044 #else
00045 /* LUA_DEBUG */
00046 
00047 #define MARK    55
00048 
00049 int32 numblocks = 0;
00050 int32 totalmem = 0;
00051 
00052 static void *checkblock(void *block) {
00053     int32 *b = (int32 *)block - 1;
00054     int32 size = *b;
00055     assert(*(((char *)b) + size + sizeof(int32)) == MARK);
00056     numblocks--;
00057     totalmem -= size;
00058     return b;
00059 }
00060 
00061 void *luaM_realloc(void *block, int32 size) {
00062     int32 realsize = HEADER + size + 1;
00063     int32 realsize = sizeof(int32) + size + sizeof(char);
00064     if (realsize != (size_t)realsize)
00065         lua_error("Allocation Error: Block too big");
00066     if (size == 0) {  // ANSI dosen't need this, but some machines...
00067         if (block) {
00068             memset(block, -1, *((int32 *)block - 1));  // erase block
00069             block = checkblock(block);
00070             free(block);
00071         }
00072         return NULL;
00073     }
00074     if (block) {
00075         block = checkblock(block);
00076         block = (int32 *)realloc(block, realsize);
00077     } else
00078         block = (int32 *)malloc(realsize);
00079     if (!block)
00080         lua_error(memEM);
00081     totalmem += size;
00082     numblocks++;
00083     *(int32 *)block = size;
00084     *(((char *)block) + size + sizeof(int32)) = MARK;
00085     return (int32 *)block+1;
00086 }
00087 
00088 #endif
00089 
00090 } // end of namespace Grim


Generated on Sat Mar 16 2019 05:01:42 for ResidualVM by doxygen 1.7.1
curved edge   curved edge