malloc.h 5.9 KB

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