forked from rustyrussell/bitcoin-iterate
-
Notifications
You must be signed in to change notification settings - Fork 2
/
space.h
59 lines (52 loc) · 1.29 KB
/
space.h
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
58
59
/*******************************************************************************
*
* = space.h
*
* Defines functions for allocating space.
*
*/
#ifndef BITCOIN_ITERATE_SPACE_H
#define BITCOIN_ITERATE_SPACE_H
#include <ccan/tal/tal.h>
#include <assert.h>
/**
* space - Simple bump allocator used to label space acquired via tal.
*
* Set to 3MB because blocks are capped at 1MB (as of
* 2016-03-24 ¯\_(ツ)_/¯).
*
*/
struct space {
char buf[3 * 1024 * 1024];
size_t off;
};
/**
* space_init - Initialize new space
* @space: Allocated space
*/
static inline void space_init(struct space *space)
{
space->off = 0;
}
/**
* space_alloc - Allocate new space
* @space: Space to allocate
* @bytes: Size to allocate
*/
static inline void *space_alloc(struct space *space, size_t bytes)
{
void *p = space->buf + space->off;
/* If this fails, enlarge buf[] above */
assert(space->off + bytes <= sizeof(space->buf));
space->off += bytes;
return p;
}
/**
* space_alloc_arr - Allocate an array of identical spaces for a given type.
* @space: Space to allocate
* @type: Type to use to size each space
* @num: Number of spaces to allocate (length of array)
*/
#define space_alloc_arr(space, type, num) \
((type *)space_alloc((space), sizeof(type) * (num)))
#endif /* BITCOIN_ITERATE_SPACE_H */