LCOV - code coverage report
Current view: top level - src - memory.c (source / functions) Coverage Total Hit
Test: pino Coverage Report Lines: 100.0 % 48 48
Test Date: 2025-10-24 02:38:25 Functions: 100.0 % 6 6
Legend: Lines: hit not hit

            Line data    Source code
       1              : /*
       2              :  * libpino - memory.c
       3              :  * 
       4              :  */
       5              : 
       6              : #include <pino.h>
       7              : #include <pino/handler.h>
       8              : 
       9              : #include <pino_internal.h>
      10              : 
      11          248 : static inline bool glow_mm(mm_t *mm, size_t step)
      12              : {
      13              :     void **ptrs;
      14              :     size_t i;
      15              : 
      16          248 :     ptrs = (void **)prealloc(mm->ptrs, (mm->capacity + step) * sizeof(void *));
      17              :     /* LCOV_EXCL_START */
      18              :     if (!ptrs) {
      19              :         PINO_SUPRTF("prealloc failed");
      20              :         return false;
      21              :     }
      22              :     /* LCOV_EXCL_STOP */
      23              : 
      24         2232 :     for (i = mm->capacity; i < mm->capacity + step; i++) {
      25         1984 :         ptrs[i] = NULL;
      26              :     }
      27              : 
      28          248 :     mm->ptrs = ptrs;
      29          248 :     mm->capacity += step;
      30              : 
      31              :     PINO_SUPRTF("glowed capacity: %zu, usage: %zu", mm->capacity, mm->usage);
      32              : 
      33          248 :     return true;
      34              : }
      35              : 
      36         1026 : extern bool pino_memory_manager_obj_init(mm_t *mm, size_t initialize_size)
      37              : {
      38              :     /* LCOV_EXCL_START */
      39              :     if (initialize_size == 0) {
      40              :         return false;
      41              :     }
      42              :     /* LCOV_EXCL_STOP */
      43              : 
      44         1026 :     mm->usage = 0;
      45         1026 :     mm->capacity = 0;
      46         1026 :     mm->ptrs = (void **)pcalloc(initialize_size, sizeof(void *));
      47              :     /* LCOV_EXCL_START */
      48              :     if (!mm->ptrs) {
      49              :         return false;
      50              :     }
      51              :     /* LCOV_EXCL_STOP */
      52              : 
      53         1026 :     mm->usage = 0;
      54         1026 :     mm->capacity = initialize_size;
      55              : 
      56              :     PINO_SUPRTF("usage: %zu, capacity: %zu", mm->usage, mm->capacity);
      57              : 
      58         1026 :     return mm;
      59              : }
      60              : 
      61         1026 : extern void pino_memory_manager_obj_free(mm_t *mm)
      62              : {
      63              :     size_t i;
      64              : 
      65         1026 :     if (!mm) {
      66              :         return; /* LCOV_EXCL_LINE */
      67              :     }
      68              : 
      69        19426 :     for (i = 0; i < mm->capacity; i++) {
      70        18400 :         if (mm->ptrs[i]) {
      71         1006 :             pfree(mm->ptrs[i]);
      72         1006 :             mm->ptrs[i] = NULL;
      73         1006 :             --mm->usage;
      74              : 
      75              :             PINO_SUPRTF("freeing: %zu, usage: %zu, capacity: %zu", i, mm->usage, mm->capacity);
      76              :         }
      77              :     }
      78              : 
      79         1026 :     pfree(mm->ptrs);
      80         1026 :     mm->ptrs = NULL;
      81         1026 :     mm->usage = 0;
      82         1026 :     mm->capacity = 0;
      83              : }
      84              : 
      85         4024 : extern void *pino_memory_manager_malloc(/* handler_entry_t */ void *entry, size_t size)
      86              : {
      87              :     size_t i;
      88              : 
      89         4024 :     if (!entry || size == 0) {
      90              :         PINO_SUPRTF("entry or size is NULL");
      91         1006 :         return NULL;
      92              :     }
      93              : 
      94         3018 :     if (((handler_entry_t *)entry)->mm.usage >= ((handler_entry_t *)entry)->mm.capacity) {
      95              :         /* LCOV_EXCL_START */
      96              :         if (!glow_mm(&((handler_entry_t *)entry)->mm, HANDLER_STEP)) {
      97              :             PINO_SUPRTF("glow_mm failed");
      98              :             return NULL;
      99              :         }
     100              :         /* LCOV_EXCL_STOP */
     101              :     }
     102              : 
     103      3002042 :     for (i = 0; i < ((handler_entry_t *)entry)->mm.capacity; i++) {
     104      3002042 :         if (!((handler_entry_t *)entry)->mm.ptrs[i]) {
     105         3018 :             ((handler_entry_t *)entry)->mm.ptrs[i] = pmalloc(size);
     106              :             /* LCOV_EXCL_START */
     107              :             if (!((handler_entry_t *)entry)->mm.ptrs[i]) {
     108              :                 PINO_SUPRTF("pmalloc failed");
     109              :                 return NULL;
     110              :             }
     111              :             /* LCOV_EXCL_STOP */
     112              : 
     113         3018 :             ++((handler_entry_t *)entry)->mm.usage;
     114              : 
     115              :             PINO_SUPRTF("magic: %.4s, using: %zu, usage: %zu, capacity: %zu",
     116              :                 ((handler_entry_t *)entry)->magic,
     117              :                 i,
     118              :                 ((handler_entry_t *)entry)->mm.usage,
     119              :                 ((handler_entry_t *)entry)->mm.capacity
     120              :             );
     121              : 
     122         3018 :             return ((handler_entry_t *)entry)->mm.ptrs[i];
     123              :         }
     124              :     }
     125              :     /* LCOV_EXCL_START */
     126              :     PINO_SUPUNREACH();
     127              :     return NULL;
     128              :     /* LCOV_EXCL_STOP */
     129              : }
     130              : 
     131         2012 : extern void *pino_memory_manager_calloc(/* handler_entry_t */ void *entry, size_t count, size_t size)
     132              : {
     133              :     void *ptr;
     134              : 
     135         2012 :     ptr = pino_memory_manager_malloc(entry, count * size);
     136              :     /* LCOV_EXCL_START */
     137              :     if (!ptr) {
     138              :         PINO_SUPRTF("pino_memory_manager_malloc failed");
     139              :         return NULL;
     140              :     }
     141              :     /* LCOV_EXCL_STOP */
     142              : 
     143         2012 :     memset(ptr, 0, count * size);
     144              : 
     145         2012 :     return ptr;
     146              : }
     147              : 
     148         3018 : extern void pino_memory_manager_free(/* handler_entry_t */ void *entry, void *ptr)
     149              : {
     150              :     size_t i;
     151              : 
     152         3018 :     if (!entry || !ptr) {
     153              :         PINO_SUPRTF("mm or ptr is NULL");
     154         1006 :         return;
     155              :     }
     156              : 
     157      2002032 :     for (i = 0; i < ((handler_entry_t *)entry)->mm.capacity; i++) {
     158      2002032 :         if (((handler_entry_t *)entry)->mm.ptrs[i] && ((handler_entry_t *)entry)->mm.ptrs[i] == ptr) {
     159         2012 :             pfree(ptr);
     160         2012 :             ((handler_entry_t *)entry)->mm.ptrs[i] = NULL;
     161         2012 :             ptr = NULL;
     162         2012 :             --((handler_entry_t *)entry)->mm.usage;
     163              : 
     164              :             PINO_SUPRTF("freeing: %zu, usage: %zu, capacity: %zu",
     165              :                 i, ((handler_entry_t *)entry)->mm.usage, ((handler_entry_t *)entry)->mm.capacity);
     166              : 
     167         2012 :             return;
     168              :         }
     169              :     }
     170              : 
     171              :     /* LCOV_EXCL_START */
     172              :     PINO_SUPUNREACH();
     173              :     return;
     174              :     /* LCOV_EXCL_STOP */
     175              : }
        

Generated by: LCOV version 2.0-1