alloc.cc
Go to the documentation of this file.
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 
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 
28 
29 void* gpr_malloc(size_t size) {
30  GPR_TIMER_SCOPE("gpr_malloc", 0);
31  void* p;
32  if (size == 0) return nullptr;
33  p = malloc(size);
34  if (!p) {
35  abort();
36  }
37  return p;
38 }
39 
40 void* gpr_zalloc(size_t size) {
41  GPR_TIMER_SCOPE("gpr_zalloc", 0);
42  void* p;
43  if (size == 0) return nullptr;
44  p = calloc(size, 1);
45  if (!p) {
46  abort();
47  }
48  return p;
49 }
50 
51 void gpr_free(void* p) {
52  GPR_TIMER_SCOPE("gpr_free", 0);
53  free(p);
54 }
55 
56 void* gpr_realloc(void* p, size_t size) {
57  GPR_TIMER_SCOPE("gpr_realloc", 0);
58  if ((size == 0) && (p == nullptr)) return nullptr;
59  p = realloc(p, size);
60  if (!p) {
61  abort();
62  }
63  return p;
64 }
65 
66 void* gpr_malloc_aligned(size_t size, size_t alignment) {
67  GPR_ASSERT(((alignment - 1) & alignment) == 0); // Must be power of 2.
68  size_t extra = alignment - 1 + sizeof(void*);
69  void* p = gpr_malloc(size + extra);
70  void** ret = reinterpret_cast<void**>(
71  (reinterpret_cast<uintptr_t>(p) + extra) & ~(alignment - 1));
72  ret[-1] = p;
73  return ret;
74 }
75 
76 void gpr_free_aligned(void* ptr) { gpr_free((static_cast<void**>(ptr))[-1]); }
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
log.h
timers.h
GPR_TIMER_SCOPE
#define GPR_TIMER_SCOPE(tag, important)
Definition: src/core/lib/profiling/timers.h:43
string.h
xds_manager.p
p
Definition: xds_manager.py:60
gpr_malloc
void * gpr_malloc(size_t size)
Definition: alloc.cc:29
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
uintptr_t
_W64 unsigned int uintptr_t
Definition: stdint-msvc2008.h:119
gpr_zalloc
void * gpr_zalloc(size_t size)
Definition: alloc.cc:40
alignment
static const std::size_t alignment
Definition: cxa_demangle.cpp:4721
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
alloc.h
gpr_free_aligned
void gpr_free_aligned(void *ptr)
Definition: alloc.cc:76
gpr_malloc_aligned
void * gpr_malloc_aligned(size_t size, size_t alignment)
Definition: alloc.cc:66
gpr_realloc
void * gpr_realloc(void *p, size_t size)
Definition: alloc.cc:56
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
gpr_free
void gpr_free(void *p)
Definition: alloc.cc:51
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:30