aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/lib/support
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/lib/support')
-rw-r--r--src/core/lib/support/alloc.c27
-rw-r--r--src/core/lib/support/cmdline.c6
-rw-r--r--src/core/lib/support/histogram.c3
-rw-r--r--src/core/lib/support/subprocess_posix.c3
4 files changed, 30 insertions, 9 deletions
diff --git a/src/core/lib/support/alloc.c b/src/core/lib/support/alloc.c
index 618c3f6acd..9973166217 100644
--- a/src/core/lib/support/alloc.c
+++ b/src/core/lib/support/alloc.c
@@ -36,9 +36,19 @@
#include <grpc/support/log.h>
#include <grpc/support/port_platform.h>
#include <stdlib.h>
+#include <string.h>
#include "src/core/lib/profiling/timers.h"
-static gpr_allocation_functions g_alloc_functions = {malloc, realloc, free};
+static void *zalloc_with_calloc(size_t sz) { return calloc(sz, 1); }
+
+static void *zalloc_with_gpr_malloc(size_t sz) {
+ void *p = gpr_malloc(sz);
+ memset(p, 0, sz);
+ return p;
+}
+
+static gpr_allocation_functions g_alloc_functions = {malloc, zalloc_with_calloc,
+ realloc, free};
gpr_allocation_functions gpr_get_allocation_functions() {
return g_alloc_functions;
@@ -48,6 +58,9 @@ void gpr_set_allocation_functions(gpr_allocation_functions functions) {
GPR_ASSERT(functions.malloc_fn != NULL);
GPR_ASSERT(functions.realloc_fn != NULL);
GPR_ASSERT(functions.free_fn != NULL);
+ if (functions.zalloc_fn == NULL) {
+ functions.zalloc_fn = zalloc_with_gpr_malloc;
+ }
g_alloc_functions = functions;
}
@@ -63,6 +76,18 @@ void *gpr_malloc(size_t size) {
return p;
}
+void *gpr_zalloc(size_t size) {
+ void *p;
+ if (size == 0) return NULL;
+ GPR_TIMER_BEGIN("gpr_zalloc", 0);
+ p = g_alloc_functions.zalloc_fn(size);
+ if (!p) {
+ abort();
+ }
+ GPR_TIMER_END("gpr_zalloc", 0);
+ return p;
+}
+
void gpr_free(void *p) {
GPR_TIMER_BEGIN("gpr_free", 0);
g_alloc_functions.free_fn(p);
diff --git a/src/core/lib/support/cmdline.c b/src/core/lib/support/cmdline.c
index d47498676d..88a65a8e2e 100644
--- a/src/core/lib/support/cmdline.c
+++ b/src/core/lib/support/cmdline.c
@@ -71,8 +71,7 @@ struct gpr_cmdline {
static int normal_state(gpr_cmdline *cl, char *arg);
gpr_cmdline *gpr_cmdline_create(const char *description) {
- gpr_cmdline *cl = gpr_malloc(sizeof(gpr_cmdline));
- memset(cl, 0, sizeof(gpr_cmdline));
+ gpr_cmdline *cl = gpr_zalloc(sizeof(gpr_cmdline));
cl->description = description;
cl->state = normal_state;
@@ -101,8 +100,7 @@ static void add_arg(gpr_cmdline *cl, const char *name, const char *help,
GPR_ASSERT(0 != strcmp(a->name, name));
}
- a = gpr_malloc(sizeof(arg));
- memset(a, 0, sizeof(arg));
+ a = gpr_zalloc(sizeof(arg));
a->name = name;
a->help = help;
a->type = type;
diff --git a/src/core/lib/support/histogram.c b/src/core/lib/support/histogram.c
index 7b016bbc78..ba8176bb05 100644
--- a/src/core/lib/support/histogram.c
+++ b/src/core/lib/support/histogram.c
@@ -102,8 +102,7 @@ gpr_histogram *gpr_histogram_create(double resolution,
h->num_buckets = bucket_for_unchecked(h, max_bucket_start) + 1;
GPR_ASSERT(h->num_buckets > 1);
GPR_ASSERT(h->num_buckets < 100000000);
- h->buckets = gpr_malloc(sizeof(uint32_t) * h->num_buckets);
- memset(h->buckets, 0, sizeof(uint32_t) * h->num_buckets);
+ h->buckets = gpr_zalloc(sizeof(uint32_t) * h->num_buckets);
return h;
}
diff --git a/src/core/lib/support/subprocess_posix.c b/src/core/lib/support/subprocess_posix.c
index 4247a1c12b..ed653b9c2e 100644
--- a/src/core/lib/support/subprocess_posix.c
+++ b/src/core/lib/support/subprocess_posix.c
@@ -76,8 +76,7 @@ gpr_subprocess *gpr_subprocess_create(int argc, const char **argv) {
_exit(1);
return NULL;
} else {
- r = gpr_malloc(sizeof(gpr_subprocess));
- memset(r, 0, sizeof(*r));
+ r = gpr_zalloc(sizeof(gpr_subprocess));
r->pid = pid;
return r;
}