diff src/lua/lmem.c @ 11:27763b933818

raise lua sources up one level
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 11:07:39 -0600
parents src/lua/src/lmem.c@f9f4f1b99eed
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lua/lmem.c	Sat Mar 03 11:07:39 2012 -0600
     1.3 @@ -0,0 +1,86 @@
     1.4 +/*
     1.5 +** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 roberto Exp $
     1.6 +** Interface to Memory Manager
     1.7 +** See Copyright Notice in lua.h
     1.8 +*/
     1.9 +
    1.10 +
    1.11 +#include <stddef.h>
    1.12 +
    1.13 +#define lmem_c
    1.14 +#define LUA_CORE
    1.15 +
    1.16 +#include "lua.h"
    1.17 +
    1.18 +#include "ldebug.h"
    1.19 +#include "ldo.h"
    1.20 +#include "lmem.h"
    1.21 +#include "lobject.h"
    1.22 +#include "lstate.h"
    1.23 +
    1.24 +
    1.25 +
    1.26 +/*
    1.27 +** About the realloc function:
    1.28 +** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
    1.29 +** (`osize' is the old size, `nsize' is the new size)
    1.30 +**
    1.31 +** Lua ensures that (ptr == NULL) iff (osize == 0).
    1.32 +**
    1.33 +** * frealloc(ud, NULL, 0, x) creates a new block of size `x'
    1.34 +**
    1.35 +** * frealloc(ud, p, x, 0) frees the block `p'
    1.36 +** (in this specific case, frealloc must return NULL).
    1.37 +** particularly, frealloc(ud, NULL, 0, 0) does nothing
    1.38 +** (which is equivalent to free(NULL) in ANSI C)
    1.39 +**
    1.40 +** frealloc returns NULL if it cannot create or reallocate the area
    1.41 +** (any reallocation to an equal or smaller size cannot fail!)
    1.42 +*/
    1.43 +
    1.44 +
    1.45 +
    1.46 +#define MINSIZEARRAY	4
    1.47 +
    1.48 +
    1.49 +void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
    1.50 +                     int limit, const char *errormsg) {
    1.51 +  void *newblock;
    1.52 +  int newsize;
    1.53 +  if (*size >= limit/2) {  /* cannot double it? */
    1.54 +    if (*size >= limit)  /* cannot grow even a little? */
    1.55 +      luaG_runerror(L, errormsg);
    1.56 +    newsize = limit;  /* still have at least one free place */
    1.57 +  }
    1.58 +  else {
    1.59 +    newsize = (*size)*2;
    1.60 +    if (newsize < MINSIZEARRAY)
    1.61 +      newsize = MINSIZEARRAY;  /* minimum size */
    1.62 +  }
    1.63 +  newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
    1.64 +  *size = newsize;  /* update only when everything else is OK */
    1.65 +  return newblock;
    1.66 +}
    1.67 +
    1.68 +
    1.69 +void *luaM_toobig (lua_State *L) {
    1.70 +  luaG_runerror(L, "memory allocation error: block too big");
    1.71 +  return NULL;  /* to avoid warnings */
    1.72 +}
    1.73 +
    1.74 +
    1.75 +
    1.76 +/*
    1.77 +** generic allocation routine.
    1.78 +*/
    1.79 +void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
    1.80 +  global_State *g = G(L);
    1.81 +  lua_assert((osize == 0) == (block == NULL));
    1.82 +  block = (*g->frealloc)(g->ud, block, osize, nsize);
    1.83 +  if (block == NULL && nsize > 0)
    1.84 +    luaD_throw(L, LUA_ERRMEM);
    1.85 +  lua_assert((nsize == 0) == (block == NULL));
    1.86 +  g->totalbytes = (g->totalbytes - osize) + nsize;
    1.87 +  return block;
    1.88 +}
    1.89 +