luamem.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. ** mem.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_mem = "$Id: mem.c,v 1.12 1996/05/06 16:59:00 roberto Exp roberto $";
  6. #include <stdlib.h>
  7. #include "mem.h"
  8. #include "lua.h"
  9. void luaI_free (void *block)
  10. {
  11. if (block)
  12. {
  13. *((int *)block) = -1; /* to catch errors */
  14. free(block);
  15. }
  16. }
  17. void *luaI_realloc (void *oldblock, unsigned long size)
  18. {
  19. void *block;
  20. size_t s = (size_t)size;
  21. if (s != size)
  22. lua_error("Allocation Error: Block too big");
  23. block = oldblock ? realloc(oldblock, s) : malloc(s);
  24. if (block == NULL)
  25. lua_error(memEM);
  26. return block;
  27. }
  28. int luaI_growvector (void **block, unsigned long nelems, int size,
  29. char *errormsg, unsigned long limit)
  30. {
  31. if (nelems >= limit)
  32. lua_error(errormsg);
  33. nelems = (nelems == 0) ? 20 : nelems*2;
  34. if (nelems > limit)
  35. nelems = limit;
  36. *block = luaI_realloc(*block, nelems*size);
  37. return (int)nelems;
  38. }
  39. void* luaI_buffer (unsigned long size)
  40. {
  41. static unsigned long buffsize = 0;
  42. static char* buffer = NULL;
  43. if (size > buffsize)
  44. buffer = luaI_realloc(buffer, buffsize=size);
  45. return buffer;
  46. }