Revert "Use aligned_alloc directly for grpc_core::Arena"

This reverts commit 333ba8feae.
pull/18966/head
Alexander Polcyn 6 years ago
parent cb966a4e5e
commit d5fb6da369
  1. 3
      include/grpc/impl/codegen/port_platform.h
  2. 2
      include/grpc/support/alloc.h
  3. 78
      src/core/lib/gpr/alloc.cc
  4. 8
      src/core/lib/gpr/alloc.h
  5. 2
      src/core/lib/gprpp/arena.h
  6. 18
      test/core/gpr/alloc_test.cc
  7. 8
      test/core/util/memory_counters.cc

@ -77,7 +77,6 @@
#define GPR_WINDOWS 1
#define GPR_WINDOWS_SUBPROCESS 1
#define GPR_WINDOWS_ENV
#define GPR_HAS_ALIGNED_MALLOC 1
#ifdef __MSYS__
#define GPR_GETPID_IN_UNISTD_H 1
#define GPR_MSYS_TMPFILE
@ -174,7 +173,6 @@
#define GPR_POSIX_SYNC 1
#define GPR_POSIX_TIME 1
#define GPR_HAS_PTHREAD_H 1
#define GPR_HAS_ALIGNED_ALLOC 1
#define GPR_GETPID_IN_UNISTD_H 1
#ifdef _LP64
#define GPR_ARCH_64 1
@ -240,7 +238,6 @@
#define GPR_POSIX_SUBPROCESS 1
#define GPR_POSIX_SYNC 1
#define GPR_POSIX_TIME 1
#define GPR_HAS_POSIX_MEMALIGN 1
#define GPR_HAS_PTHREAD_H 1
#define GPR_GETPID_IN_UNISTD_H 1
#ifndef GRPC_CFSTREAM

@ -32,8 +32,6 @@ typedef struct gpr_allocation_functions {
void* (*zalloc_fn)(size_t size); /** if NULL, uses malloc_fn then memset */
void* (*realloc_fn)(void* ptr, size_t size);
void (*free_fn)(void* ptr);
void* (*aligned_alloc_fn)(size_t size, size_t alignment);
void (*aligned_free_fn)(void* ptr);
} gpr_allocation_functions;
/** malloc.

@ -23,7 +23,6 @@
#include <grpc/support/log.h>
#include <stdlib.h>
#include <string.h>
#include "src/core/lib/gpr/alloc.h"
#include "src/core/lib/profiling/timers.h"
static void* zalloc_with_calloc(size_t sz) { return calloc(sz, 1); }
@ -34,61 +33,8 @@ static void* zalloc_with_gpr_malloc(size_t sz) {
return p;
}
static constexpr bool is_power_of_two(size_t value) {
// 2^N = 100000...000
// 2^N - 1 = 011111...111
// (2^N) && ((2^N)-1)) = 0
return (value & (value - 1)) == 0;
}
static void* aligned_alloc_with_gpr_malloc(size_t size, size_t alignment) {
GPR_DEBUG_ASSERT(is_power_of_two(alignment));
size_t extra = alignment - 1 + sizeof(void*);
void* p = gpr_malloc(size + extra);
void** ret = (void**)(((uintptr_t)p + extra) & ~(alignment - 1));
ret[-1] = p;
return (void*)ret;
}
static void aligned_free_with_gpr_malloc(void* ptr) {
gpr_free((static_cast<void**>(ptr))[-1]);
}
static void* platform_malloc_aligned(size_t size, size_t alignment) {
#if defined(GPR_HAS_ALIGNED_ALLOC)
size = GPR_ROUND_UP_TO_SPECIFIED_SIZE(size, alignment);
void* ret = aligned_alloc(alignment, size);
GPR_ASSERT(ret != nullptr);
return ret;
#elif defined(GPR_HAS_ALIGNED_MALLOC)
GPR_DEBUG_ASSERT(is_power_of_two(alignment));
void* ret = _aligned_malloc(size, alignment);
GPR_ASSERT(ret != nullptr);
return ret;
#elif defined(GPR_HAS_POSIX_MEMALIGN)
GPR_DEBUG_ASSERT(is_power_of_two(alignment));
GPR_DEBUG_ASSERT(alignment % sizeof(void*) == 0);
void* ret = nullptr;
GPR_ASSERT(posix_memalign(&ret, alignment, size) == 0);
return ret;
#else
return aligned_alloc_with_gpr_malloc(size, alignment);
#endif
}
static void platform_free_aligned(void* ptr) {
#if defined(GPR_HAS_ALIGNED_ALLOC) || defined(GPR_HAS_POSIX_MEMALIGN)
free(ptr);
#elif defined(GPR_HAS_ALIGNED_MALLOC)
_aligned_free(ptr);
#else
aligned_free_with_gpr_malloc(ptr);
#endif
}
static gpr_allocation_functions g_alloc_functions = {
malloc, zalloc_with_calloc, realloc,
free, platform_malloc_aligned, platform_free_aligned};
static gpr_allocation_functions g_alloc_functions = {malloc, zalloc_with_calloc,
realloc, free};
gpr_allocation_functions gpr_get_allocation_functions() {
return g_alloc_functions;
@ -101,12 +47,6 @@ void gpr_set_allocation_functions(gpr_allocation_functions functions) {
if (functions.zalloc_fn == nullptr) {
functions.zalloc_fn = zalloc_with_gpr_malloc;
}
GPR_ASSERT((functions.aligned_alloc_fn == nullptr) ==
(functions.aligned_free_fn == nullptr));
if (functions.aligned_alloc_fn == nullptr) {
functions.aligned_alloc_fn = aligned_alloc_with_gpr_malloc;
functions.aligned_free_fn = aligned_free_with_gpr_malloc;
}
g_alloc_functions = functions;
}
@ -148,12 +88,12 @@ void* gpr_realloc(void* p, size_t size) {
}
void* gpr_malloc_aligned(size_t size, size_t alignment) {
GPR_TIMER_SCOPE("gpr_malloc_aligned", 0);
if (size == 0) return nullptr;
return g_alloc_functions.aligned_alloc_fn(size, alignment);
GPR_ASSERT(((alignment - 1) & alignment) == 0); // Must be power of 2.
size_t extra = alignment - 1 + sizeof(void*);
void* p = gpr_malloc(size + extra);
void** ret = (void**)(((uintptr_t)p + extra) & ~(alignment - 1));
ret[-1] = p;
return (void*)ret;
}
void gpr_free_aligned(void* ptr) {
GPR_TIMER_SCOPE("gpr_free_aligned", 0);
g_alloc_functions.aligned_free_fn(ptr);
}
void gpr_free_aligned(void* ptr) { gpr_free((static_cast<void**>(ptr))[-1]); }

@ -25,12 +25,4 @@
#define GPR_ROUND_UP_TO_ALIGNMENT_SIZE(x) \
(((x) + GPR_MAX_ALIGNMENT - 1u) & ~(GPR_MAX_ALIGNMENT - 1u))
#define GPR_ROUND_UP_TO_CACHELINE_SIZE(x) \
(((x) + GPR_CACHELINE_SIZE - 1u) & ~(GPR_CACHELINE_SIZE - 1u))
#define GPR_ROUND_UP_TO_SPECIFIED_SIZE(x, align) \
(((x) + align - 1u) & ~(align - 1u))
void* gpr_malloc_cacheline(size_t size);
#endif /* GRPC_CORE_LIB_GPR_ALLOC_H */

