Imported from ../lua-3.0.tar.gz.
[lua.git] / src / luamem.c
blobe4cdd6d9a77c11d34c90692ad6c3ad1a5a2117a0
1 /*
2 ** mem.c
3 ** TecCGraf - PUC-Rio
4 */
6 char *rcs_luamem = "$Id: luamem.c,v 1.16 1997/04/01 21:23:20 roberto Exp $";
8 #include <stdlib.h>
10 #include "luamem.h"
11 #include "lua.h"
14 #define DEBUG 0
16 #if !DEBUG
18 void luaI_free (void *block)
20 if (block)
22 *((char *)block) = -1; /* to catch errors */
23 free(block);
28 void *luaI_realloc (void *oldblock, unsigned long size)
30 void *block;
31 size_t s = (size_t)size;
32 if (s != size)
33 lua_error("Allocation Error: Block too big");
34 block = oldblock ? realloc(oldblock, s) : malloc(s);
35 if (block == NULL)
36 lua_error(memEM);
37 return block;
41 int luaI_growvector (void **block, unsigned long nelems, int size,
42 char *errormsg, unsigned long limit)
44 if (nelems >= limit)
45 lua_error(errormsg);
46 nelems = (nelems == 0) ? 20 : nelems*2;
47 if (nelems > limit)
48 nelems = limit;
49 *block = luaI_realloc(*block, nelems*size);
50 return (int)nelems;
54 void* luaI_buffer (unsigned long size)
56 static unsigned long buffsize = 0;
57 static char* buffer = NULL;
58 if (size > buffsize)
59 buffer = luaI_realloc(buffer, buffsize=size);
60 return buffer;
63 #else
64 /* DEBUG */
66 #include <stdio.h>
68 # define assert(ex) {if (!(ex)){(void)fprintf(stderr, \
69 "Assertion failed: file \"%s\", line %d\n", __FILE__, __LINE__);exit(1);}}
71 #define MARK 55
73 static unsigned long numblocks = 0;
74 static unsigned long totalmem = 0;
77 static void message (void)
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)
86 fprintf(stderr,"blocks = %lu mem = %luK\n", numblocks, totalmem/1000);
87 count = 0;
88 lastnumblocks = numblocks;
89 lasttotalmem = totalmem;
94 void luaI_free (void *block)
96 if (block)
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();
109 void *luaI_realloc (void *oldblock, unsigned long size)
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)
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);
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;
137 int luaI_growvector (void **block, unsigned long nelems, int size,
138 char *errormsg, unsigned long limit)
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;
150 void* luaI_buffer (unsigned long size)
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;
159 #endif