source: trunk/lib/byte_pool.hpp @ 1950

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

Updated comments format.

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