diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
commit | dadba4d6ed9f7432185816abcbb788125aa991ff (patch) | |
tree | 343ec90be7f956520f7e8ac9bb6d8eb73f01138d /lmem.c | |
parent | d600a6b5b358c28d482b01f10bfa3292b17f5d12 (diff) | |
download | lua-dadba4d6ed9f7432185816abcbb788125aa991ff.tar.gz lua-dadba4d6ed9f7432185816abcbb788125aa991ff.tar.bz2 lua-dadba4d6ed9f7432185816abcbb788125aa991ff.zip |
Interface to Memory Manager
Diffstat (limited to 'lmem.c')
-rw-r--r-- | lmem.c | 137 |
1 files changed, 137 insertions, 0 deletions
@@ -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 | |||
15 | int 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 | |||
28 | static unsigned long Mbuffsize = 0; | ||
29 | static char *Mbuffer = NULL; | ||
30 | |||
31 | |||
32 | void *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 | |||
42 | void 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 | */ | ||
57 | void *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 | |||
85 | static unsigned long numblocks = 0; | ||
86 | static unsigned long totalmem = 0; | ||
87 | |||
88 | |||
89 | |||
90 | void luaM_query (void) | ||
91 | { | ||
92 | lua_pushnumber(totalmem); | ||
93 | lua_pushnumber(numblocks); | ||
94 | } | ||
95 | |||
96 | |||
97 | static 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 | |||
108 | void *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 | ||