2
0

lmem.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. ** $Id: lmem.c,v 1.33 2000/06/12 13:52:05 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 "lua.h"
  9. #include "lmem.h"
  10. #include "lobject.h"
  11. #include "lstate.h"
  12. /*
  13. ** Real ISO (ANSI) systems do not need these tests;
  14. ** but some systems (Sun OS) are not that ISO...
  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. #undef realloc
  29. #undef malloc
  30. #undef free
  31. #define realloc(b, s) debug_realloc(b, s)
  32. #define malloc(b) debug_realloc(NULL, 0)
  33. #define free(b) debug_realloc(b, 0)
  34. /* ensures maximum alignment for HEADER */
  35. union L_U { double d; char *s; long l; };
  36. #define HEADER (sizeof(union L_U))
  37. #define MARKSIZE 16
  38. #define MARK 0x55 /* 01010101 (a nice pattern) */
  39. #define blocksize(b) ((unsigned long *)((char *)(b) - HEADER))
  40. unsigned long memdebug_numblocks = 0;
  41. unsigned long memdebug_total = 0;
  42. unsigned long memdebug_maxmem = 0;
  43. static void *checkblock (void *block) {
  44. unsigned long *b = blocksize(block);
  45. unsigned long size = *b;
  46. int i;
  47. for (i=0;i<MARKSIZE;i++)
  48. assert(*(((char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */
  49. memdebug_numblocks--;
  50. memdebug_total -= size;
  51. return b;
  52. }
  53. static void freeblock (void *block) {
  54. if (block) {
  55. size_t size = *blocksize(block);
  56. block = checkblock(block);
  57. memset(block, -1, size+HEADER+MARKSIZE); /* erase block */
  58. (free)(block); /* free original block */
  59. }
  60. }
  61. static void *debug_realloc (void *block, size_t size) {
  62. if (size == 0) {
  63. freeblock(block);
  64. return NULL;
  65. }
  66. else {
  67. size_t realsize = HEADER+size+MARKSIZE;
  68. char *newblock = (char *)(malloc)(realsize); /* alloc a new block */
  69. int i;
  70. if (realsize < size) return NULL; /* overflow! */
  71. if (newblock == NULL) return NULL;
  72. if (block) {
  73. size_t oldsize = *blocksize(block);
  74. if (oldsize > size) oldsize = size;
  75. memcpy(newblock+HEADER, block, oldsize);
  76. freeblock(block); /* erase (and check) old copy */
  77. }
  78. memdebug_total += size;
  79. if (memdebug_total > memdebug_maxmem) memdebug_maxmem = memdebug_total;
  80. memdebug_numblocks++;
  81. *(unsigned long *)newblock = size;
  82. for (i=0;i<MARKSIZE;i++)
  83. *(newblock+HEADER+size+i) = (char)(MARK+i);
  84. return newblock+HEADER;
  85. }
  86. }
  87. /* }====================================================================== */
  88. #endif
  89. void *luaM_growaux (lua_State *L, void *block, size_t nelems,
  90. int inc, size_t size, const char *errormsg, size_t limit) {
  91. size_t newn = nelems+inc;
  92. if (nelems >= limit-inc) lua_error(L, errormsg);
  93. if ((newn ^ nelems) <= nelems || /* still the same power-of-2 limit? */
  94. (nelems > 0 && newn < MINPOWER2)) /* or block already is MINPOWER2? */
  95. return block; /* do not need to reallocate */
  96. else /* it crossed a power-of-2 boundary; grow to next power */
  97. return luaM_realloc(L, block, luaO_power2(newn)*size);
  98. }
  99. /*
  100. ** generic allocation routine.
  101. */
  102. void *luaM_realloc (lua_State *L, void *block, lint32 size) {
  103. if (size == 0) {
  104. free(block); /* block may be NULL; that is OK for free */
  105. return NULL;
  106. }
  107. else if (size >= MAX_SIZET)
  108. lua_error(L, "memory allocation error: block too big");
  109. block = realloc(block, size);
  110. if (block == NULL)
  111. lua_error(L, memEM);
  112. return block;
  113. }