lmem.c 4.9 KB

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