lmem.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. ** $Id: lmem.c,v 1.13 1999/02/26 15:50:10 roberto Exp roberto $
  3. ** Interface to Memory Manager
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include "lmem.h"
  8. #include "lstate.h"
  9. #include "lua.h"
  10. /*
  11. ** real ANSI systems do not need these tests;
  12. ** but some systems (Sun OS) are not that ANSI...
  13. */
  14. #ifdef OLD_ANSI
  15. #define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
  16. #define free(b) if (b) (free)(b)
  17. #endif
  18. #define MINSIZE 16 /* minimum size for "growing" vectors */
  19. #ifndef DEBUG
  20. static unsigned long power2 (unsigned long n) {
  21. unsigned long p = MINSIZE;
  22. while (p<=n) p<<=1;
  23. return p;
  24. }
  25. void *luaM_growaux (void *block, unsigned long nelems, int inc, int size,
  26. char *errormsg, unsigned long limit) {
  27. unsigned long newn = nelems+inc;
  28. if ((newn ^ nelems) <= nelems || /* still the same power of 2 limit? */
  29. (nelems > 0 && newn < MINSIZE)) /* or block already is MINSIZE? */
  30. return block; /* do not need to reallocate */
  31. else { /* it crossed a power of 2 boundary; grow to next power */
  32. if (newn >= limit)
  33. lua_error(errormsg);
  34. newn = power2(newn);
  35. if (newn > limit)
  36. newn = limit;
  37. return luaM_realloc(block, newn*size);
  38. }
  39. }
  40. /*
  41. ** generic allocation routine.
  42. */
  43. void *luaM_realloc (void *block, unsigned long size) {
  44. size_t s = (size_t)size;
  45. if (s != size)
  46. lua_error("memory allocation error: block too big");
  47. if (size == 0) {
  48. free(block); /* block may be NULL, that is OK for free */
  49. return NULL;
  50. }
  51. block = realloc(block, s);
  52. if (block == NULL)
  53. lua_error(memEM);
  54. return block;
  55. }
  56. #else
  57. /* DEBUG */
  58. #include <string.h>
  59. void *luaM_growaux (void *block, unsigned long nelems, int inc, int size,
  60. char *errormsg, unsigned long limit) {
  61. unsigned long newn = nelems+inc;
  62. if (newn >= limit)
  63. lua_error(errormsg);
  64. return luaM_realloc(block, newn*size);
  65. }
  66. #define HEADER (sizeof(double))
  67. #define MARK 55
  68. unsigned long numblocks = 0;
  69. unsigned long totalmem = 0;
  70. static void *checkblock (void *block) {
  71. unsigned long *b = (unsigned long *)((char *)block - HEADER);
  72. unsigned long size = *b;
  73. LUA_ASSERT(*(((char *)b)+size+HEADER) == MARK,
  74. "corrupted block");
  75. numblocks--;
  76. totalmem -= size;
  77. return b;
  78. }
  79. void *luaM_realloc (void *block, unsigned long size) {
  80. unsigned long realsize = HEADER+size+1;
  81. if (realsize != (size_t)realsize)
  82. lua_error("memory allocation error: block too big");
  83. if (size == 0) {
  84. if (block) {
  85. unsigned long *b = (unsigned long *)((char *)block - HEADER);
  86. memset(block, -1, *b); /* erase block */
  87. block = checkblock(block);
  88. }
  89. free(block);
  90. return NULL;
  91. }
  92. if (block)
  93. block = checkblock(block);
  94. block = (unsigned long *)realloc(block, realsize);
  95. if (block == NULL)
  96. lua_error(memEM);
  97. totalmem += size;
  98. numblocks++;
  99. *(unsigned long *)block = size;
  100. *(((char *)block)+size+HEADER) = MARK;
  101. return (unsigned long *)((char *)block+HEADER);
  102. }
  103. #endif