LCOV - code coverage report
Current view: top level - core/support - alloc.c (source / functions) Hit Total Coverage
Test: tmp.CaZ6RjdVn2 Lines: 27 29 93.1 %
Date: 2015-12-10 22:15:08 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are
       8             :  * met:
       9             :  *
      10             :  *     * Redistributions of source code must retain the above copyright
      11             :  * notice, this list of conditions and the following disclaimer.
      12             :  *     * Redistributions in binary form must reproduce the above
      13             :  * copyright notice, this list of conditions and the following disclaimer
      14             :  * in the documentation and/or other materials provided with the
      15             :  * distribution.
      16             :  *     * Neither the name of Google Inc. nor the names of its
      17             :  * contributors may be used to endorse or promote products derived from
      18             :  * this software without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      21             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      24             :  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      25             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      26             :  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      27             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      28             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      29             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      30             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      31             :  *
      32             :  */
      33             : 
      34             : #include <grpc/support/alloc.h>
      35             : 
      36             : #include <stdlib.h>
      37             : #include <grpc/support/log.h>
      38             : #include <grpc/support/port_platform.h>
      39             : #include "src/core/profiling/timers.h"
      40             : 
      41             : static gpr_allocation_functions g_alloc_functions = {malloc, realloc, free};
      42             : 
      43           1 : gpr_allocation_functions gpr_get_allocation_functions() {
      44           1 :   return g_alloc_functions;
      45             : }
      46             : 
      47           2 : void gpr_set_allocation_functions(gpr_allocation_functions functions) {
      48           2 :   GPR_ASSERT(functions.malloc_fn != NULL);
      49           2 :   GPR_ASSERT(functions.realloc_fn != NULL);
      50           2 :   GPR_ASSERT(functions.free_fn != NULL);
      51           2 :   g_alloc_functions = functions;
      52           2 : }
      53             : 
      54    39247097 : void *gpr_malloc(size_t size) {
      55             :   void *p;
      56             :   GPR_TIMER_BEGIN("gpr_malloc", 0);
      57    39247097 :   p = g_alloc_functions.malloc_fn(size);
      58    39285954 :   if (!p) {
      59           0 :     abort();
      60             :   }
      61             :   GPR_TIMER_END("gpr_malloc", 0);
      62    39285954 :   return p;
      63             : }
      64             : 
      65    77676249 : void gpr_free(void *p) {
      66             :   GPR_TIMER_BEGIN("gpr_free", 0);
      67    77676249 :   g_alloc_functions.free_fn(p);
      68             :   GPR_TIMER_END("gpr_free", 0);
      69    77964259 : }
      70             : 
      71    14120670 : void *gpr_realloc(void *p, size_t size) {
      72             :   GPR_TIMER_BEGIN("gpr_realloc", 0);
      73    14120670 :   p = g_alloc_functions.realloc_fn(p, size);
      74    14127465 :   if (!p) {
      75           0 :     abort();
      76             :   }
      77             :   GPR_TIMER_END("gpr_realloc", 0);
      78    14127465 :   return p;
      79             : }
      80             : 
      81        8575 : void *gpr_malloc_aligned(size_t size, size_t alignment_log) {
      82        8575 :   size_t alignment = ((size_t)1) << alignment_log;
      83        8575 :   size_t extra = alignment - 1 + sizeof(void *);
      84        8575 :   void *p = gpr_malloc(size + extra);
      85        8575 :   void **ret = (void **)(((gpr_uintptr)p + extra) & ~(alignment - 1));
      86        8575 :   ret[-1] = p;
      87        8575 :   return (void *)ret;
      88             : }
      89             : 
      90        8495 : void gpr_free_aligned(void *ptr) { free(((void **)ptr)[-1]); }

Generated by: LCOV version 1.11