Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

103 Zeilen
2.7 KiB

  1. /*
  2. ** $Id: lmem.c,v 1.84 2012/05/23 15:41:53 roberto Exp $
  3. ** Interface to Memory Manager
  4. ** See Copyright Notice in lua.h
  5. */
  6. #if defined HAVE_CONFIG_H // LOL BEGIN
  7. # include "config.h"
  8. #endif // LOL END
  9. #include <stddef.h>
  10. #define lmem_c
  11. #define LUA_CORE
  12. #include "lua.h"
  13. #include "ldebug.h"
  14. #include "ldo.h"
  15. #include "lgc.h"
  16. #include "lmem.h"
  17. #include "lobject.h"
  18. #include "lstate.h"
  19. /*
  20. ** About the realloc function:
  21. ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
  22. ** (`osize' is the old size, `nsize' is the new size)
  23. **
  24. ** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no
  25. ** matter 'x').
  26. **
  27. ** * frealloc(ud, p, x, 0) frees the block `p'
  28. ** (in this specific case, frealloc must return NULL);
  29. ** particularly, frealloc(ud, NULL, 0, 0) does nothing
  30. ** (which is equivalent to free(NULL) in ANSI C)
  31. **
  32. ** frealloc returns NULL if it cannot create or reallocate the area
  33. ** (any reallocation to an equal or smaller size cannot fail!)
  34. */
  35. #define MINSIZEARRAY 4
  36. void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
  37. int limit, const char *what) {
  38. void *newblock;
  39. int newsize;
  40. if (*size >= limit/2) { /* cannot double it? */
  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. newsize = (*size)*2;
  47. if (newsize < MINSIZEARRAY)
  48. newsize = MINSIZEARRAY; /* minimum size */
  49. }
  50. newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
  51. *size = newsize; /* update only when everything else is OK */
  52. return newblock;
  53. }
  54. l_noret luaM_toobig (lua_State *L) {
  55. luaG_runerror(L, "memory allocation error: block too big");
  56. }
  57. /*
  58. ** generic allocation routine.
  59. */
  60. void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
  61. void *newblock;
  62. global_State *g = G(L);
  63. size_t realosize = (block) ? osize : 0;
  64. lua_assert((realosize == 0) == (block == NULL));
  65. #if defined(HARDMEMTESTS)
  66. if (nsize > realosize && g->gcrunning)
  67. luaC_fullgc(L, 1); /* force a GC whenever possible */
  68. #endif
  69. newblock = (*g->frealloc)(g->ud, block, osize, nsize);
  70. if (newblock == NULL && nsize > 0) {
  71. api_check(L, nsize > realosize,
  72. "realloc cannot fail when shrinking a block");
  73. if (g->gcrunning) {
  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. }