Browse Source

Interface to Memory Manager

v5-2
Roberto Ierusalimschy 28 years ago
parent
commit
dadba4d6ed
  1. 137
      lmem.c
  2. 45
      lmem.h
  3. 163
      luamem.c
  4. 40
      luamem.h

137
lmem.c

@ -0,0 +1,137 @@
/*
** $Id: $
** Interface to Memory Manager
** See Copyright Notice in lua.h
*/
#include <stdlib.h>
#include "lmem.h"
#include "lua.h"
int luaM_growaux (void **block, unsigned long nelems, int size,
char *errormsg, unsigned long limit)
{
if (nelems >= limit)
lua_error(errormsg);
nelems = (nelems == 0) ? 32 : nelems*2;
if (nelems > limit)
nelems = limit;
*block = luaM_realloc(*block, nelems*size);
return (int)nelems;
}
static unsigned long Mbuffsize = 0;
static char *Mbuffer = NULL;
void *luaM_buffer (unsigned long size)
{
if (size > Mbuffsize) {
Mbuffsize = size;
Mbuffer = luaM_realloc(Mbuffer, Mbuffsize);
}
return Mbuffer;
}
void luaM_clearbuffer (void)
{
Mbuffsize /= 2;
Mbuffer = luaM_realloc(Mbuffer, Mbuffsize);
}
#ifndef DEBUG
/*
** generic allocation routine.
** real ANSI systems do not need some of these tests,
** since realloc(NULL, s)==malloc(s) and realloc(b, 0)==free(b).
** But some systems (e.g. Sun OS) are not that ANSI...
*/
void *luaM_realloc (void *block, unsigned long size)
{
size_t s = (size_t)size;
if (s != size)
lua_error("Allocation Error: Block too big");
if (size == 0) {
if (block) {
free(block);
}
return NULL;
}
block = block ? realloc(block, s) : malloc(s);
if (block == NULL)
lua_error(memEM);
return block;
}
#else
/* DEBUG */
#include <assert.h>
#include <string.h>
#define MARK 55
static unsigned long numblocks = 0;
static unsigned long totalmem = 0;
void luaM_query (void)
{
lua_pushnumber(totalmem);
lua_pushnumber(numblocks);
}
static void *checkblock (void *block)
{
unsigned long *b = (unsigned long *)block - 1;
unsigned long size = *b;
assert(*(((char *)b)+size+sizeof(unsigned long)) == MARK);
numblocks--;
totalmem -= size;
return b;
}
void *luaM_realloc (void *block, unsigned long size)
{
unsigned long realsize = sizeof(unsigned long)+size+sizeof(char);
if (realsize != (size_t)realsize)
lua_error("Allocation Error: Block too big");
if (size == 0) { /* ANSI doen't need this, but some machines... */
if (block) {
memset(block, -1, *((unsigned long *)block-1)); /* erase block */
block = checkblock(block);
free(block);
}
return NULL;
}
if (block) {
block = checkblock(block);
block = (unsigned long *)realloc(block, realsize);
}
else
block = (unsigned long *)malloc(realsize);
if (block == NULL)
lua_error(memEM);
totalmem += size;
numblocks++;
*(unsigned long *)block = size;
*(((char *)block)+size+sizeof(unsigned long)) = MARK;
return (unsigned long *)block+1;
}
#endif

45
lmem.h

@ -0,0 +1,45 @@
/*
** $Id: $
** Interface to Memory Manager
** See Copyright Notice in lua.h
*/
#ifndef lmem_h
#define lmem_h
#ifndef NULL
#define NULL 0
#endif
/* memory error messages */
#define codeEM "code size overflow"
#define symbolEM "symbol table overflow"
#define constantEM "constant table overflow"
#define stackEM "stack size overflow"
#define lexEM "lex buffer overflow"
#define refEM "reference table overflow"
#define tableEM "table overflow"
#define memEM "not enough memory"
void *luaM_buffer (unsigned long size);
void luaM_clearbuffer (void);
void *luaM_realloc (void *oldblock, unsigned long size);
int luaM_growaux (void **block, unsigned long nelems, int size,
char *errormsg, unsigned long limit);
#define luaM_free(b) luaM_realloc((b), 0)
#define luaM_malloc(t) luaM_realloc(NULL, (t))
#define luaM_new(t) ((t *)luaM_malloc(sizeof(t)))
#define luaM_newvector(n,t) ((t *)luaM_malloc((n)*sizeof(t)))
#define luaM_growvector(old,n,t,e,l) \
(luaM_growaux((void**)old,n,sizeof(t),e,l))
#define luaM_reallocvector(v,n,t) ((t *)luaM_realloc(v,(n)*sizeof(t)))
void luaM_query (void); /* only ifdef DEBUG */
#endif

163
luamem.c

