diff options
author | Vijay Pai <vpai@google.com> | 2018-03-01 11:48:29 -0800 |
---|---|---|
committer | Vijay Pai <vpai@google.com> | 2018-03-01 11:48:29 -0800 |
commit | b357f2f548884e0f1766b5b1c3b12e1aa64642cc (patch) | |
tree | 84a5f405562ee887fe79fb36b8a7fe42773933c6 /src/core/lib/gpr | |
parent | 2fe87b09055cd256cdce038c4c70d92b955c991b (diff) | |
parent | ccd1d55807bdb13b661dcf1d651468b2d98ff5af (diff) |
Merge branch 'master' into 2phase_thd
Diffstat (limited to 'src/core/lib/gpr')
-rw-r--r-- | src/core/lib/gpr/arena.cc | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/core/lib/gpr/arena.cc b/src/core/lib/gpr/arena.cc index 444bb3d719..b02c5b9fb6 100644 --- a/src/core/lib/gpr/arena.cc +++ b/src/core/lib/gpr/arena.cc @@ -26,6 +26,49 @@ #include <grpc/support/atm.h> #include <grpc/support/log.h> +// Uncomment this to use a simple arena that simply allocates the +// requested amount of memory for each call to gpr_arena_alloc(). This +// effectively eliminates the efficiency gain of using an arena, but it +// may be useful for debugging purposes. +//#define SIMPLE_ARENA_FOR_DEBUGGING + +#ifdef SIMPLE_ARENA_FOR_DEBUGGING + +#include <grpc/support/sync.h> + +struct gpr_arena { + gpr_mu mu; + void** ptrs; + size_t num_ptrs; +}; + +gpr_arena* gpr_arena_create(size_t ignored_initial_size) { + gpr_arena* arena = (gpr_arena*)gpr_zalloc(sizeof(*arena)); + gpr_mu_init(&arena->mu); + return arena; +} + +size_t gpr_arena_destroy(gpr_arena* arena) { + gpr_mu_destroy(&arena->mu); + for (size_t i = 0; i < arena->num_ptrs; ++i) { + gpr_free(arena->ptrs[i]); + } + gpr_free(arena->ptrs); + gpr_free(arena); + return 1; // Value doesn't matter, since it won't be used. +} + +void* gpr_arena_alloc(gpr_arena* arena, size_t size) { + gpr_mu_lock(&arena->mu); + arena->ptrs = + (void**)gpr_realloc(arena->ptrs, sizeof(void*) * (arena->num_ptrs + 1)); + void* retval = arena->ptrs[arena->num_ptrs++] = gpr_zalloc(size); + gpr_mu_unlock(&arena->mu); + return retval; +} + +#else // SIMPLE_ARENA_FOR_DEBUGGING + // TODO(roth): We currently assume that all callers need alignment of 16 // bytes, which may be wrong in some cases. As part of converting the // arena API to C++, we should consider replacing gpr_arena_alloc() with a @@ -105,3 +148,5 @@ void* gpr_arena_alloc(gpr_arena* arena, size_t size) { ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(zone)); return ptr + start - z->size_begin; } + +#endif // SIMPLE_ARENA_FOR_DEBUGGING |