@ -61,7 +61,7 @@ class Arena {
GPR_ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(Arena));
size = GPR_ROUND_UP_TO_ALIGNMENT_SIZE(size);
size_t begin = total_used_.FetchAdd(size, MemoryOrder::RELAXED);
if (GPR_LIKELY(begin + size <= initial_zone_size_)) {
if (begin + size <= initial_zone_size_) {
return reinterpret_cast<char*>(this) + base_size + begin;
} else {
return AllocZone(size);

@ -31,21 +31,12 @@ static void fake_free(void* addr) {
*(static_cast<intptr_t*>(addr)) = static_cast<intptr_t>(0xdeadd00d);
}
static void* fake_aligned_malloc(size_t size, size_t alignment) {
return (void*)(size + alignment);
}
static void fake_aligned_free(void* addr) {
*(static_cast<intptr_t*>(addr)) = static_cast<intptr_t>(0xcafef00d);
}
static void test_custom_allocs() {
const gpr_allocation_functions default_fns = gpr_get_allocation_functions();
intptr_t addr_to_free = 0;
char* i;
gpr_allocation_functions fns = {fake_malloc, nullptr,
fake_realloc, fake_free,
fake_aligned_malloc, fake_aligned_free};
gpr_allocation_functions fns = {fake_malloc, nullptr, fake_realloc,
fake_free};
gpr_set_allocation_functions(fns);
GPR_ASSERT((void*)(size_t)0xdeadbeef == gpr_malloc(0xdeadbeef));
@ -54,11 +45,6 @@ static void test_custom_allocs() {
gpr_free(&addr_to_free);
GPR_ASSERT(addr_to_free == (intptr_t)0xdeadd00d);
GPR_ASSERT((void*)(size_t)(0xdeadbeef + 64) ==
gpr_malloc_aligned(0xdeadbeef, 64));
gpr_free_aligned(&addr_to_free);
GPR_ASSERT(addr_to_free == (intptr_t)0xcafef00d);
/* Restore and check we don't get funky values and that we don't leak */
gpr_set_allocation_functions(default_fns);
GPR_ASSERT((void*)sizeof(*i) !=

@ -90,12 +90,8 @@ static void guard_free(void* vptr) {
g_old_allocs.free_fn(ptr);
}
// NB: We do not specify guard_malloc_aligned/guard_free_aligned methods. Since
// they are null, calls to gpr_malloc_aligned/gpr_free_aligned are executed as a
// wrapper over gpr_malloc/gpr_free, which do use guard_malloc/guard_free, and
// thus there allocations are tracked as well.
struct gpr_allocation_functions g_guard_allocs = {
guard_malloc, nullptr, guard_realloc, guard_free, nullptr, nullptr};
struct gpr_allocation_functions g_guard_allocs = {guard_malloc, nullptr,
guard_realloc, guard_free};
void grpc_memory_counters_init() {
memset(&g_memory_counters, 0, sizeof(g_memory_counters));

Loading…
Cancel
Save