lmem.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. ** $Id: lmem.c,v 1.91 2015/03/06 19:45:54 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 *size,
  41. int size_elems, int limit, const char *what) {
  42. void *newblock;
  43. int newsize;
  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. newsize = limit; /* still have at least one free place */
  50. }
  51. else {
  52. newsize = (*size)*2;
  53. if (newsize < MINSIZEARRAY)
  54. newsize = MINSIZEARRAY; /* minimum size */
  55. }
  56. newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
  57. *size = newsize; /* update only when everything else is OK */
  58. return newblock;
  59. }
  60. void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
  61. int final_n, int size_elem) {
  62. global_State *g = G(L);
  63. void *newblock;
  64. size_t oldsize = cast(size_t, (*size) * size_elem);
  65. size_t newsize = cast(size_t, final_n * size_elem);
  66. lua_assert(newsize <= oldsize);
  67. newblock = (*g->frealloc)(g->ud, block, oldsize, newsize);
  68. if (newblock == NULL && final_n > 0) /* allocation failed? */
  69. return block; /* keep old block */
  70. else {
  71. g->GCdebt += newsize - oldsize;
  72. *size = final_n;
  73. return newblock;
  74. }
  75. }
  76. l_noret luaM_toobig (lua_State *L) {
  77. luaG_runerror(L, "memory allocation error: block too big");
  78. }
  79. /*
  80. ** Free memory
  81. */
  82. void luaM_free_ (lua_State *L, void *block, size_t osize) {
  83. global_State *g = G(L);
  84. lua_assert((block == 0) == (block == NULL));
  85. (*g->frealloc)(g->ud, block, osize, 0);
  86. g->GCdebt -= osize;
  87. }
  88. /*
  89. ** generic allocation routine.
  90. */
  91. void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
  92. void *newblock;
  93. global_State *g = G(L);
  94. lua_assert((osize == 0) == (block == NULL));
  95. hardtest(L, osize, nsize);
  96. newblock = (*g->frealloc)(g->ud, block, osize, nsize);
  97. if (newblock == NULL && nsize > 0) {
  98. lua_assert(nsize > osize); /* cannot fail when shrinking a block */
  99. if (g->version) { /* is state fully built? */
  100. luaC_fullgc(L, 1); /* try to free some memory... */
  101. newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
  102. }
  103. if (newblock == NULL)
  104. luaD_throw(L, LUA_ERRMEM);
  105. }
  106. lua_assert((nsize == 0) == (newblock == NULL));
  107. g->GCdebt = (g->GCdebt + nsize) - osize;
  108. return newblock;
  109. }
  110. void *luaM_malloc (lua_State *L, size_t size, int tag) {
  111. hardtest(L, 0, size);
  112. if (size == 0)
  113. return NULL; /* that's all */
  114. else {
  115. global_State *g = G(L);
  116. void *newblock = (*g->frealloc)(g->ud, NULL, tag, size);
  117. if (newblock == NULL) {
  118. if (g->version) { /* is state fully built? */
  119. luaC_fullgc(L, 1); /* try to free some memory... */
  120. newblock = (*g->frealloc)(g->ud, NULL, tag, size); /* try again */
  121. }
  122. if (newblock == NULL)
  123. luaD_throw(L, LUA_ERRMEM);
  124. }
  125. g->GCdebt += size;
  126. return newblock;
  127. }
  128. }