aboutsummaryrefslogtreecommitdiff
path: root/lmem.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-09-16 16:25:59 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-09-16 16:25:59 -0300
commitdadba4d6ed9f7432185816abcbb788125aa991ff (patch)
tree343ec90be7f956520f7e8ac9bb6d8eb73f01138d /lmem.c
parentd600a6b5b358c28d482b01f10bfa3292b17f5d12 (diff)
downloadlua-dadba4d6ed9f7432185816abcbb788125aa991ff.tar.gz
lua-dadba4d6ed9f7432185816abcbb788125aa991ff.tar.bz2
lua-dadba4d6ed9f7432185816abcbb788125aa991ff.zip
Interface to Memory Manager
Diffstat (limited to 'lmem.c')
-rw-r--r--lmem.c137
1 files changed, 137 insertions, 0 deletions
diff --git a/lmem.c b/lmem.c
new file mode 100644
index 00000000..8aeb9974
--- /dev/null
+++ b/lmem.c
@@ -0,0 +1,137 @@
1/*
2** $Id: $
3** Interface to Memory Manager
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdlib.h>
9
10#include "lmem.h"
11#include "lua.h"
12
13
14
15int luaM_growaux (void **block, unsigned long nelems, int size,
16 char *errormsg, unsigned long limit)
17{
18 if (nelems >= limit)
19 lua_error(errormsg);
20 nelems = (nelems == 0) ? 32 : nelems*2;
21 if (nelems > limit)
22 nelems = limit;
23 *block = luaM_realloc(*block, nelems*size);
24 return (int)nelems;
25}
26
27
28static unsigned long Mbuffsize = 0;
29static char *Mbuffer = NULL;
30
31
32void *luaM_buffer (unsigned long size)
33{
34 if (size > Mbuffsize) {
35 Mbuffsize = size;
36 Mbuffer = luaM_realloc(Mbuffer, Mbuffsize);
37 }
38 return Mbuffer;
39}
40
41
42void luaM_clearbuffer (void)
43{
44 Mbuffsize /= 2;
45 Mbuffer = luaM_realloc(Mbuffer, Mbuffsize);
46}
47
48
49#ifndef DEBUG
50
51/*
52** generic allocation routine.
53** real ANSI systems do not need some of these tests,
54** since realloc(NULL, s)==malloc(s) and realloc(b, 0)==free(b).
55** But some systems (e.g. Sun OS) are not that ANSI...
56*/
57void *luaM_realloc (void *block, unsigned long size)
58{
59 size_t s = (size_t)size;
60 if (s != size)
61 lua_error("Allocation Error: Block too big");
62 if (size == 0) {
63 if (block) {
64 free(block);
65 }
66 return NULL;
67 }
68 block = block ? realloc(block, s) : malloc(s);
69 if (block == NULL)
70 lua_error(memEM);
71 return block;
72}
73
74
75
76#else
77/* DEBUG */
78
79#include <assert.h>
80#include <string.h>
81
82
83#define MARK 55
84
85static unsigned long numblocks = 0;
86static unsigned long totalmem = 0;
87
88
89
90void luaM_query (void)
91{
92 lua_pushnumber(totalmem);
93 lua_pushnumber(numblocks);
94}
95
96
97static void *checkblock (void *block)
98{
99 unsigned long *b = (unsigned long *)block - 1;
100 unsigned long size = *b;
101 assert(*(((char *)b)+size+sizeof(unsigned long)) == MARK);
102 numblocks--;
103 totalmem -= size;
104 return b;
105}
106
107
108void *luaM_realloc (void *block, unsigned long size)
109{
110 unsigned long realsize = sizeof(unsigned long)+size+sizeof(char);
111 if (realsize != (size_t)realsize)
112 lua_error("Allocation Error: Block too big");
113 if (size == 0) { /* ANSI doen't need this, but some machines... */
114 if (block) {
115 memset(block, -1, *((unsigned long *)block-1)); /* erase block */
116 block = checkblock(block);
117 free(block);
118 }
119 return NULL;
120 }
121 if (block) {
122 block = checkblock(block);
123 block = (unsigned long *)realloc(block, realsize);
124 }
125 else
126 block = (unsigned long *)malloc(realsize);
127 if (block == NULL)
128 lua_error(memEM);
129 totalmem += size;
130 numblocks++;
131 *(unsigned long *)block = size;
132 *(((char *)block)+size+sizeof(unsigned long)) = MARK;
133 return (unsigned long *)block+1;
134}
135
136
137#endif