Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <grpc/support/alloc.h> |
| 20 | |
David Garcia Quintas | f80b536 | 2015-12-09 11:34:51 -0800 | [diff] [blame] | 21 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 22 | #include <grpc/support/port_platform.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 23 | #include <stdlib.h> |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 24 | #include <string.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 25 | #include "src/core/lib/profiling/timers.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 26 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 27 | static void* zalloc_with_calloc(size_t sz) { return calloc(sz, 1); } |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 28 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 29 | static void* zalloc_with_gpr_malloc(size_t sz) { |
| 30 | void* p = gpr_malloc(sz); |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 31 | memset(p, 0, sz); |
| 32 | return p; |
| 33 | } |
| 34 | |
| 35 | static gpr_allocation_functions g_alloc_functions = {malloc, zalloc_with_calloc, |
| 36 | realloc, free}; |
David Garcia Quintas | f80b536 | 2015-12-09 11:34:51 -0800 | [diff] [blame] | 37 | |
David Garcia Quintas | a5aa19b | 2015-12-09 14:17:52 -0800 | [diff] [blame] | 38 | gpr_allocation_functions gpr_get_allocation_functions() { |
| 39 | return g_alloc_functions; |
David Garcia Quintas | f80b536 | 2015-12-09 11:34:51 -0800 | [diff] [blame] | 40 | } |
| 41 | |
David Garcia Quintas | a5aa19b | 2015-12-09 14:17:52 -0800 | [diff] [blame] | 42 | void 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 Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 46 | if (functions.zalloc_fn == NULL) { |
| 47 | functions.zalloc_fn = zalloc_with_gpr_malloc; |
| 48 | } |
David Garcia Quintas | a5aa19b | 2015-12-09 14:17:52 -0800 | [diff] [blame] | 49 | g_alloc_functions = functions; |
David Garcia Quintas | f80b536 | 2015-12-09 11:34:51 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 52 | void* gpr_malloc(size_t size) { |
| 53 | void* p; |
Nicolas "Pixel" Noble | 23e4bae | 2016-04-13 01:35:42 +0200 | [diff] [blame] | 54 | if (size == 0) return NULL; |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 55 | GPR_TIMER_BEGIN("gpr_malloc", 0); |
David Garcia Quintas | a5aa19b | 2015-12-09 14:17:52 -0800 | [diff] [blame] | 56 | p = g_alloc_functions.malloc_fn(size); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | if (!p) { |
| 58 | abort(); |
| 59 | } |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 60 | GPR_TIMER_END("gpr_malloc", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 61 | return p; |
| 62 | } |
| 63 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 64 | void* gpr_zalloc(size_t size) { |
| 65 | void* p; |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 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 Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 76 | void gpr_free(void* p) { |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 77 | GPR_TIMER_BEGIN("gpr_free", 0); |
David Garcia Quintas | a5aa19b | 2015-12-09 14:17:52 -0800 | [diff] [blame] | 78 | g_alloc_functions.free_fn(p); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 79 | GPR_TIMER_END("gpr_free", 0); |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 80 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 81 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 82 | void* gpr_realloc(void* p, size_t size) { |
Nicolas "Pixel" Noble | 23e4bae | 2016-04-13 01:35:42 +0200 | [diff] [blame] | 83 | if ((size == 0) && (p == NULL)) return NULL; |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 84 | GPR_TIMER_BEGIN("gpr_realloc", 0); |
David Garcia Quintas | a5aa19b | 2015-12-09 14:17:52 -0800 | [diff] [blame] | 85 | p = g_alloc_functions.realloc_fn(p, size); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 86 | if (!p) { |
| 87 | abort(); |
| 88 | } |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 89 | GPR_TIMER_END("gpr_realloc", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 90 | return p; |
| 91 | } |
| 92 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 93 | void* gpr_malloc_aligned(size_t size, size_t alignment_log) { |
zeliard | 81f750e | 2015-04-27 14:41:02 +0900 | [diff] [blame] | 94 | size_t alignment = ((size_t)1) << alignment_log; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 95 | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 98 | ret[-1] = p; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 99 | return (void*)ret; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 102 | void gpr_free_aligned(void* ptr) { gpr_free(((void**)ptr)[-1]); } |