LCOV - code coverage report
Current view: top level - lib/acl - tb_mem.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 28 30 93.3 %
Date: 2025-02-01 18:54:23 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 7 8 87.5 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2014 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include "tb_mem.h"
       6                 :            : #include "acl_log.h"
       7                 :            : 
       8                 :            : /*
       9                 :            :  *  Memory management routines for temporary memory.
      10                 :            :  *  That memory is used only during build phase and is released after
      11                 :            :  *  build is finished.
      12                 :            :  *  Note, that tb_pool/tb_alloc() are not supposed to return NULL.
      13                 :            :  *  Instead, in the case of failure to allocate memory,
      14                 :            :  *  it would do siglongjmp(pool->fail).
      15                 :            :  *  It is responsibility of the caller to save the proper context/environment,
      16                 :            :  *  in the pool->fail before calling tb_alloc() for the given pool first time.
      17                 :            :  */
      18                 :            : 
      19                 :            : static struct tb_mem_block *
      20                 :         98 : tb_pool(struct tb_mem_pool *pool, size_t sz)
      21                 :            : {
      22                 :            :         struct tb_mem_block *block;
      23                 :            :         uint8_t *ptr;
      24                 :            :         size_t size;
      25                 :            : 
      26                 :         98 :         size = sz + pool->alignment - 1;
      27                 :         98 :         block = calloc(1, size + sizeof(*pool->block));
      28         [ -  + ]:         98 :         if (block == NULL) {
      29                 :          0 :                 ACL_LOG(ERR, "%s(%zu) failed, currently allocated by pool: %zu bytes",
      30                 :            :                         __func__, sz, pool->alloc);
      31                 :          0 :                 siglongjmp(pool->fail, -ENOMEM);
      32                 :            :                 return NULL;
      33                 :            :         }
      34                 :            : 
      35                 :         98 :         block->pool = pool;
      36                 :            : 
      37                 :         98 :         block->next = pool->block;
      38                 :         98 :         pool->block = block;
      39                 :            : 
      40                 :         98 :         pool->alloc += size;
      41                 :            : 
      42                 :         98 :         ptr = (uint8_t *)(block + 1);
      43                 :         98 :         block->mem = RTE_PTR_ALIGN_CEIL(ptr, pool->alignment);
      44                 :         98 :         block->size = size - (block->mem - ptr);
      45                 :            : 
      46                 :            :         return block;
      47                 :            : }
      48                 :            : 
      49                 :            : void *
      50                 :    1264228 : tb_alloc(struct tb_mem_pool *pool, size_t size)
      51                 :            : {
      52                 :            :         struct tb_mem_block *block;
      53                 :            :         void *ptr;
      54                 :            :         size_t new_sz;
      55                 :            : 
      56                 :    1264228 :         size = RTE_ALIGN_CEIL(size, pool->alignment);
      57                 :            : 
      58                 :    1264228 :         block = pool->block;
      59   [ +  +  +  + ]:    1264228 :         if (block == NULL || block->size < size) {
      60                 :         98 :                 new_sz = (size > pool->min_alloc) ? size : pool->min_alloc;
      61                 :         98 :                 block = tb_pool(pool, new_sz);
      62                 :            :         }
      63                 :    1264228 :         ptr = block->mem;
      64                 :    1264228 :         block->size -= size;
      65                 :    1264228 :         block->mem += size;
      66                 :    1264228 :         return ptr;
      67                 :            : }
      68                 :            : 
      69                 :            : void
      70                 :         47 : tb_free_pool(struct tb_mem_pool *pool)
      71                 :            : {
      72                 :            :         struct tb_mem_block *next, *block;
      73                 :            : 
      74         [ +  + ]:        145 :         for (block = pool->block; block != NULL; block = next) {
      75                 :         98 :                 next = block->next;
      76                 :         98 :                 free(block);
      77                 :            :         }
      78                 :         47 :         pool->block = NULL;
      79                 :         47 :         pool->alloc = 0;
      80                 :         47 : }

Generated by: LCOV version 1.14