Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

104 рядки
2.7 KiB

  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. #if defined HAVE_CONFIG_H // LOL BEGIN
  10. # include "config.h"
  11. #endif // LOL END
  12. #include <stddef.h>
  13. #include "lua.h"
  14. #include "ldebug.h"
  15. #include "ldo.h"
  16. #include "lgc.h"
  17. #include "lmem.h"
  18. #include "lobject.h"
  19. #include "lstate.h"
  20. /*
  21. ** About the realloc function:
  22. ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
  23. ** ('osize' is the old size, 'nsize' is the new size)
  24. **
  25. ** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no
  26. ** matter 'x').
  27. **
  28. ** * frealloc(ud, p, x, 0) frees the block 'p'
  29. ** (in this specific case, frealloc must return NULL);
  30. ** particularly, frealloc(ud, NULL, 0, 0) does nothing
  31. ** (which is equivalent to free(NULL) in ISO C)
  32. **
  33. ** frealloc returns NULL if it cannot create or reallocate the area
  34. ** (any reallocation to an equal or smaller size cannot fail!)
  35. */
  36. #define MINSIZEARRAY 4
  37. void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
  38. int limit, const char *what) {
  39. void *newblock;
  40. int newsize;
  41. if (*size >= limit/2) { /* cannot double it? */
  42. if (*size >= limit) /* cannot grow even a little? */
  43. luaG_runerror(L, "too many %s (limit is %d)", what, limit);
  44. newsize = limit; /* still have at least one free place */
  45. }
  46. else {
  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. luaG_runerror(L, "memory allocation error: block too big");
  57. }
  58. /*
  59. ** generic allocation routine.
  60. */
  61. void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
  62. void *newblock;
  63. global_State *g = G(L);
  64. size_t realosize = (block) ? osize : 0;
  65. lua_assert((realosize == 0) == (block == NULL));
  66. #if defined(HARDMEMTESTS)
  67. if (nsize > realosize && g->gcrunning)
  68. luaC_fullgc(L, 1); /* force a GC whenever possible */
  69. #endif
  70. newblock = (*g->frealloc)(g->ud, block, osize, nsize);
  71. if (newblock == NULL && nsize > 0) {
  72. lua_assert(nsize > realosize); /* cannot fail when shrinking a block */
  73. if (g->version) { /* is state fully built? */
  74. luaC_fullgc(L, 1); /* try to free some memory... */
  75. newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
  76. }
  77. if (newblock == NULL)
  78. luaD_throw(L, LUA_ERRMEM);
  79. }
  80. lua_assert((nsize == 0) == (newblock == NULL));
  81. g->GCdebt = (g->GCdebt + nsize) - realosize;
  82. return newblock;
  83. }