lmem.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. ** $Id: lmem.c,v 1.58 2002/10/08 18:45:07 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 l_realloc(NULL,
  15. ** 0, x) allocates a new block (ANSI C assures that). (`os' is the old
  16. ** block size; some allocators may use that.)
  17. */
  18. #ifndef l_realloc
  19. #define l_realloc(b,os,s) realloc(b,s)
  20. #endif
  21. /*
  22. ** definition for free function. (`os' is the old block size; some
  23. ** allocators may use that.)
  24. */
  25. #ifndef l_free
  26. #define l_free(b,os) free(b)
  27. #endif
  28. #define MINSIZEARRAY 4
  29. void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
  30. int limit, const char *errormsg) {
  31. void *newblock;
  32. int newsize = (*size)*2;
  33. if (newsize < MINSIZEARRAY)
  34. newsize = MINSIZEARRAY; /* minimum size */
  35. else if (*size >= limit/2) { /* cannot double it? */
  36. if (*size < limit - MINSIZEARRAY) /* try something smaller... */
  37. newsize = limit; /* still have at least MINSIZEARRAY free places */
  38. else luaG_runerror(L, errormsg);
  39. }
  40. newblock = luaM_realloc(L, block,
  41. cast(lu_mem, *size)*cast(lu_mem, size_elems),
  42. cast(lu_mem, newsize)*cast(lu_mem, size_elems));
  43. *size = newsize; /* update only when everything else is OK */
  44. return newblock;
  45. }
  46. /*
  47. ** generic allocation routine.
  48. */
  49. void *luaM_realloc (lua_State *L, void *block, lu_mem oldsize, lu_mem size) {
  50. lua_assert((oldsize == 0) == (block == NULL));
  51. if (size == 0) {
  52. if (block != NULL) {
  53. l_free(block, oldsize);
  54. block = NULL;
  55. }
  56. }
  57. else if (size >= MAX_SIZET)
  58. luaG_runerror(L, "memory allocation error: block too big");
  59. else {
  60. block = l_realloc(block, oldsize, size);
  61. if (block == NULL) {
  62. if (L)
  63. luaD_throw(L, LUA_ERRMEM);
  64. else return NULL; /* error before creating state! */
  65. }
  66. }
  67. if (L && G(L)) {
  68. G(L)->nblocks -= oldsize;
  69. G(L)->nblocks += size;
  70. }
  71. return block;
  72. }