LCOV - code coverage report
Current view: top level - include/hpactor/mem - segment_provider.hpp (source / functions) Coverage Total Hit
Test: HPActor Coverage Lines: 100.0 % 6 6
Test Date: 2026-05-20 02:24:49 Functions: 100.0 % 3 3
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright 2026 HPActor Contributors
       2                 :             : //
       3                 :             : // Licensed under the Apache License, Version 2.0 (the "License");
       4                 :             : // you may not use this file except in compliance with the License.
       5                 :             : // You may obtain a copy of the License at
       6                 :             : //
       7                 :             : //     http://www.apache.org/licenses/LICENSE-2.0
       8                 :             : //
       9                 :             : // Unless required by applicable law or agreed to in writing, software
      10                 :             : // distributed under the License is distributed on an "AS IS" BASIS,
      11                 :             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12                 :             : // See the License for the specific language governing permissions and
      13                 :             : // limitations under the License.
      14                 :             : 
      15                 :             : #pragma once
      16                 :             : 
      17                 :             : #include <hpactor/mem/size_class.hpp>
      18                 :             : 
      19                 :             : #include <cstddef>
      20                 :             : #include <cstdint>
      21                 :             : #include <mutex>
      22                 :             : #include <unordered_map>
      23                 :             : #include <vector>
      24                 :             : 
      25                 :             : namespace hpactor::mem {
      26                 :             : 
      27                 :             : // Tier 0: Global segment provider. Acquires large mmap'd regions and carves
      28                 :             : // them into slabs for thread-local caches. Thread-safe.
      29                 :             : class SegmentProvider {
      30                 :             :   public:
      31                 :             :     struct SegmentInfo {
      32                 :             :         void* base;
      33                 :             :         size_t size;
      34                 :             :     };
      35                 :             : 
      36                 :             :     struct Stats {
      37                 :             :         size_t total_allocated{0};
      38                 :             :         size_t active_segments{0};
      39                 :             :     };
      40                 :             : 
      41                 :             :     static SegmentProvider& instance();
      42                 :             : 
      43                 :             :     // Acquire a slab of the given size class. Returns base pointer.
      44                 :             :     void* acquire_slab(SizeClass sc);
      45                 :             : 
      46                 :             :     // Release a slab back. When all slabs in a segment are freed, munmap.
      47                 :             :     void release_slab(void* slab, SizeClass sc);
      48                 :             : 
      49                 :             :     // Look up which segment a pointer belongs to.
      50                 :             :     SegmentInfo lookup(void* ptr) const;
      51                 :             : 
      52                 :             :     // Size of a slab for a given size class.
      53                 :             :     size_t slab_size(SizeClass sc) const;
      54                 :             : 
      55                 :             :     Stats stats() const;
      56                 :             : 
      57                 :             :   private:
      58                 :           8 :     SegmentProvider() = default;
      59                 :             : 
      60                 :             :     static constexpr size_t kSegmentSize = 2 * 1024 * 1024; // 2MB
      61                 :             :     static constexpr size_t kBaseSlabSize = 64 * 1024;      // 64KB default
      62                 :             : 
      63                 :             :     struct Segment {
      64                 :             :         void* base{nullptr};
      65                 :             :         size_t size{0};
      66                 :             :         size_t offset{0};
      67                 :             :         uint32_t ref_count{0}; // atomic not needed; protected by mutex_
      68                 :             : 
      69                 :        3024 :         void inc_ref() {
      70                 :        3024 :             ++ref_count;
      71                 :        3024 :         }
      72                 :        3556 :         uint32_t dec_ref() {
      73                 :        3556 :             return --ref_count;
      74                 :             :         }
      75                 :             :     };
      76                 :             : 
      77                 :             :     void* carve_from_segment(SizeClass sc);
      78                 :             :     void* allocate_new_segment(size_t size);
      79                 :             : 
      80                 :             :     mutable std::mutex mutex_;
      81                 :             :     std::vector<Segment> segments_;
      82                 :             :     std::unordered_map<void*, uint32_t> addr_to_segment_;
      83                 :             : };
      84                 :             : 
      85                 :             : } // namespace hpactor::mem
        

Generated by: LCOV version 2.0-1