malloc.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #ifndef _MALLOC_H_
  4. #define _MALLOC_H_
  5. // Q: What are the 'a'-variant allocation functions?
  6. // A: They allocate memory from the stack, which is automatically
  7. // freed when the invoking function returns.
  8. // But it's not portable (http://c-faq.com/malloc/alloca.html)
  9. // and I have doubts our implementation works.
  10. // -> They should NOT be used, period.
  11. #ifndef __NETBSD__
  12. #if __STDC_VERSION__ < 199901L
  13. # if __GNUC__ >= 2
  14. # define __func__ __FUNCTION__
  15. # else
  16. # define __func__ ""
  17. # endif
  18. #endif
  19. #endif
  20. #define ALC_MARK __FILE__, __LINE__, __func__
  21. //////////////////////////////////////////////////////////////////////
  22. // Whether to use Athena's built-in Memory Manager (enabled by default)
  23. // To disable just comment the following line
  24. #if !defined(DMALLOC) && !defined(BCHECK)
  25. #define USE_MEMMGR
  26. #endif
  27. // Whether to enable Memory Manager's logging
  28. #define LOG_MEMMGR
  29. #ifdef USE_MEMMGR
  30. # define aMalloc(n) _mmalloc(n,ALC_MARK)
  31. # define aMallocA(n) _mmalloc(n,ALC_MARK)
  32. # define aCalloc(m,n) _mcalloc(m,n,ALC_MARK)
  33. # define aCallocA(m,n) _mcalloc(m,n,ALC_MARK)
  34. # define aRealloc(p,n) _mrealloc(p,n,ALC_MARK)
  35. # define aStrdup(p) _mstrdup(p,ALC_MARK)
  36. # define aFree(p) _mfree(p,ALC_MARK)
  37. void* _mmalloc (size_t size, const char *file, int line, const char *func);
  38. void* _mcalloc (size_t num, size_t size, const char *file, int line, const char *func);
  39. void* _mrealloc (void *p, size_t size, const char *file, int line, const char *func);
  40. char* _mstrdup (const char *p, const char *file, int line, const char *func);
  41. void _mfree (void *p, const char *file, int line, const char *func);
  42. #else
  43. # define aMalloc(n) aMalloc_((n),ALC_MARK)
  44. # define aMallocA(n) aMallocA_((n),ALC_MARK)
  45. # define aCalloc(m,n) aCalloc_((m),(n),ALC_MARK)
  46. # define aCallocA(m,n) aCallocA_(m,n,ALC_MARK)
  47. # define aRealloc(p,n) aRealloc_(p,n,ALC_MARK)
  48. # define aStrdup(p) aStrdup_(p,ALC_MARK)
  49. # define aFree(p) aFree_(p,ALC_MARK)
  50. void* aMalloc_ (size_t size, const char *file, int line, const char *func);
  51. void* aMallocA_ (size_t size, const char *file, int line, const char *func);
  52. void* aCalloc_ (size_t num, size_t size, const char *file, int line, const char *func);
  53. void* aCallocA_ (size_t num, size_t size, const char *file, int line, const char *func);
  54. void* aRealloc_ (void *p, size_t size, const char *file, int line, const char *func);
  55. char* aStrdup_ (const char *p, const char *file, int line, const char *func);
  56. void aFree_ (void *p, const char *file, int line, const char *func);
  57. #endif
  58. ////////////// Memory Managers //////////////////
  59. #ifdef MEMWATCH
  60. # include "memwatch.h"
  61. # define MALLOC(n,file,line,func) mwMalloc((n),(file),(line))
  62. # define MALLOCA(n,file,line,func) mwMalloc((n),(file),(line))
  63. # define CALLOC(m,n,file,line,func) mwCalloc((m),(n),(file),(line))
  64. # define CALLOCA(m,n,file,line,func) mwCalloc((m),(n),(file),(line))
  65. # define REALLOC(p,n,file,line,func) mwRealloc((p),(n),(file),(line))
  66. # define STRDUP(p,file,line,func) mwStrdup((p),(file),(line))
  67. # define FREE(p,file,line,func) mwFree((p),(file),(line))
  68. #elif defined(DMALLOC)
  69. # include "dmalloc.h"
  70. # define MALLOC(n,file,line,func) dmalloc_malloc((file),(line),(n),DMALLOC_FUNC_MALLOC,0,0)
  71. # define MALLOCA(n,file,line,func) dmalloc_malloc((file),(line),(n),DMALLOC_FUNC_MALLOC,0,0)
  72. # define CALLOC(m,n,file,line,func) dmalloc_malloc((file),(line),(m)*(n),DMALLOC_FUNC_CALLOC,0,0)
  73. # define CALLOCA(m,n,file,line,func) dmalloc_malloc((file),(line),(m)*(n),DMALLOC_FUNC_CALLOC,0,0)
  74. # define REALLOC(p,n,file,line,func) dmalloc_realloc((file),(line),(p),(n),DMALLOC_FUNC_REALLOC,0)
  75. # define STRDUP(p,file,line,func) strdup(p)
  76. # define FREE(p,file,line,func) free(p)
  77. #elif defined(GCOLLECT)
  78. # include "gc.h"
  79. # define MALLOC(n,file,line,func) GC_MALLOC(n)
  80. # define MALLOCA(n,file,line,func) GC_MALLOC_ATOMIC(n)
  81. # define CALLOC(m,n,file,line,func) _bcalloc((m),(n))
  82. # define CALLOCA(m,n,file,line,func) _bcallocA((m),(n))
  83. # define REALLOC(p,n,file,line,func) GC_REALLOC((p),(n))
  84. # define STRDUP(p,file,line,func) _bstrdup(p)
  85. # define FREE(p,file,line,func) GC_FREE(p)
  86. void * _bcalloc(size_t, size_t);
  87. void * _bcallocA(size_t, size_t);
  88. char * _bstrdup(const char *);
  89. /* FIXME Why is this the same as #else? [FlavioJS]
  90. #elif defined(BCHECK)
  91. # define MALLOC(n,file,line,func) malloc(n)
  92. # define MALLOCA(n,file,line,func) malloc(n)
  93. # define CALLOC(m,n,file,line,func) calloc((m),(n))
  94. # define CALLOCA(m,n,file,line,func) calloc((m),(n))
  95. # define REALLOC(p,n,file,line,func) realloc((p),(n))
  96. # define STRDUP(p,file,line,func) strdup(p)
  97. # define FREE(p,file,line,func) free(p)
  98. */
  99. #else
  100. # define MALLOC(n,file,line,func) malloc(n)
  101. # define MALLOCA(n,file,line,func) malloc(n)
  102. # define CALLOC(m,n,file,line,func) calloc((m),(n))
  103. # define CALLOCA(m,n,file,line,func) calloc((m),(n))
  104. # define REALLOC(p,n,file,line,func) realloc((p),(n))
  105. # define STRDUP(p,file,line,func) strdup(p)
  106. # define FREE(p,file,line,func) free(p)
  107. #endif
  108. /////////////// Buffer Creation /////////////////
  109. // Full credit for this goes to Shinomori [Ajarn]
  110. #ifdef __GNUC__ // GCC has variable length arrays
  111. #define CREATE_BUFFER(name, type, size) type name[size]
  112. #define DELETE_BUFFER(name)
  113. #else // others don't, so we emulate them
  114. #define CREATE_BUFFER(name, type, size) type *name = (type *) aCalloc (size, sizeof(type))
  115. #define DELETE_BUFFER(name) aFree(name)
  116. #endif
  117. ////////////// Others //////////////////////////
  118. // should be merged with any of above later
  119. #define CREATE(result, type, number) (result) = (type *) aCalloc ((number), sizeof(type))
  120. #define CREATE_A(result, type, number) (result) = (type *) aCallocA ((number), sizeof(type))
  121. #define RECREATE(result, type, number) (result) = (type *) aRealloc ((result), sizeof(type) * (number))
  122. ////////////////////////////////////////////////
  123. unsigned int malloc_usage (void);
  124. #ifndef INLINE
  125. #ifdef _WIN32
  126. #define INLINE
  127. #else
  128. #define INLINE inline
  129. #endif
  130. #endif
  131. void malloc_init (void);
  132. void malloc_final (void);
  133. #endif