lmem.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. ** $Id: lmem.c,v 1.35 2000/08/04 19:38:35 roberto Exp roberto $
  3. ** Interface to Memory Manager
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include "lua.h"
  8. #include "ldo.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 <limits.h>
  28. #include <string.h>
  29. #undef realloc
  30. #undef malloc
  31. #undef free
  32. #define realloc(b, s) debug_realloc(b, s)
  33. #define malloc(b) debug_realloc(NULL, 0)
  34. #define free(b) debug_realloc(b, 0)
  35. /* ensures maximum alignment for HEADER */
  36. union L_U { double d; char *s; long l; };
  37. #define HEADER (sizeof(union L_U))
  38. #define MARKSIZE 16
  39. #define MARK 0x55 /* 01010101 (a nice pattern) */
  40. #define blocksize(b) ((unsigned long *)((char *)(b) - HEADER))
  41. unsigned long memdebug_numblocks = 0;
  42. unsigned long memdebug_total = 0;
  43. unsigned long memdebug_maxmem = 0;
  44. unsigned long memdebug_memlimit = LONG_MAX;
  45. static void *checkblock (void *block) {
  46. unsigned long *b = blocksize(block);
  47. unsigned long size = *b;
  48. int i;
  49. for (i=0;i<MARKSIZE;i++)
  50. assert(*(((char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */
  51. memdebug_numblocks--;
  52. memdebug_total -= size;
  53. return b;
  54. }
  55. static void freeblock (void *block) {
  56. if (block) {
  57. size_t size = *blocksize(block);
  58. block = checkblock(block);
  59. memset(block, -1, size+HEADER+MARKSIZE); /* erase block */
  60. (free)(block); /* free original block */
  61. }
  62. }
  63. static void *debug_realloc (void *block, size_t size) {
  64. if (size == 0) {
  65. freeblock(block);
  66. return NULL;
  67. }
  68. else if (memdebug_total+size > memdebug_memlimit)
  69. return NULL; /* to test memory allocation errors */
  70. else {
  71. size_t realsize = HEADER+size+MARKSIZE;
  72. char *newblock = (char *)(malloc)(realsize); /* alloc a new block */
  73. int i;
  74. if (realsize < size) return NULL; /* overflow! */
  75. if (newblock == NULL) return NULL;
  76. if (block) {
  77. size_t oldsize = *blocksize(block);
  78. if (oldsize > size) oldsize = size;
  79. memcpy(newblock+HEADER, block, oldsize);
  80. freeblock(block); /* erase (and check) old copy */
  81. }
  82. memdebug_total += size;
  83. if (memdebug_total > memdebug_maxmem) memdebug_maxmem = memdebug_total;
  84. memdebug_numblocks++;
  85. *(unsigned long *)newblock = size;
  86. for (i=0;i<MARKSIZE;i++)
  87. *(newblock+HEADER+size+i) = (char)(MARK+i);
  88. return newblock+HEADER;
  89. }
  90. }
  91. /* }====================================================================== */
  92. #endif
  93. void *luaM_growaux (lua_State *L, void *block, size_t nelems,
  94. int inc, size_t size, const char *errormsg, size_t limit) {
  95. size_t newn = nelems+inc;
  96. if (nelems >= limit-inc) lua_error(L, errormsg);
  97. if ((newn ^ nelems) <= nelems || /* still the same power-of-2 limit? */
  98. (nelems > 0 && newn < MINPOWER2)) /* or block already is MINPOWER2? */
  99. return block; /* do not need to reallocate */
  100. else /* it crossed a power-of-2 boundary; grow to next power */
  101. return luaM_realloc(L, block, luaO_power2(newn)*size);
  102. }
  103. /*
  104. ** generic allocation routine.
  105. */
  106. void *luaM_realloc (lua_State *L, void *block, lint32 size) {
  107. if (size == 0) {
  108. free(block); /* block may be NULL; that is OK for free */
  109. return NULL;
  110. }
  111. else if (size >= MAX_SIZET)
  112. lua_error(L, "memory allocation error: block too big");
  113. block = realloc(block, size);
  114. if (block == NULL) {
  115. if (L)
  116. luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */
  117. else return NULL; /* error before creating state! */
  118. }
  119. return block;
  120. }