6 char *rcs_luamem
= "$Id: luamem.c,v 1.16 1997/04/01 21:23:20 roberto Exp $";
18 void luaI_free (void *block
)
22 *((char *)block
) = -1; /* to catch errors */
28 void *luaI_realloc (void *oldblock
, unsigned long size
)
31 size_t s
= (size_t)size
;
33 lua_error("Allocation Error: Block too big");
34 block
= oldblock
? realloc(oldblock
, s
) : malloc(s
);
41 int luaI_growvector (void **block
, unsigned long nelems
, int size
,
42 char *errormsg
, unsigned long limit
)
46 nelems
= (nelems
== 0) ? 20 : nelems
*2;
49 *block
= luaI_realloc(*block
, nelems
*size
);
54 void* luaI_buffer (unsigned long size
)
56 static unsigned long buffsize
= 0;
57 static char* buffer
= NULL
;
59 buffer
= luaI_realloc(buffer
, buffsize
=size
);
68 # define assert(ex) {if (!(ex)){(void)fprintf(stderr, \
69 "Assertion failed: file \"%s\", line %d\n", __FILE__, __LINE__);exit(1);}}
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))
81 static unsigned long lastnumblocks
= 0;
82 static unsigned long lasttotalmem
= 0;
83 if (!inrange(numblocks
, lastnumblocks
) || !inrange(totalmem
, lasttotalmem
)
86 fprintf(stderr
,"blocks = %lu mem = %luK\n", numblocks
, totalmem
/1000);
88 lastnumblocks
= numblocks
;
89 lasttotalmem
= totalmem
;
94 void luaI_free (void *block
)
98 unsigned long *b
= (unsigned long *)block
- 1;
99 unsigned long size
= *b
;
100 assert(*(((char *)b
)+size
+sizeof(unsigned long)) == MARK
);
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");
117 unsigned long *b
= (unsigned long *)oldblock
- 1;
118 unsigned long oldsize
= *b
;
119 assert(*(((char *)b
)+oldsize
+sizeof(unsigned long)) == MARK
);
122 block
= (unsigned long *)realloc(b
, realsize
);
125 block
= (unsigned long *)malloc(realsize
);
127 lua_error("not enough memory");
131 *(((char *)block
)+size
+sizeof(unsigned long)) = MARK
;
137 int luaI_growvector (void **block
, unsigned long nelems
, int size
,
138 char *errormsg
, unsigned long limit
)
142 nelems
= (nelems
== 0) ? 20 : nelems
*2;
145 *block
= luaI_realloc(*block
, nelems
*size
);
150 void* luaI_buffer (unsigned long size
)
152 static unsigned long buffsize
= 0;
153 static char* buffer
= NULL
;
155 buffer
= luaI_realloc(buffer
, buffsize
=size
);