lmem.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. ** $Id: lmem.c,v 1.11 1999/02/25 15:16:26 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 some of these tests,
  12. ** since realloc(NULL, s)==malloc(s).
  13. ** But some systems (Sun OS) are not that ANSI...
  14. */
  15. #ifdef OLD_ANSI
  16. #define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
  17. #define free(b) if (b) (free)(b)
  18. #endif
  19. #define MINSIZE 16 /* minimum size for "growing" vectors */
  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) { /* cross a power of 2 boundary? */
  29. if (newn >= limit)
  30. lua_error(errormsg);
  31. newn = power2(newn);
  32. if (newn > limit)
  33. newn = limit;
  34. return luaM_realloc(block, newn*size);
  35. }
  36. else {
  37. LUA_ASSERT(power2(nelems) == power2(newn), "bad arithmetic");
  38. return block;
  39. }
  40. }
  41. #ifndef DEBUG
  42. /*
  43. ** generic allocation routine.
  44. */
  45. void *luaM_realloc (void *block, unsigned long size) {
  46. size_t s = (size_t)size;
  47. if (s != size)
  48. lua_error("memory allocation error: block too big");
  49. if (size == 0) {
  50. free(block); /* block may be NULL, that is OK for free */
  51. return NULL;
  52. }
  53. block = realloc(block, s);
  54. if (block == NULL)
  55. lua_error(memEM);
  56. return block;
  57. }
  58. #else
  59. /* DEBUG */
  60. #include <string.h>
  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