LCOV - code coverage report
Current view: top level - girepository/cmph - buffer_manager.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 0.0 % 31 0
Test Date: 2024-11-26 05:23:01 Functions: 0.0 % 4 0
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : #include "buffer_manager.h"
       2                 :             : #include "buffer_entry.h"
       3                 :             : #include <stdio.h>
       4                 :             : #include <assert.h>
       5                 :             : #include <stdlib.h>
       6                 :             : struct __buffer_manager_t
       7                 :             : {
       8                 :             :         cmph_uint32 memory_avail;         // memory available
       9                 :             :         buffer_entry_t ** buffer_entries; // buffer entries to be managed
      10                 :             :         cmph_uint32 nentries;             // number of entries to be managed
      11                 :             :         cmph_uint32 *memory_avail_list;   // memory available list
      12                 :             :         int pos_avail_list;               // current position in memory available list
      13                 :             : };
      14                 :             : 
      15                 :           0 : buffer_manager_t * buffer_manager_new(cmph_uint32 memory_avail, cmph_uint32 nentries)
      16                 :             : {
      17                 :             :         cmph_uint32 memory_avail_entry, i;
      18                 :           0 :         buffer_manager_t *buff_manager = (buffer_manager_t *)malloc(sizeof(buffer_manager_t));
      19                 :           0 :         assert(buff_manager);
      20                 :           0 :         buff_manager->memory_avail = memory_avail;
      21                 :           0 :         buff_manager->buffer_entries = (buffer_entry_t **)calloc((size_t)nentries, sizeof(buffer_entry_t *));
      22                 :           0 :         buff_manager->memory_avail_list = (cmph_uint32 *)calloc((size_t)nentries, sizeof(cmph_uint32));
      23                 :           0 :         buff_manager->pos_avail_list = -1;
      24                 :           0 :         buff_manager->nentries = nentries;
      25                 :           0 :         memory_avail_entry = buff_manager->memory_avail/buff_manager->nentries + 1;
      26                 :           0 :         for(i = 0; i < buff_manager->nentries; i++)
      27                 :             :         {
      28                 :           0 :                 buff_manager->buffer_entries[i] = buffer_entry_new(memory_avail_entry);
      29                 :             :         }       
      30                 :           0 :         return buff_manager;
      31                 :             : }
      32                 :             : 
      33                 :           0 : void buffer_manager_open(buffer_manager_t * buffer_manager, cmph_uint32 index, char * filename)
      34                 :             : {
      35                 :           0 :         buffer_entry_open(buffer_manager->buffer_entries[index], filename);
      36                 :           0 : }
      37                 :             : 
      38                 :           0 : cmph_uint8 * buffer_manager_read_key(buffer_manager_t * buffer_manager, cmph_uint32 index, cmph_uint32 * keylen)
      39                 :             : {
      40                 :           0 :         cmph_uint8 * key = NULL;
      41                 :           0 :         if (buffer_manager->pos_avail_list >= 0 ) // recovering memory
      42                 :             :         {
      43                 :           0 :                 cmph_uint32 new_capacity = buffer_entry_get_capacity(buffer_manager->buffer_entries[index]) + buffer_manager->memory_avail_list[(buffer_manager->pos_avail_list)--];
      44                 :           0 :                 buffer_entry_set_capacity(buffer_manager->buffer_entries[index], new_capacity);
      45                 :             :         }
      46                 :           0 :         key = buffer_entry_read_key(buffer_manager->buffer_entries[index], keylen);
      47                 :           0 :         if (key == NULL) // storing memory to be recovered
      48                 :             :         {
      49                 :           0 :                 buffer_manager->memory_avail_list[++(buffer_manager->pos_avail_list)] = buffer_entry_get_capacity(buffer_manager->buffer_entries[index]);
      50                 :             :         }
      51                 :           0 :         return key;
      52                 :             : }
      53                 :             : 
      54                 :           0 : void buffer_manager_destroy(buffer_manager_t * buffer_manager)
      55                 :             : { 
      56                 :             :         cmph_uint32 i;
      57                 :           0 :         for(i = 0; i < buffer_manager->nentries; i++)
      58                 :             :         {
      59                 :           0 :                 buffer_entry_destroy(buffer_manager->buffer_entries[i]);
      60                 :             :         }
      61                 :           0 :         free(buffer_manager->memory_avail_list);
      62                 :           0 :         free(buffer_manager->buffer_entries);
      63                 :           0 :         free(buffer_manager);
      64                 :           0 : }
        

Generated by: LCOV version 2.0-1