malloc.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #define ALC_MARK __FILE__, __LINE__, __func__
  7. // default use of the built-in memory manager
  8. #if !defined(NO_MEMMGR) && !defined(USE_MEMMGR)
  9. #if defined(MEMWATCH) || defined(DMALLOC) || defined(GCOLLECT)
  10. // disable built-in memory manager when using another memory library
  11. #define NO_MEMMGR
  12. #else
  13. // use built-in memory manager by default
  14. #define USE_MEMMGR
  15. #endif
  16. #endif
  17. //////////////////////////////////////////////////////////////////////
  18. // Athena's built-in Memory Manager
  19. #ifdef USE_MEMMGR
  20. // Enable memory manager logging by default
  21. #define LOG_MEMMGR
  22. // no logging for minicore
  23. #if defined(MINICORE) && defined(LOG_MEMMGR)
  24. #undef LOG_MEMMGR
  25. #endif
  26. # define aMalloc(n) _mmalloc(n,ALC_MARK)
  27. # define aCalloc(m,n) _mcalloc(m,n,ALC_MARK)
  28. # define aRealloc(p,n) _mrealloc(p,n,ALC_MARK)
  29. # define aStrdup(p) _mstrdup(p,ALC_MARK)
  30. # define aFree(p) _mfree(p,ALC_MARK)
  31. void* _mmalloc (size_t size, const char *file, int line, const char *func);
  32. void* _mcalloc (size_t num, size_t size, const char *file, int line, const char *func);
  33. void* _mrealloc (void *p, size_t size, const char *file, int line, const char *func);
  34. char* _mstrdup (const char *p, const char *file, int line, const char *func);
  35. void _mfree (void *p, const char *file, int line, const char *func);
  36. #else
  37. # define aMalloc(n) aMalloc_((n),ALC_MARK)
  38. # define aCalloc(m,n) aCalloc_((m),(n),ALC_MARK)
  39. # define aRealloc(p,n) aRealloc_(p,n,ALC_MARK)
  40. # define aStrdup(p) aStrdup_(p,ALC_MARK)
  41. # define aFree(p) aFree_(p,ALC_MARK)
  42. void* aMalloc_ (size_t size, const char *file, int line, const char *func);
  43. void* aCalloc_ (size_t num, size_t size, const char *file, int line, const char *func);
  44. void* aRealloc_ (void *p, size_t size, const char *file, int line, const char *func);
  45. char* aStrdup_ (const char *p, const char *file, int line, const char *func);
  46. void aFree_ (void *p, const char *file, int line, const char *func);
  47. #endif
  48. // deprecated, do not use
  49. #define aMallocA aMalloc
  50. #define aCallocA aCalloc
  51. #define CREATE_A CREATE
  52. /////////////// Buffer Creation /////////////////
  53. // Full credit for this goes to Shinomori [Ajarn]
  54. #ifdef __GNUC__ // GCC has variable length arrays
  55. #define CREATE_BUFFER(name, type, size) type name[size]
  56. #define DELETE_BUFFER(name)
  57. #else // others don't, so we emulate them
  58. #define CREATE_BUFFER(name, type, size) type *name = (type *) aCalloc (size, sizeof(type))
  59. #define DELETE_BUFFER(name) aFree(name)
  60. #endif
  61. ////////////// Others //////////////////////////
  62. // should be merged with any of above later
  63. #define CREATE(result, type, number) (result) = (type *) aCalloc ((number), sizeof(type))
  64. #define RECREATE(result, type, number) (result) = (type *) aRealloc ((result), sizeof(type) * (number))
  65. ////////////////////////////////////////////////
  66. void malloc_memory_check(void);
  67. bool malloc_verify_ptr(void* ptr);
  68. size_t malloc_usage (void);
  69. void malloc_init (void);
  70. void malloc_final (void);
  71. #endif /* _MALLOC_H_ */