lmem.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. ** $Id: lmem.c,v 1.93 2017/12/07 18:59:52 roberto Exp roberto $
  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 (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(size_t, *psize) * size_elems,
  59. cast(size_t, size) * size_elems);
  60. if (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(size_t, (*size) * size_elem);
  70. size_t newsize = cast(size_t, final_n * size_elem);
  71. lua_assert(newsize <= oldsize);
  72. newblock = (*g->frealloc)(g->ud, block, oldsize, newsize);
  73. if (newblock == NULL && final_n > 0) /* allocation failed? */
  74. return block; /* keep old block */
  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((block == 0) == (block == NULL));
  90. (*g->frealloc)(g->ud, block, osize, 0);
  91. g->GCdebt -= osize;
  92. }
  93. /*
  94. ** generic allocation routine.
  95. */
  96. void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
  97. void *newblock;
  98. global_State *g = G(L);
  99. lua_assert((osize == 0) == (block == NULL));
  100. hardtest(L, osize, nsize);
  101. newblock = (*g->frealloc)(g->ud, block, osize, nsize);
  102. if (newblock == NULL && nsize > 0) {
  103. /* Is state fully built? Not shrinking a block? */
  104. if (g->version && nsize > osize) {
  105. luaC_fullgc(L, 1); /* try to free some memory... */
  106. newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
  107. }
  108. if (newblock == NULL)
  109. return NULL;
  110. }
  111. lua_assert((nsize == 0) == (newblock == NULL));
  112. g->GCdebt = (g->GCdebt + nsize) - osize;
  113. return newblock;
  114. }
  115. void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize,
  116. size_t nsize) {
  117. void *newblock = luaM_realloc_(L, block, osize, nsize);
  118. if (newblock == NULL && nsize > 0) /* allocation failed? */
  119. luaM_error(L);
  120. return newblock;
  121. }
  122. void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
  123. hardtest(L, 0, size);
  124. if (size == 0)
  125. return NULL; /* that's all */
  126. else {
  127. global_State *g = G(L);
  128. void *newblock = (*g->frealloc)(g->ud, NULL, tag, size);
  129. if (newblock == NULL) {
  130. if (g->version) { /* is state fully built? */
  131. luaC_fullgc(L, 1); /* try to free some memory... */
  132. newblock = (*g->frealloc)(g->ud, NULL, tag, size); /* try again */
  133. }
  134. if (newblock == NULL)
  135. luaM_error(L);
  136. }
  137. g->GCdebt += size;
  138. return newblock;
  139. }
  140. }