lmem.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. ** $Id: lmem.c $
  3. ** Interface to Memory Manager
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lmem_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <stddef.h>
  10. #include "lua.h"
  11. #include "ldebug.h"
  12. #include "ldo.h"
  13. #include "lgc.h"
  14. #include "lmem.h"
  15. #include "lobject.h"
  16. #include "lstate.h"
  17. /*
  18. ** About the realloc function:
  19. ** void *frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
  20. ** ('osize' is the old size, 'nsize' is the new size)
  21. **
  22. ** - frealloc(ud, p, x, 0) frees the block 'p' and returns NULL.
  23. ** Particularly, frealloc(ud, NULL, 0, 0) does nothing,
  24. ** which is equivalent to free(NULL) in ISO C.
  25. **
  26. ** - frealloc(ud, NULL, x, s) creates a new block of size 's'
  27. ** (no matter 'x'). Returns NULL if it cannot create the new block.
  28. **
  29. ** - otherwise, frealloc(ud, b, x, y) reallocates the block 'b' from
  30. ** size 'x' to size 'y'. Returns NULL if it cannot reallocate the
  31. ** block to the new size.
  32. */
  33. /*
  34. ** Macro to call the allocation function.
  35. */
  36. #define callfrealloc(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns))
  37. /*
  38. ** When an allocation fails, it will try again after an emergency
  39. ** collection, except when it cannot run a collection. The GC should
  40. ** not be called while the state is not fully built, as the collector
  41. ** is not yet fully initialized. Also, it should not be called when
  42. ** 'gcstopem' is true, because then the interpreter is in the middle of
  43. ** a collection step.
  44. */
  45. #define cantryagain(g) (completestate(g) && !g->gcstopem)
  46. #if defined(EMERGENCYGCTESTS)
  47. /*
  48. ** First allocation will fail except when freeing a block (frees never
  49. ** fail) and when it cannot try again; this fail will trigger 'tryagain'
  50. ** and a full GC cycle at every allocation.
  51. */
  52. static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
  53. if (ns > 0 && cantryagain(g))
  54. return NULL; /* fail */
  55. else /* normal allocation */
  56. return callfrealloc(g, block, os, ns);
  57. }
  58. #else
  59. #define firsttry(g,block,os,ns) callfrealloc(g, block, os, ns)
  60. #endif
  61. /*
  62. ** {==================================================================
  63. ** Functions to allocate/deallocate arrays for the Parser
  64. ** ===================================================================
  65. */
  66. /*
  67. ** Minimum size for arrays during parsing, to avoid overhead of
  68. ** reallocating to size 1, then 2, and then 4. All these arrays
  69. ** will be reallocated to exact sizes or erased when parsing ends.
  70. */
  71. #define MINSIZEARRAY 4
  72. void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
  73. int size_elems, int limit, const char *what) {
  74. void *newblock;
  75. int size = *psize;
  76. if (nelems + 1 <= size) /* does one extra element still fit? */
  77. return block; /* nothing to be done */
  78. if (size >= limit / 2) { /* cannot double it? */
  79. if (l_unlikely(size >= limit)) /* cannot grow even a little? */
  80. luaG_runerror(L, "too many %s (limit is %d)", what, limit);
  81. size = limit; /* still have at least one free place */
  82. }
  83. else {
  84. size *= 2;
  85. if (size < MINSIZEARRAY)
  86. size = MINSIZEARRAY; /* minimum size */
  87. }
  88. lua_assert(nelems + 1 <= size && size <= limit);
  89. /* 'limit' ensures that multiplication will not overflow */
  90. newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems,
  91. cast_sizet(size) * size_elems);
  92. *psize = size; /* update only when everything else is OK */
  93. return newblock;
  94. }
  95. /*
  96. ** In prototypes, the size of the array is also its number of
  97. ** elements (to save memory). So, if it cannot shrink an array
  98. ** to its number of elements, the only option is to raise an
  99. ** error.
  100. */
  101. void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
  102. int final_n, int size_elem) {
  103. void *newblock;
  104. size_t oldsize = cast_sizet((*size) * size_elem);
  105. size_t newsize = cast_sizet(final_n * size_elem);
  106. lua_assert(newsize <= oldsize);
  107. newblock = luaM_saferealloc_(L, block, oldsize, newsize);
  108. *size = final_n;
  109. return newblock;
  110. }
  111. /* }================================================================== */
  112. l_noret luaM_toobig (lua_State *L) {
  113. luaG_runerror(L, "memory allocation error: block too big");
  114. }
  115. /*
  116. ** Free memory
  117. */
  118. void luaM_free_ (lua_State *L, void *block, size_t osize) {
  119. global_State *g = G(L);
  120. lua_assert((osize == 0) == (block == NULL));
  121. callfrealloc(g, block, osize, 0);
  122. g->GCdebt -= osize;
  123. }
  124. /*
  125. ** In case of allocation fail, this function will do an emergency
  126. ** collection to free some memory and then try the allocation again.
  127. */
  128. static void *tryagain (lua_State *L, void *block,
  129. size_t osize, size_t nsize) {
  130. global_State *g = G(L);
  131. if (cantryagain(g)) {
  132. luaC_fullgc(L, 1); /* try to free some memory... */
  133. return callfrealloc(g, block, osize, nsize); /* try again */
  134. }
  135. else return NULL; /* cannot run an emergency collection */
  136. }
  137. /*
  138. ** Generic allocation routine.
  139. */
  140. void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
  141. void *newblock;
  142. global_State *g = G(L);
  143. lua_assert((osize == 0) == (block == NULL));
  144. newblock = firsttry(g, block, osize, nsize);
  145. if (l_unlikely(newblock == NULL && nsize > 0)) {
  146. newblock = tryagain(L, block, osize, nsize);
  147. if (newblock == NULL) /* still no memory? */
  148. return NULL; /* do not update 'GCdebt' */
  149. }
  150. lua_assert((nsize == 0) == (newblock == NULL));
  151. g->GCdebt = (g->GCdebt + nsize) - osize;
  152. return newblock;
  153. }
  154. void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize,
  155. size_t nsize) {
  156. void *newblock = luaM_realloc_(L, block, osize, nsize);
  157. if (l_unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */
  158. luaM_error(L);
  159. return newblock;
  160. }
  161. void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
  162. if (size == 0)
  163. return NULL; /* that's all */
  164. else {
  165. global_State *g = G(L);
  166. void *newblock = firsttry(g, NULL, tag, size);
  167. if (l_unlikely(newblock == NULL)) {
  168. newblock = tryagain(L, NULL, tag, size);
  169. if (newblock == NULL)
  170. luaM_error(L);
  171. }
  172. g->GCdebt += size;
  173. return newblock;
  174. }
  175. }