• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include <grpc/support/alloc.h>
22 
23 #include <grpc/support/log.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "src/core/lib/profiling/timers.h"
27 
zalloc_with_calloc(size_t sz)28 static void* zalloc_with_calloc(size_t sz) { return calloc(sz, 1); }
29 
zalloc_with_gpr_malloc(size_t sz)30 static void* zalloc_with_gpr_malloc(size_t sz) {
31   void* p = gpr_malloc(sz);
32   memset(p, 0, sz);
33   return p;
34 }
35 
36 static gpr_allocation_functions g_alloc_functions = {malloc, zalloc_with_calloc,
37                                                      realloc, free};
38 
gpr_get_allocation_functions()39 gpr_allocation_functions gpr_get_allocation_functions() {
40   return g_alloc_functions;
41 }
42 
gpr_set_allocation_functions(gpr_allocation_functions functions)43 void gpr_set_allocation_functions(gpr_allocation_functions functions) {
44   GPR_ASSERT(functions.malloc_fn != nullptr);
45   GPR_ASSERT(functions.realloc_fn != nullptr);
46   GPR_ASSERT(functions.free_fn != nullptr);
47   if (functions.zalloc_fn == nullptr) {
48     functions.zalloc_fn = zalloc_with_gpr_malloc;
49   }
50   g_alloc_functions = functions;
51 }
52 
gpr_malloc(size_t size)53 void* gpr_malloc(size_t size) {
54   GPR_TIMER_SCOPE("gpr_malloc", 0);
55   void* p;
56   if (size == 0) return nullptr;
57   p = g_alloc_functions.malloc_fn(size);
58   if (!p) {
59     abort();
60   }
61   return p;
62 }
63 
gpr_zalloc(size_t size)64 void* gpr_zalloc(size_t size) {
65   GPR_TIMER_SCOPE("gpr_zalloc", 0);
66   void* p;
67   if (size == 0) return nullptr;
68   p = g_alloc_functions.zalloc_fn(size);
69   if (!p) {
70     abort();
71   }
72   return p;
73 }
74 
gpr_free(void * p)75 void gpr_free(void* p) {
76   GPR_TIMER_SCOPE("gpr_free", 0);
77   g_alloc_functions.free_fn(p);
78 }
79 
gpr_realloc(void * p,size_t size)80 void* gpr_realloc(void* p, size_t size) {
81   GPR_TIMER_SCOPE("gpr_realloc", 0);
82   if ((size == 0) && (p == nullptr)) return nullptr;
83   p = g_alloc_functions.realloc_fn(p, size);
84   if (!p) {
85     abort();
86   }
87   return p;
88 }
89 
gpr_malloc_aligned(size_t size,size_t alignment)90 void* gpr_malloc_aligned(size_t size, size_t alignment) {
91   GPR_ASSERT(((alignment - 1) & alignment) == 0);  // Must be power of 2.
92   size_t extra = alignment - 1 + sizeof(void*);
93   void* p = gpr_malloc(size + extra);
94   void** ret = (void**)(((uintptr_t)p + extra) & ~(alignment - 1));
95   ret[-1] = p;
96   return (void*)ret;
97 }
98 
gpr_free_aligned(void * ptr)99 void gpr_free_aligned(void* ptr) { gpr_free((static_cast<void**>(ptr))[-1]); }
100