ROOT-Sim core  3.0.0-rc.2
A General-Purpose Multi-threaded Parallel/Distributed Simulation Library
buddy.h
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <datatypes/bitmap.h>
12 
13 #include <assert.h>
14 #include <stdalign.h>
15 #include <stdbool.h>
16 #include <stddef.h>
17 #include <stdint.h>
18 
19 #define B_TOTAL_EXP 16U
20 #define B_BLOCK_EXP 6U
21 
22 #define next_exp_of_2(i) (sizeof(i) * CHAR_BIT - intrinsics_clz(i))
23 #define buddy_allocation_block_compute(req_size) next_exp_of_2(max(req_size, 1U << B_BLOCK_EXP) - 1);
24 
25 #define buddy_left_child(i) (((i) << 1U) + 1U)
26 #define buddy_right_child(i) (((i) << 1U) + 2U)
27 #define buddy_parent(i) ((((i) + 1) >> 1U) - 1U)
28 
30 struct buddy_state {
32 
33  alignas(16) uint8_t longest[(1U << (B_TOTAL_EXP - B_BLOCK_EXP + 1))];
35  alignas(16) unsigned char base_mem[1U << B_TOTAL_EXP];
39  // this tracks writes to the allocation tree
40  (1 << (B_TOTAL_EXP - 2 * B_BLOCK_EXP + 1)) +
41  // while this tracks writes to the actual memory buffer
42  (1 << (B_TOTAL_EXP - B_BLOCK_EXP))
43  )
44  ];
45 };
46 
47 static_assert(
48  offsetof(struct buddy_state, longest) ==
49  offsetof(struct buddy_state, base_mem) -
50  sizeof(((struct buddy_state *)0)->longest),
51  "longest and base_mem are not contiguous, this will break incremental checkpointing");
52 
53 extern void buddy_init(struct buddy_state *self);
54 extern void *buddy_malloc(struct buddy_state *self, uint_fast8_t req_blks_exp);
55 extern uint_fast32_t buddy_free(struct buddy_state *self, void *ptr);
56 
58  bool handled;
59  union {
60  int_fast32_t variation;
61  uint_fast32_t original;
62  };
63 };
64 extern struct buddy_realloc_res buddy_best_effort_realloc(struct buddy_state *self, void *ptr, size_t req_size);
65 extern void buddy_dirty_mark(struct buddy_state *self, const void *ptr, size_t s);
Bitmap data type.
unsigned char block_bitmap
The type of a generic bitmap.
Definition: bitmap.h:22
#define bitmap_required_size(requested_bits)
Computes the required size of a bitmap.
Definition: bitmap.h:63
Definition: buddy.h:57
The checkpointable memory context of a single buddy system.
Definition: buddy.h:30
block_bitmap dirty[bitmap_required_size((1<<(B_TOTAL_EXP - 2 *B_BLOCK_EXP+1))+(1<<(B_TOTAL_EXP - B_BLOCK_EXP)))]
Keeps track of memory blocks which have been dirtied by a write.
Definition: buddy.h:44
uint8_t longest[(1U<<(B_TOTAL_EXP - B_BLOCK_EXP+1))]
The checkpointed binary tree representing the buddy system.
Definition: buddy.h:33
unsigned char base_mem[1U<< B_TOTAL_EXP]
The memory buffer served to the model.
Definition: buddy.h:35