blob: 886d69d64c31541fd768581c40594772f86bec3b [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
19#include <grpc/support/alloc.h>
20
David Garcia Quintasf80b5362015-12-09 11:34:51 -080021#include <grpc/support/log.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080022#include <grpc/support/port_platform.h>
Craig Tillerf40df232016-03-25 13:38:14 -070023#include <stdlib.h>
Craig Tiller6f417882017-02-16 14:09:39 -080024#include <string.h>
Craig Tiller9533d042016-03-25 17:11:06 -070025#include "src/core/lib/profiling/timers.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080026
Craig Tiller6f417882017-02-16 14:09:39 -080027static void *zalloc_with_calloc(size_t sz) { return calloc(sz, 1); }
28
29static void *zalloc_with_gpr_malloc(size_t sz) {
30 void *p = gpr_malloc(sz);
31 memset(p, 0, sz);
32 return p;
33}
34
35static gpr_allocation_functions g_alloc_functions = {malloc, zalloc_with_calloc,
36 realloc, free};
David Garcia Quintasf80b5362015-12-09 11:34:51 -080037
David Garcia Quintasa5aa19b2015-12-09 14:17:52 -080038gpr_allocation_functions gpr_get_allocation_functions() {
39 return g_alloc_functions;
David Garcia Quintasf80b5362015-12-09 11:34:51 -080040}
41
David Garcia Quintasa5aa19b2015-12-09 14:17:52 -080042void gpr_set_allocation_functions(gpr_allocation_functions functions) {
43 GPR_ASSERT(functions.malloc_fn != NULL);
44 GPR_ASSERT(functions.realloc_fn != NULL);
45 GPR_ASSERT(functions.free_fn != NULL);
Craig Tiller6f417882017-02-16 14:09:39 -080046 if (functions.zalloc_fn == NULL) {
47 functions.zalloc_fn = zalloc_with_gpr_malloc;
48 }
David Garcia Quintasa5aa19b2015-12-09 14:17:52 -080049 g_alloc_functions = functions;
David Garcia Quintasf80b5362015-12-09 11:34:51 -080050}
51
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052void *gpr_malloc(size_t size) {
Craig Tiller8910ac62015-10-08 16:49:15 -070053 void *p;
Nicolas "Pixel" Noble23e4bae2016-04-13 01:35:42 +020054 if (size == 0) return NULL;
Craig Tiller0ba432d2015-10-09 16:57:11 -070055 GPR_TIMER_BEGIN("gpr_malloc", 0);
David Garcia Quintasa5aa19b2015-12-09 14:17:52 -080056 p = g_alloc_functions.malloc_fn(size);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080057 if (!p) {
58 abort();
59 }
Craig Tiller0ba432d2015-10-09 16:57:11 -070060 GPR_TIMER_END("gpr_malloc", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061 return p;
62}
63
Craig Tiller6f417882017-02-16 14:09:39 -080064void *gpr_zalloc(size_t size) {
65 void *p;
66 if (size == 0) return NULL;
67 GPR_TIMER_BEGIN("gpr_zalloc", 0);
68 p = g_alloc_functions.zalloc_fn(size);
69 if (!p) {
70 abort();
71 }
72 GPR_TIMER_END("gpr_zalloc", 0);
73 return p;
74}
75
Craig Tiller1f41b6b2015-10-09 15:07:02 -070076void gpr_free(void *p) {
Craig Tiller0ba432d2015-10-09 16:57:11 -070077 GPR_TIMER_BEGIN("gpr_free", 0);
David Garcia Quintasa5aa19b2015-12-09 14:17:52 -080078 g_alloc_functions.free_fn(p);
Craig Tiller0ba432d2015-10-09 16:57:11 -070079 GPR_TIMER_END("gpr_free", 0);
Craig Tiller8910ac62015-10-08 16:49:15 -070080}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081
82void *gpr_realloc(void *p, size_t size) {
Nicolas "Pixel" Noble23e4bae2016-04-13 01:35:42 +020083 if ((size == 0) && (p == NULL)) return NULL;
Craig Tiller0ba432d2015-10-09 16:57:11 -070084 GPR_TIMER_BEGIN("gpr_realloc", 0);
David Garcia Quintasa5aa19b2015-12-09 14:17:52 -080085 p = g_alloc_functions.realloc_fn(p, size);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080086 if (!p) {
87 abort();
88 }
Craig Tiller0ba432d2015-10-09 16:57:11 -070089 GPR_TIMER_END("gpr_realloc", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080090 return p;
91}
92
Nicolas Noble085603c2015-02-24 13:29:29 -080093void *gpr_malloc_aligned(size_t size, size_t alignment_log) {
zeliard81f750e2015-04-27 14:41:02 +090094 size_t alignment = ((size_t)1) << alignment_log;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080095 size_t extra = alignment - 1 + sizeof(void *);
96 void *p = gpr_malloc(size + extra);
Craig Tiller7536af02015-12-22 13:49:30 -080097 void **ret = (void **)(((uintptr_t)p + extra) & ~(alignment - 1));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080098 ret[-1] = p;
99 return (void *)ret;
100}
101
Paddy Byers9db72682016-03-10 22:56:15 +0000102void gpr_free_aligned(void *ptr) { gpr_free(((void **)ptr)[-1]); }