malloc.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 "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. /////////////// Buffer Creation /////////////////
  49. // Full credit for this goes to Shinomori [Ajarn]
  50. #ifdef __GNUC__ // GCC has variable length arrays
  51. #define CREATE_BUFFER(name, type, size) type name[size]
  52. #define DELETE_BUFFER(name)
  53. #else // others don't, so we emulate them
  54. #define CREATE_BUFFER(name, type, size) type *name = (type *) aCalloc (size, sizeof(type))
  55. #define DELETE_BUFFER(name) aFree(name)
  56. #endif
  57. ////////////// Others //////////////////////////
  58. // should be merged with any of above later
  59. #define CREATE(result, type, number) (result) = (type *) aCalloc ((number), sizeof(type))
  60. #define RECREATE(result, type, number) (result) = (type *) aRealloc ((result), sizeof(type) * (number))
  61. ////////////////////////////////////////////////
  62. void malloc_memory_check(void);
  63. bool malloc_verify_ptr(void* ptr);
  64. size_t malloc_usage (void);
  65. void malloc_init (void);
  66. void malloc_final (void);
  67. #endif /* _MALLOC_H_ */