lmem.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. ** $Id: lmem.c,v 1.18 1999/08/16 20:52:00 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. const 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. unsigned long *b = blocksize(block);
  62. unsigned long size = *b;
  63. int i;
  64. for (i=0;i<MARKSIZE;i++)
  65. LUA_ASSERT(*(((char *)b)+HEADER+size+i) == MARK+i, "corrupted block");
  66. numblocks--;
  67. totalmem -= size;
  68. return b;
  69. }
  70. static void freeblock (void *block) {
  71. if (block) {
  72. memset(block, -1, *blocksize(block)); /* erase block */
  73. block = checkblock(block);
  74. free(block);
  75. }
  76. }
  77. void *luaM_realloc (void *block, unsigned long size) {
  78. unsigned long realsize = HEADER+size+MARKSIZE;
  79. if (realsize != (size_t)realsize)
  80. lua_error("memory allocation error: block too big");
  81. if (size == 0) {
  82. freeblock(block);
  83. return NULL;
  84. }
  85. else {
  86. char *newblock = malloc(realsize);
  87. int i;
  88. if (block) {
  89. unsigned long oldsize = *blocksize(block);
  90. if (oldsize > size) oldsize = size;
  91. memcpy(newblock+HEADER, block, oldsize);
  92. freeblock(block); /* erase (and check) old copy */
  93. }
  94. if (newblock == NULL)
  95. lua_error(memEM);
  96. totalmem += size;
  97. numblocks++;
  98. *(unsigned long *)newblock = size;
  99. for (i=0;i<MARKSIZE;i++)
  100. *(newblock+HEADER+size+i) = (char)(MARK+i);
  101. return newblock+HEADER;
  102. }
  103. }
  104. #endif