luamem.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. ** mem.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_luamem = "$Id: luamem.c,v 1.15 1997/03/31 14:17:09 roberto Exp roberto $";
  6. #include <stdlib.h>
  7. #include "luamem.h"
  8. #include "lua.h"
  9. #define DEBUG 0
  10. #if !DEBUG
  11. void luaI_free (void *block)
  12. {
  13. if (block)
  14. {
  15. *((char *)block) = -1; /* to catch errors */
  16. free(block);
  17. }
  18. }
  19. void *luaI_realloc (void *oldblock, unsigned long size)
  20. {
  21. void *block;
  22. size_t s = (size_t)size;
  23. if (s != size)
  24. lua_error("Allocation Error: Block too big");
  25. block = oldblock ? realloc(oldblock, s) : malloc(s);
  26. if (block == NULL)
  27. lua_error(memEM);
  28. return block;
  29. }
  30. int luaI_growvector (void **block, unsigned long nelems, int size,
  31. char *errormsg, unsigned long limit)
  32. {
  33. if (nelems >= limit)
  34. lua_error(errormsg);
  35. nelems = (nelems == 0) ? 20 : nelems*2;
  36. if (nelems > limit)
  37. nelems = limit;
  38. *block = luaI_realloc(*block, nelems*size);
  39. return (int)nelems;
  40. }
  41. void* luaI_buffer (unsigned long size)
  42. {
  43. static unsigned long buffsize = 0;
  44. static char* buffer = NULL;
  45. if (size > buffsize)
  46. buffer = luaI_realloc(buffer, buffsize=size);
  47. return buffer;
  48. }
  49. #else
  50. /* DEBUG */
  51. #include <stdio.h>
  52. # define assert(ex) {if (!(ex)){(void)fprintf(stderr, \
  53. "Assertion failed: file \"%s\", line %d\n", __FILE__, __LINE__);exit(1);}}
  54. #define MARK 55
  55. static unsigned long numblocks = 0;
  56. static unsigned long totalmem = 0;
  57. static void message (void)
  58. {
  59. #define inrange(x,y) ((x) < (((y)*3)/2) && (x) > (((y)*2)/3))
  60. static int count = 0;
  61. static unsigned long lastnumblocks = 0;
  62. static unsigned long lasttotalmem = 0;
  63. if (!inrange(numblocks, lastnumblocks) || !inrange(totalmem, lasttotalmem)
  64. || count++ >= 5000)
  65. {
  66. fprintf(stderr,"blocks = %lu mem = %luK\n", numblocks, totalmem/1000);
  67. count = 0;
  68. lastnumblocks = numblocks;
  69. lasttotalmem = totalmem;
  70. }
  71. }
  72. void luaI_free (void *block)
  73. {
  74. if (block)
  75. {
  76. unsigned long *b = (unsigned long *)block - 1;
  77. unsigned long size = *b;
  78. assert(*(((char *)b)+size+sizeof(unsigned long)) == MARK);
  79. numblocks--;
  80. totalmem -= size;
  81. free(b);
  82. message();
  83. }
  84. }
  85. void *luaI_realloc (void *oldblock, unsigned long size)
  86. {
  87. unsigned long *block;
  88. unsigned long realsize = sizeof(unsigned long)+size+sizeof(char);
  89. if (realsize != (size_t)realsize)
  90. lua_error("Allocation Error: Block too big");
  91. if (oldblock)
  92. {
  93. unsigned long *b = (unsigned long *)oldblock - 1;
  94. unsigned long oldsize = *b;
  95. assert(*(((char *)b)+oldsize+sizeof(unsigned long)) == MARK);
  96. totalmem -= oldsize;
  97. numblocks--;
  98. block = (unsigned long *)realloc(b, realsize);
  99. }
  100. else
  101. block = (unsigned long *)malloc(realsize);
  102. if (block == NULL)
  103. lua_error("not enough memory");
  104. totalmem += size;
  105. numblocks++;
  106. *block = size;
  107. *(((char *)block)+size+sizeof(unsigned long)) = MARK;
  108. message();
  109. return block+1;
  110. }
  111. int luaI_growvector (void **block, unsigned long nelems, int size,
  112. char *errormsg, unsigned long limit)
  113. {
  114. if (nelems >= limit)
  115. lua_error(errormsg);
  116. nelems = (nelems == 0) ? 20 : nelems*2;
  117. if (nelems > limit)
  118. nelems = limit;
  119. *block = luaI_realloc(*block, nelems*size);
  120. return (int)nelems;
  121. }
  122. void* luaI_buffer (unsigned long size)
  123. {
  124. static unsigned long buffsize = 0;
  125. static char* buffer = NULL;
  126. if (size > buffsize)
  127. buffer = luaI_realloc(buffer, buffsize=size);
  128. return buffer;
  129. }
  130. #endif