3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
27 #include "src/core/lib/profiling/timers.h"
29 void* gpr_malloc(size_t size) {
30 GPR_TIMER_SCOPE("gpr_malloc", 0);
32 if (size == 0) return nullptr;
40 void* gpr_zalloc(size_t size) {
41 GPR_TIMER_SCOPE("gpr_zalloc", 0);
43 if (size == 0) return nullptr;
51 void gpr_free(void* p) {
52 GPR_TIMER_SCOPE("gpr_free", 0);
56 void* gpr_realloc(void* p, size_t size) {
57 GPR_TIMER_SCOPE("gpr_realloc", 0);
58 if ((size == 0) && (p == nullptr)) return nullptr;
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));
76 void gpr_free_aligned(void* ptr) { gpr_free((static_cast<void**>(ptr))[-1]); }