9
3

lmem.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. void *newblock;
  37. int newsize;
  38. if (*size >= limit/2) { /* cannot double it? */
  39. if (*size >= limit) /* cannot grow even a little? */
  40. luaG_runerror(L, "too many %s (limit is %d)", what, limit);
  41. newsize = limit; /* still have at least one free place */
  42. }
  43. else {
  44. newsize = (*size)*2;
  45. if (newsize < MINSIZEARRAY)
  46. newsize = MINSIZEARRAY; /* minimum size */
  47. }
  48. newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
  49. *size = newsize; /* update only when everything else is OK */
  50. return newblock;
  51. }
  52. l_noret luaM_toobig (lua_State *L) {
  53. luaG_runerror(L, "memory allocation error: block too big");
  54. }
  55. /*
  56. ** generic allocation routine.
  57. */
  58. void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
  59. void *newblock;
  60. global_State *g = G(L);
  61. size_t realosize = (block) ? osize : 0;
  62. lua_assert((realosize == 0) == (block == NULL));
  63. #if defined(HARDMEMTESTS)
  64. if (nsize > realosize && g->gcrunning)
  65. luaC_fullgc(L, 1); /* force a GC whenever possible */
  66. #endif
  67. newblock = (*g->frealloc)(g->ud, block, osize, nsize);
  68. if (newblock == NULL && nsize > 0) {
  69. lua_assert(nsize > realosize); /* cannot fail when shrinking a block */
  70. if (g->version) { /* is state fully built? */
  71. luaC_fullgc(L, 1); /* try to free some memory... */
  72. newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
  73. }
  74. if (newblock == NULL)
  75. luaD_throw(L, LUA_ERRMEM);
  76. }
  77. lua_assert((nsize == 0) == (newblock == NULL));
  78. g->GCdebt = (g->GCdebt + nsize) - realosize;
  79. return newblock;
  80. }