lmem.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. ** $Id: lmem.c,v 1.91 2015/03/06 19:45:54 roberto Exp $
  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. /*
  18. ** About the realloc function:
  19. ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
  20. ** ('osize' is the old size, 'nsize' is the new size)
  21. **
  22. ** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no
  23. ** matter 'x').
  24. **
  25. ** * frealloc(ud, p, x, 0) frees the block 'p'
  26. ** (in this specific case, frealloc must return NULL);
  27. ** particularly, frealloc(ud, NULL, 0, 0) does nothing
  28. ** (which is equivalent to free(NULL) in ISO C)
  29. **
  30. ** frealloc returns NULL if it cannot create or reallocate the area
  31. ** (any reallocation to an equal or smaller size cannot fail!)
  32. */
  33. #define MINSIZEARRAY 4
  34. void *luaM_growaux_(lua_State *L, void *block, int *size, size_t size_elems,
  35. int limit, const char *what)
  36. {
  37. void *newblock;
  38. int newsize;
  39. if (*size >= limit / 2) /* cannot double it? */
  40. {
  41. if (*size >= limit) /* cannot grow even a little? */
  42. luaG_runerror(L, "too many %s (limit is %d)", what, limit);
  43. newsize = limit; /* still have at least one free place */
  44. }
  45. else
  46. {
  47. newsize = (*size) * 2;
  48. if (newsize < MINSIZEARRAY)
  49. newsize = MINSIZEARRAY; /* minimum size */
  50. }
  51. newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
  52. *size = newsize; /* update only when everything else is OK */
  53. return newblock;
  54. }
  55. l_noret luaM_toobig(lua_State *L)
  56. {
  57. luaG_runerror(L, "memory allocation error: block too big");
  58. }
  59. /*
  60. ** generic allocation routine.
  61. */
  62. void *luaM_realloc_(lua_State *L, void *block, size_t osize, size_t nsize)
  63. {
  64. void *newblock;
  65. global_State *g = G(L);
  66. size_t realosize = (block) ? osize : 0;
  67. lua_assert((realosize == 0) == (block == NULL));
  68. #if defined(HARDMEMTESTS)
  69. if (nsize > realosize && g->gcrunning)
  70. luaC_fullgc(L, 1); /* force a GC whenever possible */
  71. #endif
  72. newblock = (*g->frealloc)(g->ud, block, osize, nsize);
  73. if (newblock == NULL && nsize > 0)
  74. {
  75. lua_assert(nsize > realosize); /* cannot fail when shrinking a block */
  76. if (g->version) /* is state fully built? */
  77. {
  78. luaC_fullgc(L, 1); /* try to free some memory... */
  79. newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
  80. }
  81. if (newblock == NULL)
  82. luaD_throw(L, LUA_ERRMEM);
  83. }
  84. lua_assert((nsize == 0) == (newblock == NULL));
  85. g->GCdebt = (g->GCdebt + nsize) - realosize;
  86. return newblock;
  87. }