diff options
author | Mark D. Roth <roth@google.com> | 2018-02-16 07:18:01 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-16 07:18:01 -0800 |
commit | f1fcc4a23fc330eba95329f380c4e8bce225b467 (patch) | |
tree | 9eb05c5f4013be2357f4ce66adac883e40276206 | |
parent | b0d71823a0f031ad1c04be30f22653177139da0b (diff) | |
parent | 835ae6b05842164fcf3a55ab7fc46d4459b79aee (diff) |
Merge pull request #14432 from markdroth/alignment
Use aligned memory if needed.
-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> |