lmem.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. ** $Id: lmem.c,v 1.38 2000/10/26 12:47:05 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 realloc(b, s) debug_realloc(b, s)
  22. #define malloc(b) debug_realloc(NULL, b)
  23. #define free(b) debug_realloc(b, 0)
  24. /* ensures maximum alignment for HEADER */
  25. #define HEADER (sizeof(union L_Umaxalign))
  26. #define MARKSIZE 16
  27. #define MARK 0x55 /* 01010101 (a nice pattern) */
  28. #define blocksize(b) ((unsigned long *)((char *)(b) - HEADER))
  29. unsigned long memdebug_numblocks = 0;
  30. unsigned long memdebug_total = 0;
  31. unsigned long memdebug_maxmem = 0;
  32. unsigned long memdebug_memlimit = LONG_MAX;
  33. static void *checkblock (void *block) {
  34. unsigned long *b = blocksize(block);
  35. unsigned long size = *b;
  36. int i;
  37. for (i=0;i<MARKSIZE;i++)
  38. assert(*(((char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */
  39. memdebug_numblocks--;
  40. memdebug_total -= size;
  41. return b;
  42. }
  43. static void freeblock (void *block) {
  44. if (block) {
  45. size_t size = *blocksize(block);
  46. block = checkblock(block);
  47. memset(block, -1, size+HEADER+MARKSIZE); /* erase block */
  48. (free)(block); /* free original block */
  49. }
  50. }
  51. static void *debug_realloc (void *block, size_t size) {
  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. size_t realsize = HEADER+size+MARKSIZE;
  60. char *newblock = (char *)(malloc)(realsize); /* alloc a new block */
  61. int i;
  62. if (realsize < size) return NULL; /* overflow! */
  63. if (newblock == NULL) return NULL;
  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. memdebug_total += size;
  71. if (memdebug_total > memdebug_maxmem) memdebug_maxmem = memdebug_total;
  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. /*
  82. ** Real ISO (ANSI) systems do not need these tests;
  83. ** but some systems (Sun OS) are not that ISO...
  84. */
  85. #ifdef OLD_ANSI
  86. #define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
  87. #define free(b) if (b) (free)(b)
  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. if (L)
  112. luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */
  113. else return NULL; /* error before creating state! */
  114. }
  115. return block;
  116. }