aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Mark D. Roth <roth@google.com>2018-02-16 07:18:01 -0800
committerGravatar GitHub <noreply@github.com>2018-02-16 07:18:01 -0800
commitf1fcc4a23fc330eba95329f380c4e8bce225b467 (patch)
tree9eb05c5f4013be2357f4ce66adac883e40276206 /src
parentb0d71823a0f031ad1c04be30f22653177139da0b (diff)
parent835ae6b05842164fcf3a55ab7fc46d4459b79aee (diff)
Merge pull request #14432 from markdroth/alignment
Use aligned memory if needed.
Diffstat (limited to 'src')
-rw-r--r--src/core/lib/gprpp/memory.h13
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>