luamem.c 1.2 KB

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