misc/liblua/lmem.c
author nemo
Mon, 13 May 2019 14:19:45 -0400
changeset 14925 d2f5b6b01846
parent 10017 de822cd3df3a
permissions -rw-r--r--
just to get it to build
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     1
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     2
** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 roberto Exp $
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     3
** Interface to Memory Manager
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     4
** See Copyright Notice in lua.h
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     5
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     6
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     7
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     8
#include <stddef.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     9
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    10
#define lmem_c
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    11
#define LUA_CORE
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    12
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    13
#include "lua.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    14
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    15
#include "ldebug.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    16
#include "ldo.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    17
#include "lmem.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    18
#include "lobject.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    19
#include "lstate.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    20
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    21
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    22
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    23
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    24
** About the realloc function:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    25
** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    26
** (`osize' is the old size, `nsize' is the new size)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    27
**
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    28
** Lua ensures that (ptr == NULL) iff (osize == 0).
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    29
**
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    30
** * frealloc(ud, NULL, 0, x) creates a new block of size `x'
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    31
**
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    32
** * frealloc(ud, p, x, 0) frees the block `p'
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    33
** (in this specific case, frealloc must return NULL).
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    34
** particularly, frealloc(ud, NULL, 0, 0) does nothing
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    35
** (which is equivalent to free(NULL) in ANSI C)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    36
**
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    37
** frealloc returns NULL if it cannot create or reallocate the area
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    38
** (any reallocation to an equal or smaller size cannot fail!)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    39
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    40
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    41
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    42
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 2812
diff changeset
    43
#define MINSIZEARRAY    4
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    44
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    45
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    46
void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    47
                     int limit, const char *errormsg) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    48
  void *newblock;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    49
  int newsize;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    50
  if (*size >= limit/2) {  /* cannot double it? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    51
    if (*size >= limit)  /* cannot grow even a little? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    52
      luaG_runerror(L, errormsg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    53
    newsize = limit;  /* still have at least one free place */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    54
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    55
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    56
    newsize = (*size)*2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    57
    if (newsize < MINSIZEARRAY)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    58
      newsize = MINSIZEARRAY;  /* minimum size */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    59
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    60
  newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    61
  *size = newsize;  /* update only when everything else is OK */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    62
  return newblock;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    63
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    64
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    65
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    66
void *luaM_toobig (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    67
  luaG_runerror(L, "memory allocation error: block too big");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    68
  return NULL;  /* to avoid warnings */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    69
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    70
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    71
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    72
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    73
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    74
** generic allocation routine.
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    75
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    76
void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    77
  global_State *g = G(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    78
  lua_assert((osize == 0) == (block == NULL));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    79
  block = (*g->frealloc)(g->ud, block, osize, nsize);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    80
  if (block == NULL && nsize > 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    81
    luaD_throw(L, LUA_ERRMEM);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    82
  lua_assert((nsize == 0) == (block == NULL));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    83
  g->totalbytes = (g->totalbytes - osize) + nsize;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    84
  return block;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    85
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    86