lmem.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. ** $Id: lmem.c,v 1.72 2006/09/14 12:59:06 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. ** Lua ensures that (ptr == NULL) iff (osize == 0).
  22. **
  23. ** * frealloc(ud, NULL, 0, x) creates a new block of size `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 ANSI 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. void *luaM_toobig (lua_State *L) {
  53. luaG_runerror(L, "memory allocation error: block too big");
  54. return NULL; /* to avoid warnings */
  55. }
  56. /*
  57. ** generic allocation routine.
  58. */
  59. void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
  60. void *newblock;
  61. global_State *g = G(L);
  62. lua_assert((osize == 0) == (block == NULL));
  63. #if defined(HARDMEMTESTS)
  64. if (nsize > osize && g->GCthreshold != MAX_LUMEM)
  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 > osize); /* cannot fail when shrinking a block */
  70. if (g->GCthreshold != MAX_LUMEM) {
  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->totalbytes = (g->totalbytes - osize) + nsize;
  79. return newblock;
  80. }