@ -1,163 +0,0 @@
/*
** mem.c
** TecCGraf - PUC-Rio
*/
char *rcs_luamem = "$Id: luamem.c,v 1.16 1997/04/01 21:23:20 roberto Exp $";
#include <stdlib.h>
#include "luamem.h"
#include "lua.h"
#define DEBUG 0
#if !DEBUG
static void lfree (void *block)
{
if (block)
{
*((char *)block) = -1; /* to catch errors */
free(block);
}
}
void *luaI_realloc (void *oldblock, unsigned long size)
{
void *block;
size_t s = (size_t)size;
if (s != size)
lua_error("Allocation Error: Block too big");
if (size == 0) { /* ANSI doen't need this, but some machines... */
lfree(oldblock);
return NULL;
}
block = oldblock ? realloc(oldblock, s) : malloc(s);
if (block == NULL)
lua_error(memEM);
return block;
}
int luaI_growvector (void **block, unsigned long nelems, int size,
char *errormsg, unsigned long limit)
{
if (nelems >= limit)
lua_error(errormsg);
nelems = (nelems == 0) ? 20 : nelems*2;
if (nelems > limit)
nelems = limit;
*block = luaI_realloc(*block, nelems*size);
return (int)nelems;
}
void* luaI_buffer (unsigned long size)
{
static unsigned long buffsize = 0;
static char* buffer = NULL;
if (size > buffsize)
buffer = luaI_realloc(buffer, buffsize=size);
return buffer;
}
#else
/* DEBUG */
#include <stdio.h>
# define assert(ex) {if (!(ex)){(void)fprintf(stderr, \
"Assertion failed: file \"%s\", line %d\n", __FILE__, __LINE__);exit(1);}}
#define MARK 55
static unsigned long numblocks = 0;
static unsigned long totalmem = 0;
static void message (void)
{
#define inrange(x,y) ((x) < (((y)*3)/2) && (x) > (((y)*2)/3))
static int count = 0;
static unsigned long lastnumblocks = 0;
static unsigned long lasttotalmem = 0;
if (!inrange(numblocks, lastnumblocks) || !inrange(totalmem, lasttotalmem)
|| count++ >= 5000)
{
fprintf(stderr,"blocks = %lu mem = %luK\n", numblocks, totalmem/1000);
count = 0;
lastnumblocks = numblocks;
lasttotalmem = totalmem;
}
}
void luaI_free (void *block)
{
if (block)
{
unsigned long *b = (unsigned long *)block - 1;
unsigned long size = *b;
assert(*(((char *)b)+size+sizeof(unsigned long)) == MARK);
numblocks--;
totalmem -= size;
free(b);
message();
}
}
void *luaI_realloc (void *oldblock, unsigned long size)
{
unsigned long *block;
unsigned long realsize = sizeof(unsigned long)+size+sizeof(char);
if (realsize != (size_t)realsize)
lua_error("Allocation Error: Block too big");
if (oldblock)
{
unsigned long *b = (unsigned long *)oldblock - 1;
unsigned long oldsize = *b;
assert(*(((char *)b)+oldsize+sizeof(unsigned long)) == MARK);
totalmem -= oldsize;
numblocks--;
block = (unsigned long *)realloc(b, realsize);
}
else
block = (unsigned long *)malloc(realsize);
if (block == NULL)
lua_error("not enough memory");
totalmem += size;
numblocks++;
*block = size;
*(((char *)block)+size+sizeof(unsigned long)) = MARK;
message();
return block+1;
}
int luaI_growvector (void **block, unsigned long nelems, int size,
char *errormsg, unsigned long limit)
{
if (nelems >= limit)
lua_error(errormsg);
nelems = (nelems == 0) ? 20 : nelems*2;
if (nelems > limit)
nelems = limit;
*block = luaI_realloc(*block, nelems*size);
return (int)nelems;
}
void* luaI_buffer (unsigned long size)
{
static unsigned long buffsize = 0;
static char* buffer = NULL;
if (size > buffsize)
buffer = luaI_realloc(buffer, buffsize=size);
return buffer;
}
#endif

40
luamem.h

@ -1,40 +0,0 @@
/*
** mem.c
** memory manager for lua
** $Id: luamem.h,v 1.10 1997/07/29 20:38:45 roberto Exp roberto $
*/
#ifndef luamem_h
#define luamem_h
#ifndef NULL
#define NULL 0
#endif
/* memory error messages */
#define codeEM "code size overflow"
#define symbolEM "symbol table overflow"
#define constantEM "constant table overflow"
#define stackEM "stack size overflow"
#define lexEM "lex buffer overflow"
#define refEM "reference table overflow"
#define tableEM "table overflow"
#define memEM "not enough memory"
void *luaI_realloc (void *oldblock, unsigned long size);
void *luaI_buffer (unsigned long size);
int luaI_growvector (void **block, unsigned long nelems, int size,
char *errormsg, unsigned long limit);
#define luaI_free(b) luaI_realloc((b), 0)
#define luaI_malloc(s) luaI_realloc(NULL, (s))
#define new(s) ((s *)luaI_malloc(sizeof(s)))
#define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s)))
#define growvector(old,n,s,e,l) \
(luaI_growvector((void**)old,n,sizeof(s),e,l))
#define shrinkvector(v,n,t) ((t *)luaI_realloc(v,(n)*sizeof(t)))
#endif
Loading…
Cancel
Save