source: trunk/lib_c/byte_pool.h @ 3391

Last change on this file since 3391 was 3391, checked in by linmengl, 6 years ago

check in IDISA C library and other support libraries. Some template features still remain.

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