source: trunk/symbol_table/src/byte_pool.hpp @ 2026

Last change on this file since 2026 was 1967, checked in by ksherdy, 7 years ago

Added src directory.

File size: 1.1 KB
Line 
1/*
2 * Created on: 18-December-2011
3 * Author: Ken Herdy
4 *
5 * Byte pool. Templated on custom allocator, see allocator.hpp.
6 *
7 */
8#ifndef BYTE_POOL_HPP
9#define BYTE_POOL_HPP
10
11#include "../lib/allocator.hpp"
12#include <string.h>
13
14template<class ALLOCATOR>
15class byte_pool
16{
17public:
18    byte_pool(const uint64_t base_size=1024) {
19        alloc_segment_size = 0;
20        offset = 0;
21        pool = static_cast<uint8_t *>(allocator.allocate(base_size, alloc_segment_size));
22    }
23
24    ~byte_pool() {
25        allocator.destroy();
26    }
27
28    uint8_t * insert(uint8_t * bytes, uint32_t lgth) {
29
30        if(lgth > alloc_segment_size) {
31            pool = static_cast<uint8_t *>(allocator.allocate(lgth, alloc_segment_size));
32            offset = 0;
33        }
34
35        uint8_t * next = &(pool[offset]);
36        memcpy(next, bytes, lgth);
37        alloc_segment_size -= lgth;
38        offset += lgth;
39
40        return next;
41    }
42
43    uint8_t * insert(uint8_t * bytes, uint32_t lgth, uint32_t advance) {
44        uint32_t total_lgth = lgth + advance;
45        return insert(bytes, total_lgth);
46    }
47
48private:
49    ALLOCATOR allocator;
50    uint64_t alloc_segment_size;
51    uint32_t offset;
52    uint8_t * pool;
53};
54
55#endif // BYTE_POOL_HPP
Note: See TracBrowser for help on using the repository browser.