Skip to content

Commit

Permalink
Use aligned memory as needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
markdroth committed Feb 15, 2018
1 parent 8224c45 commit 835ae6b
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/core/lib/gprpp/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,27 @@

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)...);
}

// Alternative to delete, since we cannot use it (for fear of libstdc++)
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>
Expand Down

0 comments on commit 835ae6b

Please sign in to comment.