diff options
author | 2018-02-15 10:42:52 -0800 | |
---|---|---|
committer | 2018-02-15 10:42:52 -0800 | |
commit | 835ae6b05842164fcf3a55ab7fc46d4459b79aee (patch) | |
tree | 23291fd1a0cec16ded950c51c13b0a2229c36d51 /src/core/lib/gprpp/memory.h | |
parent | 8224c45866c6a2cfa29ede0c91a6ae9f40572658 (diff) |
Use aligned memory as needed.
Diffstat (limited to 'src/core/lib/gprpp/memory.h')
-rw-r--r-- | src/core/lib/gprpp/memory.h | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/core/lib/gprpp/memory.h b/src/core/lib/gprpp/memory.h index 17f42f5983..f7cd6424af 100644 --- a/src/core/lib/gprpp/memory.h +++ b/src/core/lib/gprpp/memory.h @@ -27,10 +27,15 @@ namespace grpc_core { +// The alignment of memory returned by gpr_malloc(). +constexpr size_t kAllignmentForDefaultAllocationInBytes = 8; + // Alternative to new, since we cannot use it (for fear of libstdc++) template <typename T, typename... Args> inline T* New(Args&&... args) { - void* p = gpr_malloc(sizeof(T)); + void* p = alignof(T) > kAllignmentForDefaultAllocationInBytes + ? gpr_malloc_aligned(sizeof(T), alignof(T)) + : gpr_malloc(sizeof(T)); return new (p) T(std::forward<Args>(args)...); } @@ -38,7 +43,11 @@ inline T* New(Args&&... args) { template <typename T> inline void Delete(T* p) { p->~T(); - gpr_free(p); + if (alignof(T) > kAllignmentForDefaultAllocationInBytes) { + gpr_free_aligned(p); + } else { + gpr_free(p); + } } template <typename T> |