Skip to content
maxymania edited this page Mar 23, 2013 · 2 revisions
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

#include "mem_pool.h"

/* the Lua interpreter */
lua_State* L;



static void *me_alloc (void *ud, void *ptr, size_t osize, size_t nsize)
{
    MPool* pool = ud;
    void *nptr;
    if(nsize==0){
        if(ptr)
            MPool_Ex_FreeCleanup(pool,ptr);
        return NULL;
    }
    if(ptr==NULL){
        nptr=MPool_Ex_AllocJoin(pool,nsize);
        if(!nptr){
            MPool_Ex_ResetAllocCursor(pool);
            nptr=MPool_Ex_AllocJoin(pool,nsize);
        }
        return nptr;
    }else{
        if(osize>=nsize){
            MPool_Ex_Tryshrink(pool,ptr,nsize);
            return ptr;
        }
        if(MPool_Ex_Trygrow(pool,ptr,nsize)){
            return ptr;
        }
        nptr=MPool_Ex_AllocJoin(pool,nsize);
        if(!nptr){
            MPool_Ex_ResetAllocCursor(pool);
            nptr=MPool_Ex_AllocJoin(pool,nsize);
        }
        if(!nptr)return NULL;
        memcpy(nptr,ptr,osize);
        MPool_Ex_FreeCleanup(pool,ptr);
        return nptr;
    }
}


#define HEAP 1<<24

int main ( int argc, char *argv[] )
{
    MPool* pool = malloc(sizeof(MPool));
    MPool_Init(pool,malloc(HEAP),HEAP);
    /* initialize Lua */
    //L = luaL_newstate();
    L = lua_newstate(me_alloc,pool);
    /* load various Lua libraries */
    luaL_openlibs(L);
    luaopen_table(L);
    luaopen_io(L);
    luaopen_string(L);
    luaopen_math(L);
    luaL_dofile(L, "tree.lua");

    /* cleanup Lua */
    lua_close(L);

    return 0;
}

Clone this wiki locally