lmem.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. ** $Id: lmem.c,v 1.92 2017/12/06 18:36:31 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. /* 'limit' ensures that multiplication will not overflow */
  57. newblock = luaM_realloc(L, block, cast(size_t, *psize) * size_elems,
  58. cast(size_t, size) * size_elems);
  59. *psize = size; /* update only when everything else is OK */
  60. return newblock;
  61. }
  62. void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
  63. int final_n, int size_elem) {
  64. global_State *g = G(L);
  65. void *newblock;
  66. size_t oldsize = cast(size_t, (*size) * size_elem);
  67. size_t newsize = cast(size_t, final_n * size_elem);
  68. lua_assert(newsize <= oldsize);
  69. newblock = (*g->frealloc)(g->ud, block, oldsize, newsize);
  70. if (newblock == NULL && final_n > 0) /* allocation failed? */
  71. return block; /* keep old block */
  72. else {
  73. g->GCdebt += newsize - oldsize;
  74. *size = final_n;
  75. return newblock;
  76. }
  77. }
  78. l_noret luaM_toobig (lua_State *L) {
  79. luaG_runerror(L, "memory allocation error: block too big");
  80. }
  81. /*
  82. ** Free memory
  83. */
  84. void luaM_free_ (lua_State *L, void *block, size_t osize) {
  85. global_State *g = G(L);
  86. lua_assert((block == 0) == (block == NULL));
  87. (*g->frealloc)(g->ud, block, osize, 0);
  88. g->GCdebt -= osize;
  89. }
  90. /*
  91. ** generic allocation routine.
  92. */
  93. void *luaM_realloc (lua_State *L, void *block, size_t osize, size_t nsize) {
  94. void *newblock;
  95. global_State *g = G(L);
  96. lua_assert((osize == 0) == (block == NULL));
  97. hardtest(L, osize, nsize);
  98. newblock = (*g->frealloc)(g->ud, block, osize, nsize);
  99. if (newblock == NULL && nsize > 0) {
  100. lua_assert(nsize > osize); /* cannot fail when shrinking a block */
  101. if (g->version) { /* is state fully built? */
  102. luaC_fullgc(L, 1); /* try to free some memory... */
  103. newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
  104. }
  105. if (newblock == NULL)
  106. luaD_throw(L, LUA_ERRMEM);
  107. }
  108. lua_assert((nsize == 0) == (newblock == NULL));
  109. g->GCdebt = (g->GCdebt + nsize) - osize;
  110. return newblock;
  111. }
  112. void *luaM_malloc (lua_State *L, size_t size, int tag) {
  113. hardtest(L, 0, size);
  114. if (size == 0)
  115. return NULL; /* that's all */
  116. else {
  117. global_State *g = G(L);
  118. void *newblock = (*g->frealloc)(g->ud, NULL, tag, size);
  119. if (newblock == NULL) {
  120. if (g->version) { /* is state fully built? */
  121. luaC_fullgc(L, 1); /* try to free some memory... */
  122. newblock = (*g->frealloc)(g->ud, NULL, tag, size); /* try again */
  123. }
  124. if (newblock == NULL)
  125. luaD_throw(L, LUA_ERRMEM);
  126. }
  127. g->GCdebt += size;
  128. return newblock;
  129. }
  130. }