summaryrefslogtreecommitdiff
path: root/luamem.c
blob: d040f31db6ab16a9830830d02dd8a9207d55d84d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
** mem.c
** TecCGraf - PUC-Rio
*/

char *rcs_mem = "$Id: mem.c,v 1.6 1996/01/22 14:15:13 roberto Exp roberto $";

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "mem.h"
#include "lua.h"
#include "table.h"

static void mem_error (void)
{
  Long recovered = luaI_collectgarbage();  /* try to collect garbage  */
  if (recovered)
    lua_error("not enough memory");
  else
  { /* if there is no garbage then must exit */
    fprintf(stderr, "lua error: memory overflow - unable to recover\n");
    exit(1);
  }
}

void luaI_free (void *block)
{
  *((int *)block) = -1;  /* to catch errors */
  free(block);
}


void *luaI_malloc (unsigned long size)
{
  void *block = malloc((size_t)size);
  if (block == NULL)
    mem_error();
  return block;
}


void *luaI_realloc (void *oldblock, unsigned long size)
{
  void *block = realloc(oldblock, (size_t)size);
  if (block == NULL)
    mem_error();
  return block;
}


char *luaI_strdup (char *str)
{
  char *newstr = luaI_malloc(strlen(str)+1);
  strcpy(newstr, str);
  return newstr;
}