lmem.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. ** $Id: lmem.c,v 1.56 2002/06/11 16:23:47 roberto Exp roberto $
  3. ** Interface to Memory Manager
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include "lua.h"
  8. #include "ldebug.h"
  9. #include "ldo.h"
  10. #include "lmem.h"
  11. #include "lobject.h"
  12. #include "lstate.h"
  13. /*
  14. ** definition for realloc function. It must assure that
  15. ** l_realloc(block, x, 0) frees the block, and l_realloc(NULL, 0, x)
  16. ** allocates a new block (ANSI C assures that).
  17. ** (`os' is the old block size; some allocators may use that.)
  18. */
  19. #ifndef l_realloc
  20. #define l_realloc(b,os,s) realloc(b,s)
  21. #endif
  22. #define MINSIZEARRAY 4
  23. void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
  24. int limit, const char *errormsg) {
  25. void *newblock;
  26. int newsize = (*size)*2;
  27. if (newsize < MINSIZEARRAY)
  28. newsize = MINSIZEARRAY; /* minimum size */
  29. else if (*size >= limit/2) { /* cannot double it? */
  30. if (*size < limit - MINSIZEARRAY) /* try something smaller... */
  31. newsize = limit; /* still have at least MINSIZEARRAY free places */
  32. else luaG_runerror(L, errormsg);
  33. }
  34. newblock = luaM_realloc(L, block,
  35. cast(lu_mem, *size)*cast(lu_mem, size_elems),
  36. cast(lu_mem, newsize)*cast(lu_mem, size_elems));
  37. *size = newsize; /* update only when everything else is OK */
  38. return newblock;
  39. }
  40. /*
  41. ** generic allocation routine.
  42. */
  43. void *luaM_realloc (lua_State *L, void *block, lu_mem oldsize, lu_mem size) {
  44. if (size == 0) {
  45. if (block != NULL) {
  46. l_realloc(block, oldsize, size);
  47. block = NULL;
  48. }
  49. }
  50. else if (size >= MAX_SIZET)
  51. luaG_runerror(L, "memory allocation error: block too big");
  52. else {
  53. block = l_realloc(block, oldsize, size);
  54. if (block == NULL) {
  55. if (L)
  56. luaD_throw(L, LUA_ERRMEM);
  57. else return NULL; /* error before creating state! */
  58. }
  59. }
  60. if (L && G(L)) {
  61. G(L)->nblocks -= oldsize;
  62. G(L)->nblocks += size;
  63. }
  64. return block;
  65. }