blob: 518bdb99f74a0661f9e291fbf754e7a45e819a0d [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 Tillerbaa14a92017-11-03 09:09:36 -070027static void* zalloc_with_calloc(size_t sz) { return calloc(sz, 1); }
Craig Tiller6f417882017-02-16 14:09:39 -080028
Craig Tillerbaa14a92017-11-03 09:09:36 -070029static void* zalloc_with_gpr_malloc(size_t sz) {
30 void* p = gpr_malloc(sz);
Craig Tiller6f417882017-02-16 14:09:39 -080031 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) {
Craig Tiller4782d922017-11-10 09:53:21 -080043 GPR_ASSERT(functions.malloc_fn != nullptr);
44 GPR_ASSERT(functions.realloc_fn != nullptr);
45 GPR_ASSERT(functions.free_fn != nullptr);
46 if (functions.zalloc_fn == nullptr) {
Craig Tiller6f417882017-02-16 14:09:39 -080047 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
Craig Tillerbaa14a92017-11-03 09:09:36 -070052void* gpr_malloc(size_t size) {
53 void* p;
Craig Tiller4782d922017-11-10 09:53:21 -080054 if (size == 0) return nullptr;
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 Tillerbaa14a92017-11-03 09:09:36 -070064void* gpr_zalloc(size_t size) {
65 void* p;
Craig Tiller4782d922017-11-10 09:53:21 -080066 if (size == 0) return nullptr;
Craig Tiller6f417882017-02-16 14:09:39 -080067 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 Tillerbaa14a92017-11-03 09:09:36 -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
Craig Tillerbaa14a92017-11-03 09:09:36 -070082void* gpr_realloc(void* p, size_t size) {
Craig Tiller4782d922017-11-10 09:53:21 -080083 if ((size == 0) && (p == nullptr)) return nullptr;
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
Craig Tillerbaa14a92017-11-03 09:09:36 -070093void* 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;
Craig Tillerbaa14a92017-11-03 09:09:36 -070095 size_t extra = alignment - 1 + sizeof(void*);
96 void* p = gpr_malloc(size + extra);
97 void** ret = (void**)(((uintptr_t)p + extra) & ~(alignment - 1));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080098 ret[-1] = p;
Craig Tillerbaa14a92017-11-03 09:09:36 -070099 return (void*)ret;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800100}
101
Craig Tillerbaa14a92017-11-03 09:09:36 -0700102void gpr_free_aligned(void* ptr) { gpr_free(((void**)ptr)[-1]); }