lmem.c 5.8 KB

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