lmem.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. ** $Id: lmem.c,v 1.16 1999/05/20 20:43:06 roberto Exp roberto $
  3. ** Interface to Memory Manager
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include "lmem.h"
  8. #include "lstate.h"
  9. #include "lua.h"
  10. /*
  11. ** real ANSI systems do not need these tests;
  12. ** but some systems (Sun OS) are not that ANSI...
  13. */
  14. #ifdef OLD_ANSI
  15. #define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
  16. #define free(b) if (b) (free)(b)
  17. #endif
  18. #define MINSIZE 8 /* minimum size for "growing" vectors */
  19. static unsigned long power2 (unsigned long n) {
  20. unsigned long p = MINSIZE;
  21. while (p<=n) p<<=1;
  22. return p;
  23. }
  24. void *luaM_growaux (void *block, unsigned long nelems, int inc, int size,
  25. char *errormsg, unsigned long limit) {
  26. unsigned long newn = nelems+inc;
  27. if (newn >= limit) lua_error(errormsg);
  28. if ((newn ^ nelems) <= nelems || /* still the same power of 2 limit? */
  29. (nelems > 0 && newn < MINSIZE)) /* or block already is MINSIZE? */
  30. return block; /* do not need to reallocate */
  31. else /* it crossed a power of 2 boundary; grow to next power */
  32. return luaM_realloc(block, power2(newn)*size);
  33. }
  34. #ifndef DEBUG
  35. /*
  36. ** generic allocation routine.
  37. */
  38. void *luaM_realloc (void *block, unsigned long size) {
  39. size_t s = (size_t)size;
  40. if (s != size)
  41. lua_error("memory allocation error: block too big");
  42. if (size == 0) {
  43. free(block); /* block may be NULL, that is OK for free */
  44. return NULL;
  45. }
  46. block = realloc(block, s);
  47. if (block == NULL)
  48. lua_error(memEM);
  49. return block;
  50. }
  51. #else
  52. /* DEBUG */
  53. #include <string.h>
  54. #define HEADER (sizeof(double))
  55. #define MARKSIZE 16
  56. #define MARK 55
  57. #define blocksize(b) ((unsigned long *)((char *)(b) - HEADER))
  58. unsigned long numblocks = 0;
  59. unsigned long totalmem = 0;
  60. static void *checkblock (void *block) {
  61. if (block == NULL)
  62. return NULL;
  63. else {
  64. unsigned long *b = blocksize(block);
  65. unsigned long size = *b;
  66. int i;
  67. for (i=0;i<MARKSIZE;i++)
  68. LUA_ASSERT(*(((char *)b)+HEADER+size+i) == MARK+i, "corrupted block");
  69. numblocks--;
  70. totalmem -= size;
  71. return b;
  72. }
  73. }
  74. static void freeblock (void *block) {
  75. if (block)
  76. memset(block, -1, *blocksize(block)); /* erase block */
  77. free(checkblock(block));
  78. }
  79. void *luaM_realloc (void *block, unsigned long size) {
  80. unsigned long realsize = HEADER+size+MARKSIZE;
  81. if (realsize != (size_t)realsize)
  82. lua_error("memory allocation error: block too big");
  83. if (size == 0) {
  84. freeblock(block);
  85. return NULL;
  86. }
  87. else {
  88. char *newblock = malloc(realsize);
  89. int i;
  90. if (block) {
  91. unsigned long oldsize = *blocksize(block);
  92. if (oldsize > size) oldsize = size;
  93. memcpy(newblock+HEADER, block, oldsize);
  94. freeblock(block); /* erase (and check) old copy */
  95. }
  96. if (newblock == NULL)
  97. lua_error(memEM);
  98. totalmem += size;
  99. numblocks++;
  100. *(unsigned long *)newblock = size;
  101. for (i=0;i<MARKSIZE;i++)
  102. *(newblock+HEADER+size+i) = MARK+i;
  103. return newblock+HEADER;
  104. }
  105. }
  106. #endif