|
7 | 7 | #include <block_malloc/block_malloc.h> |
8 | 8 |
|
9 | 9 | #include <box_malloc/box_malloc.h> |
10 | | -#include "obj_offset.h" |
| 10 | +#include "obj_usage.h" |
11 | 11 | #include "logutil.h" |
12 | | - |
13 | | -static void rlock(atomic_int_fast64_t *lock) { |
14 | | - int_fast64_t expected = 0; |
15 | | - while (!atomic_compare_exchange_weak(lock, &expected, 1)) { |
16 | | - expected = 0; // 重置 expected |
17 | | - } |
18 | | -} |
19 | | - |
20 | | -static void runlock(atomic_int_fast64_t *lock) { |
21 | | - atomic_store(lock, 0); |
22 | | -} |
23 | | - |
24 | | -static void lock(atomic_int_fast64_t *lock) { |
25 | | - int_fast64_t expected = 0; |
26 | | - while (!atomic_compare_exchange_weak(lock, &expected, 2)) { |
27 | | - expected = 0; |
28 | | - } |
29 | | -} |
30 | | - |
31 | | -static void unlock(atomic_int_fast64_t *lock) { |
32 | | - atomic_store(lock, 0); |
33 | | -} |
34 | | - |
35 | | -typedef struct |
36 | | -{ |
37 | | - #define BOX_MAGIC "box_malloc" |
38 | | - uint8_t magic[16]; // "box_malloc" |
39 | | - uint64_t boxhead_bytessize; // 伙伴系统的总size |
40 | | - uint64_t box_bytessize; // 总内存大小,不可变,内存长度必须=16^n*x,n>=1,x=[1,15] |
41 | | - blocks_meta_t blocks; |
42 | | -} box_meta_t; |
| 12 | +#include "lock.h" |
| 13 | +#include "box.h" |
43 | 14 |
|
44 | 15 | static int check_magic(box_meta_t *meta) { |
45 | 16 | if (memcmp(meta->magic, BOX_MAGIC, sizeof(meta->magic)) != 0) { |
46 | 17 | return -1; |
47 | 18 | } |
48 | 19 | return 0; |
49 | 20 | } |
50 | | -typedef enum |
51 | | -{ |
52 | | - BOX_UNUSED = 0, // 未用(可以分配 obj、box) |
53 | | - BOX_FORMATTED = 1, // box 已格式化 |
54 | | - OBJ_START = 2, // obj_start |
55 | | - OBJ_CONTINUED = 3 // obj_continued |
56 | | -} BoxState; |
57 | | - |
58 | | -typedef struct |
59 | | -{ |
60 | | - uint8_t state : 2; // 0=未用(可以分配obj、box),1=已格式化为box,2=obj |
61 | | - int8_t continue_max : 6; // 连续的最大空闲obj,[0~16] |
62 | | -} __attribute__((packed)) box_child_t; |
63 | | - |
64 | | -typedef struct |
65 | | -{ |
66 | | - uint8_t state : 2; // 0=未用(可以分配obj、box),1=已格式化为box,2=obj |
67 | | - int8_t max_obj_capacity : 6; // 连续的最大空闲obj,[0~16] |
68 | | - |
69 | | - // lock |
70 | | - atomic_int_fast64_t rw_lock; // 0=无锁,1=读锁,2=写锁(简化实现) |
71 | | - |
72 | | - // parent |
73 | | - int32_t parent; // parent_blockid |
74 | | - |
75 | | - // box |
76 | | - uint8_t objlevel; //[0,16] boxlevel=本层的objlevel+1 |
77 | | - |
78 | | - // obj,childbox usage |
79 | | - uint8_t avliable_slot; // 【2,16】 |
80 | | - obj_usage child_max_obj_capacity; // 下层的最大对象容量 |
81 | | - box_child_t used_slots[16]; |
82 | | - |
83 | | - // childbox |
84 | | - int32_t childs_blockid[16]; |
85 | | - |
86 | | -} __attribute__((packed)) box_head_t; // |
87 | 21 |
|
88 | 22 | static void box_format(box_meta_t *meta, box_head_t *node, uint8_t objlevel, uint8_t avliable_slot, int32_t parent_id); |
89 | 23 |
|
|
0 commit comments