lmem.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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(HARDMEMTESTS)
  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 alocation.)
  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, NULL, x, s) creates a new block of size 's' (no
  38. ** matter 'x').
  39. **
  40. ** * frealloc(ud, p, x, 0) frees the block 'p'
  41. ** (in this specific case, frealloc must return NULL);
  42. ** particularly, frealloc(ud, NULL, 0, 0) does nothing
  43. ** (which is equivalent to free(NULL) in ISO C)
  44. **
  45. ** frealloc returns NULL if it cannot create or reallocate the area
  46. ** (any reallocation to an equal or smaller size cannot fail!)
  47. */
  48. #define MINSIZEARRAY 4
  49. void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
  50. int size_elems, int limit, const char *what) {
  51. void *newblock;
  52. int size = *psize;
  53. if (nelems + 1 <= size) /* does one extra element still fit? */
  54. return block; /* nothing to be done */
  55. if (size >= limit / 2) { /* cannot double it? */
  56. if (unlikely(size >= limit)) /* cannot grow even a little? */
  57. luaG_runerror(L, "too many %s (limit is %d)", what, limit);
  58. size = limit; /* still have at least one free place */
  59. }
  60. else {
  61. size *= 2;
  62. if (size < MINSIZEARRAY)
  63. size = MINSIZEARRAY; /* minimum size */
  64. }
  65. lua_assert(nelems + 1 <= size && size <= limit);
  66. /* 'limit' ensures that multiplication will not overflow */
  67. newblock = luaM_realloc_(L, block, cast_sizet(*psize) * size_elems,
  68. cast_sizet(size) * size_elems);
  69. if (unlikely(newblock == NULL))
  70. luaM_error(L);
  71. *psize = size; /* update only when everything else is OK */
  72. return newblock;
  73. }
  74. void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
  75. int final_n, int size_elem) {
  76. global_State *g = G(L);
  77. void *newblock;
  78. size_t oldsize = cast_sizet((*size) * size_elem);
  79. size_t newsize = cast_sizet(final_n * size_elem);
  80. lua_assert(newsize <= oldsize);
  81. newblock = (*g->frealloc)(g->ud, block, oldsize, newsize);
  82. if (unlikely(newblock == NULL && final_n > 0)) /* allocation failed? */
  83. luaM_error(L);
  84. else {
  85. g->GCdebt += newsize - oldsize;
  86. *size = final_n;
  87. return newblock;
  88. }
  89. }
  90. l_noret luaM_toobig (lua_State *L) {
  91. luaG_runerror(L, "memory allocation error: block too big");
  92. }
  93. /*
  94. ** Free memory
  95. */
  96. void luaM_free_ (lua_State *L, void *block, size_t osize) {
  97. global_State *g = G(L);
  98. lua_assert((osize == 0) == (block == NULL));
  99. (*g->frealloc)(g->ud, block, osize, 0);
  100. g->GCdebt -= osize;
  101. }
  102. /*
  103. ** In case of allocation fail, this function will call the GC to try
  104. ** to free some memory and then try the allocation again.
  105. ** (It should not be called when shrinking a block, because then the
  106. ** interpreter may be in the middle of a collection step.)
  107. */
  108. static void *tryagain (lua_State *L, void *block,
  109. size_t osize, size_t nsize) {
  110. global_State *g = G(L);
  111. if (ttisnil(&g->nilvalue)) { /* is state fully build? */
  112. luaC_fullgc(L, 1); /* try to free some memory... */
  113. return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
  114. }
  115. else return NULL; /* cannot free any memory without a full state */
  116. }
  117. /*
  118. ** generic allocation routine.
  119. */
  120. void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
  121. void *newblock;
  122. global_State *g = G(L);
  123. lua_assert((osize == 0) == (block == NULL));
  124. newblock = firsttry(g, block, osize, nsize);
  125. if (unlikely(newblock == NULL && nsize > 0)) {
  126. if (nsize > osize) /* not shrinking a block? */
  127. newblock = tryagain(L, block, osize, nsize);
  128. if (newblock == NULL) /* still no memory? */
  129. return NULL;
  130. }
  131. lua_assert((nsize == 0) == (newblock == NULL));
  132. g->GCdebt = (g->GCdebt + nsize) - osize;
  133. return newblock;
  134. }
  135. void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize,
  136. size_t nsize) {
  137. void *newblock = luaM_realloc_(L, block, osize, nsize);
  138. if (unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */
  139. luaM_error(L);
  140. return newblock;
  141. }
  142. void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
  143. if (size == 0)
  144. return NULL; /* that's all */
  145. else {
  146. global_State *g = G(L);
  147. void *newblock = firsttry(g, NULL, tag, size);
  148. if (unlikely(newblock == NULL)) {
  149. newblock = tryagain(L, NULL, tag, size);
  150. if (newblock == NULL)
  151. luaM_error(L);
  152. }
  153. g->GCdebt += size;
  154. return newblock;
  155. }
  156. }