lmem.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. ** $Id: lmem.c,v 1.80 2010/12/20 18:17:46 roberto Exp roberto $
  3. ** Interface to Memory Manager
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stddef.h>
  7. #define lmem_c
  8. #define LUA_CORE
  9. #include "lua.h"
  10. #include "ldebug.h"
  11. #include "ldo.h"
  12. #include "lgc.h"
  13. #include "lmem.h"
  14. #include "lobject.h"
  15. #include "lstate.h"
  16. /*
  17. ** About the realloc function:
  18. ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
  19. ** (`osize' is the old size, `nsize' is the new size)
  20. **
  21. ** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no
  22. ** matter 'x').
  23. **
  24. ** * frealloc(ud, p, x, 0) frees the block `p'
  25. ** (in this specific case, frealloc must return NULL);
  26. ** particularly, frealloc(ud, NULL, 0, 0) does nothing
  27. ** (which is equivalent to free(NULL) in ANSI C)
  28. **
  29. ** frealloc returns NULL if it cannot create or reallocate the area
  30. ** (any reallocation to an equal or smaller size cannot fail!)
  31. */
  32. #define MINSIZEARRAY 4
  33. void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
  34. int limit, const char *what) {
  35. void *newblock;
  36. int newsize;
  37. if (*size >= limit/2) { /* cannot double it? */
  38. if (*size >= limit) /* cannot grow even a little? */
  39. luaG_runerror(L, "too many %s (limit is %d)", what, limit);
  40. newsize = limit; /* still have at least one free place */
  41. }
  42. else {
  43. newsize = (*size)*2;
  44. if (newsize < MINSIZEARRAY)
  45. newsize = MINSIZEARRAY; /* minimum size */
  46. }
  47. newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
  48. *size = newsize; /* update only when everything else is OK */
  49. return newblock;
  50. }
  51. void *luaM_toobig (lua_State *L) {
  52. luaG_runerror(L, "memory allocation error: block too big");
  53. return NULL; /* to avoid warnings */
  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. api_check(L, nsize > realosize,
  70. "realloc cannot fail when shrinking a block");
  71. if (g->gcrunning) {
  72. luaC_fullgc(L, 1); /* try to free some memory... */
  73. newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
  74. }
  75. if (newblock == NULL)
  76. luaD_throw(L, LUA_ERRMEM);
  77. }
  78. lua_assert((nsize == 0) == (newblock == NULL));
  79. g->GCdebt = (g->GCdebt + nsize) - realosize;
  80. #if defined(TRACEMEM)
  81. { /* auxiliary patch to monitor garbage collection.
  82. ** To plot, gnuplot with following command:
  83. ** plot TRACEMEM using 1:2 with lines, TRACEMEM using 1:3 with lines
  84. */
  85. static unsigned long total = 0; /* our "time" */
  86. static FILE *f = NULL; /* output file */
  87. total++; /* "time" always grows */
  88. if ((total % 200) == 0) {
  89. if (f == NULL) f = fopen(TRACEMEM, "w");
  90. fprintf(f, "%lu %u %d %d\n", total,
  91. g->totalbytes, g->GCdebt, g->gcstate * 1000);
  92. }
  93. }
  94. #endif
  95. return newblock;
  96. }