lmem.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. ** $Id: lmem.c,v 1.43 2001/01/19 13:20:30 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. #ifdef LUA_DEBUG
  13. /*
  14. ** {======================================================================
  15. ** Controlled version for realloc.
  16. ** =======================================================================
  17. */
  18. #include <assert.h>
  19. #include <limits.h>
  20. #include <string.h>
  21. #define basicrealloc(b, os, s) debug_realloc(b, os, s)
  22. #define basicfree(b, s) debug_realloc(b, s, 0)
  23. /* ensures maximum alignment for HEADER */
  24. #define HEADER (sizeof(union L_Umaxalign))
  25. #define MARKSIZE 32
  26. #define MARK 0x55 /* 01010101 (a nice pattern) */
  27. #define blocksize(b) ((size_t *)((char *)(b) - HEADER))
  28. mem_int memdebug_numblocks = 0;
  29. mem_int memdebug_total = 0;
  30. mem_int memdebug_maxmem = 0;
  31. mem_int memdebug_memlimit = LONG_MAX;
  32. static void *checkblock (void *block) {
  33. size_t *b = blocksize(block);
  34. size_t size = *b;
  35. int i;
  36. for (i=0;i<MARKSIZE;i++)
  37. assert(*(((char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */
  38. return b;
  39. }
  40. static void freeblock (void *block) {
  41. if (block) {
  42. size_t size = *blocksize(block);
  43. block = checkblock(block);
  44. memset(block, -1, size+HEADER+MARKSIZE); /* erase block */
  45. free(block); /* free original block */
  46. memdebug_numblocks--;
  47. memdebug_total -= size;
  48. }
  49. }
  50. static void *debug_realloc (void *block, size_t oldsize, size_t size) {
  51. assert((oldsize == 0) ? block == NULL : oldsize == *blocksize(block));
  52. if (size == 0) {
  53. freeblock(block);
  54. return NULL;
  55. }
  56. else if (memdebug_total+size > memdebug_memlimit)
  57. return NULL; /* to test memory allocation errors */
  58. else {
  59. char *newblock;
  60. int i;
  61. size_t realsize = HEADER+size+MARKSIZE;
  62. if (realsize < size) return NULL; /* overflow! */
  63. newblock = (char *)malloc(realsize); /* alloc a new block */
  64. if (newblock == NULL) return NULL;
  65. if (oldsize > size) oldsize = size;
  66. if (block) {
  67. memcpy(newblock+HEADER, block, oldsize);
  68. freeblock(block); /* erase (and check) old copy */
  69. }
  70. /* initialize new part of the block with something `weird' */
  71. memset(newblock+HEADER+oldsize, -MARK, size-oldsize);
  72. memdebug_total += size;
  73. if (memdebug_total > memdebug_maxmem)
  74. memdebug_maxmem = memdebug_total;
  75. memdebug_numblocks++;
  76. *(size_t *)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. #else
  84. /* no debug */
  85. /*
  86. ** Real ISO (ANSI) systems do not need these tests;
  87. ** but some systems (Sun OS) are not that ISO...
  88. */
  89. #ifdef OLD_ANSI
  90. #define basicrealloc(b,os,s) ((b) == NULL ? malloc(s) : realloc(b, s))
  91. #define basicfree(b,s) if (b) free(b)
  92. #else
  93. #define basicrealloc(b,os,s) realloc(b,s)
  94. #define basicfree(b,s) free(b)
  95. #endif
  96. #endif
  97. void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
  98. int limit, const char *errormsg) {
  99. void *newblock;
  100. int newsize = (*size)*2;
  101. if (newsize < MINPOWER2)
  102. newsize = MINPOWER2; /* minimum size */
  103. else if (*size >= limit/2) { /* cannot double it? */
  104. if (*size < limit - MINPOWER2) /* try something smaller... */
  105. newsize = limit; /* still have at least MINPOWER2 free places */
  106. else luaD_error(L, errormsg);
  107. }
  108. newblock = luaM_realloc(L, block, (luint32)(*size)*(luint32)size_elems,
  109. (luint32)newsize*(luint32)size_elems);
  110. *size = newsize; /* update only when everything else is OK */
  111. return newblock;
  112. }
  113. /*
  114. ** generic allocation routine.
  115. */
  116. void *luaM_realloc (lua_State *L, void *block, luint32 oldsize, luint32 size) {
  117. if (size == 0) {
  118. basicfree(block, oldsize); /* block may be NULL; that is OK for free */
  119. block = NULL;
  120. }
  121. else if (size >= MAX_SIZET)
  122. luaD_error(L, "memory allocation error: block too big");
  123. else {
  124. block = basicrealloc(block, oldsize, size);
  125. if (block == NULL) {
  126. if (L)
  127. luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */
  128. else return NULL; /* error before creating state! */
  129. }
  130. }
  131. if (L && G(L)) {
  132. G(L)->nblocks -= oldsize;
  133. G(L)->nblocks += size;
  134. }
  135. return block;
  136. }