DelegateMQ
Loading...
Searching...
No Matches
xallocator.h
Go to the documentation of this file.
1#ifndef _XALLOCATOR_H
2#define _XALLOCATOR_H
3
4#include <cstdint>
5#include <stddef.h>
6
7// @see https://github.com/endurodave/xallocator
8// David Lafreniere
9
10#ifdef __cplusplus
11// Define AUTOMATIC_XALLOCATOR_INIT_DESTROY to automatically call xalloc_init() and
12// xalloc_destroy() when using xallocator in C++ projects. On embedded systems that
13// never exit, you can save 1-byte of RAM storage per translation unit by undefining
14// AUTOMATIC_XALLOCATOR_INIT_DESTROY and calling xalloc_init() manually before the OS
15// starts.
16#define AUTOMATIC_XALLOCATOR_INIT_DESTROY
17#ifdef AUTOMATIC_XALLOCATOR_INIT_DESTROY
25class XallocInitDestroy
26{
27public:
28 XallocInitDestroy();
29 ~XallocInitDestroy();
30private:
31 static int32_t refCount;
32};
33static XallocInitDestroy xallocInitDestroy;
34#endif // AUTOMATIC_XALLOCATOR_INIT_DESTROY
35#endif // __cplusplus
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
48void xalloc_init();
49
55void xalloc_destroy();
56
59void *xmalloc(size_t size);
60
63void xfree(void* ptr);
64
68void *xrealloc(void *ptr, size_t size);
69
71void xalloc_stats();
72
73#ifdef DMQ_ALLOCATOR
74 // Macro to overload new/delete with xalloc/xfree. Add macro to any class to enable
75 // fixed-block memory allocation. Add to a base class provides fixed-block memory
76 // for the base and all derived classes.
77 #define XALLOCATOR \
78 public: \
79 void* operator new(size_t size) { \
80 return xmalloc(size); \
81 } \
82 void* operator new(size_t size, void* mem) { \
83 return mem; \
84 } \
85 void* operator new(size_t size, const std::nothrow_t& nt) { \
86 return xmalloc(size); \
87 } \
88 void* operator new[](size_t size) { \
89 return xmalloc(size); \
90 } \
91 void operator delete(void* pObject) { \
92 xfree(pObject); \
93 } \
94 void operator delete(void* pObject, const std::nothrow_t& nt) { \
95 xfree(pObject); \
96 } \
97 void operator delete[](void* pData) { \
98 xfree(pData); \
99 }
100#endif
101
102
103#ifdef __cplusplus
104}
105#endif
106
107#endif
void xalloc_init()
Definition xallocator.cpp:176
void xfree(void *ptr)
Definition xallocator.cpp:302
void xalloc_stats()
Output allocator statistics to the standard output.
Definition xallocator.cpp:358
void xalloc_destroy()
Definition xallocator.cpp:217
void * xrealloc(void *ptr, size_t size)
Definition xallocator.cpp:324
void * xmalloc(size_t size)
Definition xallocator.cpp:284