allocator.hpp 1.27 KB
Newer Older
PanZezhong's avatar
PanZezhong committed
1
2
3
4
#ifndef ALLOCATOR_HPP
#define ALLOCATOR_HPP

#include "infinicore_infer.h"
thatPepe's avatar
thatPepe committed
5
6
7
8
#include <map>
#include <set>
#include <unordered_map>
#include <vector>
PanZezhong's avatar
PanZezhong committed
9
10
11
12
13

class AllocatorBase {
public:
    virtual void *alloc(size_t size) = 0;
    virtual void release(void *ptr) = 0;
thatPepe's avatar
thatPepe committed
14
    virtual ~AllocatorBase() = default;
PanZezhong's avatar
PanZezhong committed
15
16
};

thatPepe's avatar
thatPepe committed
17
class MemoryPool : public AllocatorBase {
PanZezhong's avatar
PanZezhong committed
18
public:
PanZezhong's avatar
PanZezhong committed
19
    static constexpr size_t DEFAULT_ALIGNMENT = 512;
20
21

    explicit MemoryPool(size_t initialSize = 0, size_t alignment = DEFAULT_ALIGNMENT);
thatPepe's avatar
thatPepe committed
22
    ~MemoryPool();
23

PanZezhong's avatar
PanZezhong committed
24
25
    void *alloc(size_t size) override;
    void release(void *ptr) override;
thatPepe's avatar
thatPepe committed
26

27
28
    size_t getAlignment() const { return _alignment; }

thatPepe's avatar
thatPepe committed
29
30
31
32
33
34
private:
    struct Block {
        void *base;
        void *ptr;
        size_t size;
        bool is_free;
35

thatPepe's avatar
thatPepe committed
36
37
        Block(void *b, void *p, size_t s, bool f)
            : base(b), ptr(p), size(s), is_free(f) {}
38

thatPepe's avatar
thatPepe committed
39
40
41
42
43
44
45
46
        bool operator<(const Block &other) const {
            return ptr < other.ptr;
        }
    };

    void *allocateNewRegion(size_t size);
    void tryCoalesce(const Block &block);

47
    size_t _alignment;
thatPepe's avatar
thatPepe committed
48
49
50
51
    std::vector<void *> _base_regions;
    std::set<Block> _all_blocks;
    std::multimap<size_t, std::set<Block>::iterator> _free_blocks;
    std::unordered_map<void *, std::set<Block>::iterator> _ptr_to_block;
PanZezhong's avatar
PanZezhong committed
52
53
54
};

#endif