lmem.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. ** $Id: lmem.c,v 1.14 1999/03/01 17:49:13 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 8 /* 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 >= limit) lua_error(errormsg);
  29. if ((newn ^ nelems) <= nelems || /* still the same power of 2 limit? */
  30. (nelems > 0 && newn < MINSIZE)) /* or block already is MINSIZE? */
  31. return block; /* do not need to reallocate */
  32. else /* it crossed a power of 2 boundary; grow to next power */
  33. return luaM_realloc(block, power2(newn)*size);
  34. }
  35. /*
  36. ** generic allocation routine.
  37. */
  38. void *luaM_realloc (void *block, unsigned long size) {
  39. size_t s = (size_t)size;
  40. if (s != size)
  41. lua_error("memory allocation error: block too big");
  42. if (size == 0) {
  43. free(block); /* block may be NULL, that is OK for free */
  44. return NULL;
  45. }
  46. block = realloc(block, s);
  47. if (block == NULL)
  48. lua_error(memEM);
  49. return block;
  50. }
  51. #else
  52. /* DEBUG */
  53. #include <string.h>
  54. void *luaM_growaux (void *block, unsigned long nelems, int inc, int size,
  55. char *errormsg, unsigned long limit) {
  56. unsigned long newn = nelems+inc;
  57. if (newn >= limit)
  58. lua_error(errormsg);
  59. return luaM_realloc(block, newn*size);
  60. }
  61. #define HEADER (sizeof(double))
  62. #define MARK 55
  63. unsigned long numblocks = 0;
  64. unsigned long totalmem = 0;
  65. static void *checkblock (void *block) {
  66. unsigned long *b = (unsigned long *)((char *)block - HEADER);
  67. unsigned long size = *b;
  68. LUA_ASSERT(*(((char *)b)+size+HEADER) == MARK,
  69. "corrupted block");
  70. numblocks--;
  71. totalmem -= size;
  72. return b;
  73. }
  74. void *luaM_realloc (void *block, unsigned long size) {
  75. unsigned long realsize = HEADER+size+1;
  76. if (realsize != (size_t)realsize)
  77. lua_error("memory allocation error: block too big");
  78. if (size == 0) {
  79. if (block) {
  80. unsigned long *b = (unsigned long *)((char *)block - HEADER);
  81. memset(block, -1, *b); /* erase block */
  82. block = checkblock(block);
  83. }
  84. free(block);
  85. return NULL;
  86. }
  87. if (block)
  88. block = checkblock(block);
  89. block = (unsigned long *)realloc(block, realsize);
  90. if (block == NULL)
  91. lua_error(memEM);
  92. totalmem += size;
  93. numblocks++;
  94. *(unsigned long *)block = size;
  95. *(((char *)block)+size+HEADER) = MARK;
  96. return (unsigned long *)((char *)block+HEADER);
  97. }
  98. #endif