lmem.c 3.5 KB

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