lmem.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. ** $Id: lmem.c,v 1.23 1999/12/27 17:33:22 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. ** real 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. ** Controled 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. #define HEADER (sizeof(double)) /* maximum alignment */
  32. #define MARKSIZE 16
  33. #define MARK 0x55 /* 01010101 (a nice pattern) */
  34. #define blocksize(b) ((unsigned long *)((char *)(b) - HEADER))
  35. unsigned long memdebug_numblocks = 0;
  36. unsigned long memdebug_total = 0;
  37. static void *checkblock (void *block) {
  38. unsigned long *b = blocksize(block);
  39. unsigned long size = *b;
  40. int i;
  41. for (i=0;i<MARKSIZE;i++)
  42. assert(*(((char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */
  43. memdebug_numblocks--;
  44. memdebug_total -= size;
  45. return b;
  46. }
  47. static void freeblock (void *block) {
  48. if (block) {
  49. size_t size = *blocksize(block);
  50. block = checkblock(block);
  51. memset(block, -1, size+HEADER+MARKSIZE); /* erase block */
  52. (free)(block); /* free original block */
  53. }
  54. }
  55. static void *debug_realloc (void *block, size_t size) {
  56. size_t realsize = HEADER+size+MARKSIZE;
  57. if (size == 0) {
  58. freeblock(block);
  59. return NULL;
  60. }
  61. else {
  62. char *newblock = (malloc)(realsize); /* alloc a new block */
  63. int i;
  64. if (block) {
  65. size_t oldsize = *blocksize(block);
  66. if (oldsize > size) oldsize = size;
  67. memcpy(newblock+HEADER, block, oldsize);
  68. freeblock(block); /* erase (and check) old copy */
  69. }
  70. if (newblock == NULL) return NULL;
  71. memdebug_total += size;
  72. memdebug_numblocks++;
  73. *(unsigned long *)newblock = size;
  74. for (i=0;i<MARKSIZE;i++)
  75. *(newblock+HEADER+size+i) = (char)(MARK+i);
  76. return newblock+HEADER;
  77. }
  78. }
  79. /* }====================================================================== */
  80. #endif
  81. void *luaM_growaux (lua_State *L, void *block, unsigned long nelems,
  82. int inc, int size, const char *errormsg, unsigned long limit) {
  83. unsigned long newn = nelems+inc;
  84. if (newn >= limit) lua_error(L, errormsg);
  85. if ((newn ^ nelems) <= nelems || /* still the same power-of-2 limit? */
  86. (nelems > 0 && newn < MINPOWER2)) /* or block already is MINPOWER2? */
  87. return block; /* do not need to reallocate */
  88. else /* it crossed a power-of-2 boundary; grow to next power */
  89. return luaM_realloc(L, block, luaO_power2(newn)*size);
  90. }
  91. /*
  92. ** generic allocation routine.
  93. */
  94. void *luaM_realloc (lua_State *L, void *block, unsigned long size) {
  95. size_t s = (size_t)size;
  96. if (s != size)
  97. lua_error(L, "memory allocation error: block too big");
  98. if (size == 0) {
  99. free(block); /* block may be NULL; that is OK for free */
  100. return NULL;
  101. }
  102. block = realloc(block, s);
  103. if (block == NULL)
  104. lua_error(L, memEM);
  105. return block;
  106. }