Skip to content

Commit 2ce48fd

Browse files
author
peng.li24
committed
update
1 parent 7523506 commit 2ce48fd

4 files changed

Lines changed: 93 additions & 70 deletions

File tree

src/box.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#ifndef BOX_H
2+
#define BOX_H
3+
4+
#include <stdint.h>
5+
#include <stddef.h>
6+
7+
#include <block_malloc/block_malloc.h>
8+
#include "obj_usage.h"
9+
10+
typedef struct
11+
{
12+
#define BOX_MAGIC "box_malloc"
13+
uint8_t magic[16]; // "box_malloc"
14+
uint64_t boxhead_bytessize; // 伙伴系统的总size
15+
uint64_t box_bytessize; // 总内存大小,不可变,内存长度必须=16^n*x,n>=1,x=[1,15]
16+
blocks_meta_t blocks;
17+
} box_meta_t;
18+
19+
typedef enum
20+
{
21+
BOX_UNUSED = 0, // 未用(可以分配 obj、box)
22+
BOX_FORMATTED = 1, // box 已格式化
23+
OBJ_START = 2, // obj_start
24+
OBJ_CONTINUED = 3 // obj_continued
25+
} BoxState;
26+
27+
typedef struct
28+
{
29+
uint8_t state : 2; // 0=未用(可以分配obj、box),1=已格式化为box,2=obj
30+
int8_t continue_max : 6; // 连续的最大空闲obj,[0~16]
31+
} __attribute__((packed)) box_child_t;
32+
33+
typedef struct
34+
{
35+
uint8_t state : 2; // 0=未用(可以分配obj、box),1=已格式化为box,2=obj
36+
int8_t max_obj_capacity : 6; // 连续的最大空闲obj,[0~16]
37+
38+
// lock
39+
atomic_int_fast64_t rw_lock; // 0=无锁,1=读锁,2=写锁(简化实现)
40+
41+
// parent
42+
int32_t parent; // parent_blockid
43+
44+
// box
45+
uint8_t objlevel; //[0,16] boxlevel=本层的objlevel+1
46+
47+
// obj,childbox usage
48+
uint8_t avliable_slot; // 【2,16】
49+
obj_usage child_max_obj_capacity; // 下层的最大对象容量
50+
box_child_t used_slots[16];
51+
52+
// childbox
53+
int32_t childs_blockid[16];
54+
55+
} __attribute__((packed)) box_head_t; //
56+
57+
#endif // BOX_H

src/box_malloc.c

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,83 +7,17 @@
77
#include <block_malloc/block_malloc.h>
88

99
#include <box_malloc/box_malloc.h>
10-
#include "obj_offset.h"
10+
#include "obj_usage.h"
1111
#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"
4314

4415
static int check_magic(box_meta_t *meta) {
4516
if (memcmp(meta->magic, BOX_MAGIC, sizeof(meta->magic)) != 0) {
4617
return -1;
4718
}
4819
return 0;
4920
}
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; //
8721

8822
static void box_format(box_meta_t *meta, box_head_t *node, uint8_t objlevel, uint8_t avliable_slot, int32_t parent_id);
8923

src/lock.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef LOCK_H
2+
#define LOCK_H
3+
4+
#include <stdatomic.h>
5+
6+
static void rlock(atomic_int_fast64_t *lock) {
7+
int_fast64_t expected = 0;
8+
while (!atomic_compare_exchange_weak(lock, &expected, 1)) {
9+
expected = 0; // 重置 expected
10+
}
11+
}
12+
13+
static void runlock(atomic_int_fast64_t *lock) {
14+
atomic_store(lock, 0);
15+
}
16+
17+
static void lock(atomic_int_fast64_t *lock) {
18+
int_fast64_t expected = 0;
19+
while (!atomic_compare_exchange_weak(lock, &expected, 2)) {
20+
expected = 0;
21+
}
22+
}
23+
24+
static void unlock(atomic_int_fast64_t *lock) {
25+
atomic_store(lock, 0);
26+
}
27+
#endif // LOCK_H

src/obj_offset.h renamed to src/obj_usage.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef OBJ_USAGE_H
2+
#define OBJ_USAGE_H
3+
14
#include <stdbool.h>
25
#include <stddef.h>
36
#include <stdint.h>
@@ -68,4 +71,6 @@ static uint64_t obj_offset(const obj_usage a)
6871
}
6972
offset *= (a.multiple);
7073
return offset;
71-
}
74+
}
75+
76+
#endif // OBJ_USAGE_H

0 commit comments

Comments
 (0)