LCOV - code coverage report
Current view: top level - os_stub/cryptlib_mbedtls/sys_call - mem_allocation.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 93.3 % 15 14
Test Date: 2025-06-29 08:09:00 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /**
       2              :  *  Copyright Notice:
       3              :  *  Copyright 2021-2022 DMTF. All rights reserved.
       4              :  *  License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
       5              :  **/
       6              : 
       7              : /** @file
       8              :  * Base Memory Allocation Routines Wrapper.
       9              :  **/
      10              : 
      11              : #include <base.h>
      12              : #include "library/debuglib.h"
      13              : #include "library/malloclib.h"
      14              : #include <stddef.h>
      15              : 
      16              : 
      17              : /* Extra header to record the memory buffer size from malloc routine.*/
      18              : 
      19              : #define CRYPTMEM_HEAD_VERSION 0x1
      20              : typedef struct {
      21              :     uint32_t version;
      22              :     uint32_t reserved;
      23              :     size_t size;
      24              : } CRYPTMEM_HEAD;
      25              : 
      26              : #define CRYPTMEM_OVERHEAD sizeof(CRYPTMEM_HEAD)
      27              : 
      28              : 
      29              : /* -- Memory-Allocation Routines --*/
      30              : 
      31              : 
      32              : /* Allocates memory blocks */
      33      2684027 : void *my_calloc(size_t num, size_t size)
      34              : {
      35              :     CRYPTMEM_HEAD *pool_hdr;
      36              :     size_t new_size;
      37              :     void *data;
      38              : 
      39              : 
      40              :     /* Adjust the size by the buffer header overhead*/
      41              : 
      42      2684027 :     new_size = (size_t)(size * num) + CRYPTMEM_OVERHEAD;
      43              : 
      44      2684027 :     data = allocate_zero_pool(new_size);
      45      2684027 :     if (data != NULL) {
      46      2684027 :         pool_hdr = (CRYPTMEM_HEAD *)data;
      47              : 
      48              :         /* Record the memory brief information*/
      49              : 
      50      2684027 :         pool_hdr->version = CRYPTMEM_HEAD_VERSION;
      51      2684027 :         pool_hdr->size = size;
      52              : 
      53      2684027 :         return (void *)(pool_hdr + 1);
      54              :     } else {
      55              : 
      56              :         /* The buffer allocation failed.*/
      57              : 
      58            0 :         return NULL;
      59              :     }
      60              : }
      61              : 
      62              : /* De-allocates or frees a memory block */
      63      2698247 : void my_free(void *ptr)
      64              : {
      65              :     CRYPTMEM_HEAD *pool_hdr;
      66              : 
      67              : 
      68              :     /* In Standard C, free() handles a null pointer argument transparently. This
      69              :      * is not true of free_pool() below, so protect it.*/
      70              : 
      71      2698247 :     if (ptr != NULL) {
      72      2683200 :         pool_hdr = (CRYPTMEM_HEAD *)ptr - 1;
      73      2683200 :         LIBSPDM_ASSERT(pool_hdr->version == CRYPTMEM_HEAD_VERSION);
      74      2683200 :         free_pool(pool_hdr);
      75              :     }
      76      2698247 : }
        

Generated by: LCOV version 2.0-1