File size: 2,199 Bytes
cc372ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// SPDX-FileCopyrightText: Hadad <hadad@linuxmail.org>
// SPDX-License-Identifier: Apache-2.0
//

#ifndef POCKET_TTS_MEMORY_POOL_HPP
#define POCKET_TTS_MEMORY_POOL_HPP

#include <atomic>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <mutex>
#include <unordered_map>
#include <vector>

namespace pocket_tts_accelerator {

struct MemoryBlock {
    std::unique_ptr<std::uint8_t[]> data;
    std::size_t block_size;
    bool is_in_use;
    std::uint64_t last_access_timestamp;
};

class MemoryPool {
public:
    explicit MemoryPool(std::size_t initial_pool_size_bytes = 64 * 1024 * 1024);
    ~MemoryPool();

    MemoryPool(const MemoryPool&) = delete;
    MemoryPool& operator=(const MemoryPool&) = delete;
    MemoryPool(MemoryPool&&) = delete;
    MemoryPool& operator=(MemoryPool&&) = delete;

    std::uint8_t* allocate(std::size_t requested_size_bytes);
    void deallocate(std::uint8_t* pointer);
    void clear_unused_blocks();
    void reset_pool();

    std::size_t get_total_allocated_bytes() const;
    std::size_t get_total_used_bytes() const;
    std::size_t get_block_count() const;

private:
    std::size_t find_suitable_block_index(std::size_t requested_size) const;
    void create_new_block(std::size_t block_size);
    std::uint64_t get_current_timestamp() const;

    std::vector<MemoryBlock> memory_blocks;
    std::unordered_map<std::uint8_t*, std::size_t> pointer_to_block_index;
    mutable std::mutex pool_mutex;
    std::size_t total_allocated_bytes;
    std::size_t total_used_bytes;
    std::size_t maximum_pool_size_bytes;
};

class ScopedMemoryAllocation {
public:
    ScopedMemoryAllocation(MemoryPool& pool, std::size_t size);
    ~ScopedMemoryAllocation();

    ScopedMemoryAllocation(const ScopedMemoryAllocation&) = delete;
    ScopedMemoryAllocation& operator=(const ScopedMemoryAllocation&) = delete;
    ScopedMemoryAllocation(ScopedMemoryAllocation&& other) noexcept;
    ScopedMemoryAllocation& operator=(ScopedMemoryAllocation&& other) noexcept;

    std::uint8_t* get() const;
    std::size_t size() const;

private:
    MemoryPool* memory_pool_pointer;
    std::uint8_t* allocated_pointer;
    std::size_t allocation_size;
};

}

#endif