aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-03-03 16:55:38 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-03-03 16:55:38 -0300
commita8a15ff1bdcb7526625b717c64c1d286f0625dd2 (patch)
tree2622f01af1fef682cd92815f4a401718b2c18c3d
parent131d66efd2dc26b05bcb2bdaf67f5175f3eda1aa (diff)
downloadlua-a8a15ff1bdcb7526625b717c64c1d286f0625dd2.tar.gz
lua-a8a15ff1bdcb7526625b717c64c1d286f0625dd2.tar.bz2
lua-a8a15ff1bdcb7526625b717c64c1d286f0625dd2.zip
"mem.c.debug" incorporated (with "#if").
-rw-r--r--luamem.c103
1 files changed, 102 insertions, 1 deletions
diff --git a/luamem.c b/luamem.c
index db67fb87..be08b608 100644
--- a/luamem.c
+++ b/luamem.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_mem = "$Id: mem.c,v 1.12 1996/05/06 16:59:00 roberto Exp roberto $"; 6char *rcs_mem = "$Id: mem.c,v 1.14 1997/02/25 11:12:28 roberto Exp roberto $";
7 7
8#include <stdlib.h> 8#include <stdlib.h>
9 9
@@ -11,6 +11,10 @@ char *rcs_mem = "$Id: mem.c,v 1.12 1996/05/06 16:59:00 roberto Exp roberto $";
11#include "lua.h" 11#include "lua.h"
12 12
13 13
14#define DEBUG 0
15
16#if !DEBUG
17
14void luaI_free (void *block) 18void luaI_free (void *block)
15{ 19{
16 if (block) 20 if (block)
@@ -56,3 +60,100 @@ void* luaI_buffer (unsigned long size)
56 return buffer; 60 return buffer;
57} 61}
58 62
63#else
64/* DEBUG */
65
66#include <stdio.h>
67
68# define assert(ex) {if (!(ex)){(void)fprintf(stderr, \
69 "Assertion failed: file \"%s\", line %d\n", __FILE__, __LINE__);exit(1);}}
70
71#define MARK 55
72
73static unsigned long numblocks = 0;
74static unsigned long totalmem = 0;
75
76
77static void message (void)
78{
79#define inrange(x,y) ((x) < (((y)*3)/2) && (x) > (((y)*2)/3))
80 static int count = 0;
81 static unsigned long lastnumblocks = 0;
82 static unsigned long lasttotalmem = 0;
83 if (!inrange(numblocks, lastnumblocks) || !inrange(totalmem, lasttotalmem)
84 || count++ >= 5000)
85 {
86 fprintf(stderr,"blocks = %lu mem = %luK\n", numblocks, totalmem/1000);
87 count = 0;
88 lastnumblocks = numblocks;
89 lasttotalmem = totalmem;
90 }
91}
92
93
94void luaI_free (void *block)
95{
96 if (block)
97 {
98 unsigned long *b = (unsigned long *)block - 1;
99 unsigned long size = *b;
100 assert(*(((char *)b)+size+sizeof(unsigned long)) == MARK);
101 numblocks--;
102 totalmem -= size;
103 free(b);
104 message();
105 }
106}
107
108
109void *luaI_realloc (void *oldblock, unsigned long size)
110{
111 unsigned long *block;
112 unsigned long realsize = sizeof(unsigned long)+size+sizeof(char);
113 if (realsize != (size_t)realsize)
114 lua_error("Allocation Error: Block too big");
115 if (oldblock)
116 {
117 unsigned long *b = (unsigned long *)oldblock - 1;
118 unsigned long oldsize = *b;
119 assert(*(((char *)b)+oldsize+sizeof(unsigned long)) == MARK);
120 totalmem -= oldsize;
121 numblocks--;
122 block = (unsigned long *)realloc(b, realsize);
123 }
124 else
125 block = (unsigned long *)malloc(realsize);
126 if (block == NULL)
127 lua_error("not enough memory");
128 totalmem += size;
129 numblocks++;
130 *block = size;
131 *(((char *)block)+size+sizeof(unsigned long)) = MARK;
132 message();
133 return block+1;
134}
135
136
137int luaI_growvector (void **block, unsigned long nelems, int size,
138 char *errormsg, unsigned long limit)
139{
140 if (nelems >= limit)
141 lua_error(errormsg);
142 nelems = (nelems == 0) ? 20 : nelems*2;
143 if (nelems > limit)
144 nelems = limit;
145 *block = luaI_realloc(*block, nelems*size);
146 return (int)nelems;
147}
148
149
150void* luaI_buffer (unsigned long size)
151{
152 static unsigned long buffsize = 0;
153 static char* buffer = NULL;
154 if (size > buffsize)
155 buffer = luaI_realloc(buffer, buffsize=size);
156 return buffer;
157}
158
159#endif