diff options
Diffstat (limited to 'luamem.c')
-rw-r--r-- | luamem.c | 163 |
1 files changed, 0 insertions, 163 deletions
diff --git a/luamem.c b/luamem.c deleted file mode 100644 index 812da490..00000000 --- a/luamem.c +++ /dev/null | |||
@@ -1,163 +0,0 @@ | |||
1 | /* | ||
2 | ** mem.c | ||
3 | ** TecCGraf - PUC-Rio | ||
4 | */ | ||
5 | |||
6 | char *rcs_luamem = "$Id: luamem.c,v 1.16 1997/04/01 21:23:20 roberto Exp $"; | ||
7 | |||
8 | #include <stdlib.h> | ||
9 | |||
10 | #include "luamem.h" | ||
11 | #include "lua.h" | ||
12 | |||
13 | |||
14 | #define DEBUG 0 | ||
15 | |||
16 | #if !DEBUG | ||
17 | |||
18 | static void lfree (void *block) | ||
19 | { | ||
20 | if (block) | ||
21 | { | ||
22 | *((char *)block) = -1; /* to catch errors */ | ||
23 | free(block); | ||
24 | } | ||
25 | } | ||
26 | |||
27 | |||
28 | void *luaI_realloc (void *oldblock, unsigned long size) | ||
29 | { | ||
30 | void *block; | ||
31 | size_t s = (size_t)size; | ||
32 | if (s != size) | ||
33 | lua_error("Allocation Error: Block too big"); | ||
34 | if (size == 0) { /* ANSI doen't need this, but some machines... */ | ||
35 | lfree(oldblock); | ||
36 | return NULL; | ||
37 | } | ||
38 | block = oldblock ? realloc(oldblock, s) : malloc(s); | ||
39 | if (block == NULL) | ||
40 | lua_error(memEM); | ||
41 | return block; | ||
42 | } | ||
43 | |||
44 | |||
45 | int luaI_growvector (void **block, unsigned long nelems, int size, | ||
46 | char *errormsg, unsigned long limit) | ||
47 | { | ||
48 | if (nelems >= limit) | ||
49 | lua_error(errormsg); | ||
50 | nelems = (nelems == 0) ? 20 : nelems*2; | ||
51 | if (nelems > limit) | ||
52 | nelems = limit; | ||
53 | *block = luaI_realloc(*block, nelems*size); | ||
54 | return (int)nelems; | ||
55 | } | ||
56 | |||
57 | |||
58 | void* luaI_buffer (unsigned long size) | ||
59 | { | ||
60 | static unsigned long buffsize = 0; | ||
61 | static char* buffer = NULL; | ||
62 | if (size > buffsize) | ||
63 | buffer = luaI_realloc(buffer, buffsize=size); | ||
64 | return buffer; | ||
65 | } | ||
66 | |||
67 | #else | ||
68 | /* DEBUG */ | ||
69 | |||
70 | #include <stdio.h> | ||
71 | |||
72 | # define assert(ex) {if (!(ex)){(void)fprintf(stderr, \ | ||
73 | "Assertion failed: file \"%s\", line %d\n", __FILE__, __LINE__);exit(1);}} | ||
74 | |||
75 | #define MARK 55 | ||
76 | |||
77 | static unsigned long numblocks = 0; | ||
78 | static unsigned long totalmem = 0; | ||
79 | |||
80 | |||
81 | static void message (void) | ||
82 | { | ||
83 | #define inrange(x,y) ((x) < (((y)*3)/2) && (x) > (((y)*2)/3)) | ||
84 | static int count = 0; | ||
85 | static unsigned long lastnumblocks = 0; | ||
86 | static unsigned long lasttotalmem = 0; | ||
87 | if (!inrange(numblocks, lastnumblocks) || !inrange(totalmem, lasttotalmem) | ||
88 | || count++ >= 5000) | ||
89 | { | ||
90 | fprintf(stderr,"blocks = %lu mem = %luK\n", numblocks, totalmem/1000); | ||
91 | count = 0; | ||
92 | lastnumblocks = numblocks; | ||
93 | lasttotalmem = totalmem; | ||
94 | } | ||
95 | } | ||
96 | |||
97 | |||
98 | void luaI_free (void *block) | ||
99 | { | ||
100 | if (block) | ||
101 | { | ||
102 | unsigned long *b = (unsigned long *)block - 1; | ||
103 | unsigned long size = *b; | ||
104 | assert(*(((char *)b)+size+sizeof(unsigned long)) == MARK); | ||
105 | numblocks--; | ||
106 | totalmem -= size; | ||
107 | free(b); | ||
108 | message(); | ||
109 | } | ||
110 | } | ||
111 | |||
112 | |||
113 | void *luaI_realloc (void *oldblock, unsigned long size) | ||
114 | { | ||
115 | unsigned long *block; | ||
116 | unsigned long realsize = sizeof(unsigned long)+size+sizeof(char); | ||
117 | if (realsize != (size_t)realsize) | ||
118 | lua_error("Allocation Error: Block too big"); | ||
119 | if (oldblock) | ||
120 | { | ||
121 | unsigned long *b = (unsigned long *)oldblock - 1; | ||
122 | unsigned long oldsize = *b; | ||
123 | assert(*(((char *)b)+oldsize+sizeof(unsigned long)) == MARK); | ||
124 | totalmem -= oldsize; | ||
125 | numblocks--; | ||
126 | block = (unsigned long *)realloc(b, realsize); | ||
127 | } | ||
128 | else | ||
129 | block = (unsigned long *)malloc(realsize); | ||
130 | if (block == NULL) | ||
131 | lua_error("not enough memory"); | ||
132 | totalmem += size; | ||
133 | numblocks++; | ||
134 | *block = size; | ||
135 | *(((char *)block)+size+sizeof(unsigned long)) = MARK; | ||
136 | message(); | ||
137 | return block+1; | ||
138 | } | ||
139 | |||
140 | |||
141 | int luaI_growvector (void **block, unsigned long nelems, int size, | ||
142 | char *errormsg, unsigned long limit) | ||
143 | { | ||
144 | if (nelems >= limit) | ||
145 | lua_error(errormsg); | ||
146 | nelems = (nelems == 0) ? 20 : nelems*2; | ||
147 | if (nelems > limit) | ||
148 | nelems = limit; | ||
149 | *block = luaI_realloc(*block, nelems*size); | ||
150 | return (int)nelems; | ||
151 | } | ||
152 | |||
153 | |||
154 | void* luaI_buffer (unsigned long size) | ||
155 | { | ||
156 | static unsigned long buffsize = 0; | ||
157 | static char* buffer = NULL; | ||
158 | if (size > buffsize) | ||
159 | buffer = luaI_realloc(buffer, buffsize=size); | ||
160 | return buffer; | ||
161 | } | ||
162 | |||
163 | #endif | ||