-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_malloc.hpp
57 lines (46 loc) · 1.37 KB
/
block_malloc.hpp
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
// Block allocator to use for deconstructing "things" (mainly HTTP requests)
// where there tend to be lots of little "new"s all of which can be deleted
// at the same time.
// sizes are fixed could pass in an external array if need be.
#ifndef BLOCK_MALLOC_H
#define BLOCK_MALLOC_H
#include "pico/stdlib.h"
#define BLOCK_SIZE 4096
#define BLOCK_COUNT 16
// Set this to use calloc to
// allocate the main blocks of memory
// Otherwise just big array.
//#define BLOCK_USE_CALLOC 1
class BlockPool;
#define _BLOCK_POOL_GUARD 0xAAAAAAAA
class Block {
int guard0;
#ifdef BLOCK_USE_CALLOC
uint8_t* block;
#else
uint8_t block[BLOCK_SIZE];
#endif
int guard1;
uint used_count;
uint free_count;
bool allocated; // whether this block is allocated or free.
Block* nextBlock; // chain if more than BLOCK_SIZE needed
Block* activeBlock; // for chained, points to last block in list where mem allocated from.
BlockPool* pool; // Source pool to get new blocks from.
void* localAlloc(size_t bytes);
public:
Block();
~Block();
void* allocate(size_t bytes);
void allocateBlock(BlockPool* pool);
bool isAllocated() const {return allocated;}
void free();
};
class BlockPool {
Block blocks[BLOCK_COUNT];
int position; // for round-robin allocation.
public:
BlockPool();
Block* allocate();
};
#endif