-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.c
188 lines (148 loc) · 4.51 KB
/
store.c
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#define BYTE_SEQ_STORE_MIN_NODE_DATA_SIZE 4
#define BYTE_SEQ_STORE_MAX_BYTE_SEQ_SIZE 0x0000ffff
typedef struct _ByteSeqStoreNode ByteSeqStoreNode;
typedef struct _ByteSeqStoreNode
{
ByteSeqStoreNode *next_node;
s32 used_size;
u8 data [];
} ByteSeqStoreNode;
typedef struct
{
ByteSeqStoreNode *first_node;
ByteSeqStoreNode *last_node;
s32 node_data_size;
s64 memory_usage;
s64 seq_count;
} ByteSeqStore;
static __may_inline void ByteSeqStore_preinit (ByteSeqStore *bss)
{
if (!bss)
return (void) ffsc (__func__);
bss->first_node = NULL;
bss->last_node = NULL;
bss->node_data_size = 0;
bss->memory_usage = 0;
bss->seq_count = 0;
}
static __not_inline void ByteSeqStore_free (ByteSeqStore *bss)
{
if (!bss)
return (void) ffsc (__func__);
while (bss->first_node)
{
ByteSeqStoreNode *del_node = bss->first_node;
bss->first_node = del_node->next_node;
free (del_node);
}
ByteSeqStore_preinit (bss);
}
static __not_inline ByteSeqStoreNode *ByteSeqStore_alloc_node (s32 node_size)
{
ByteSeqStoreNode *new_node = malloc (node_size);
if (!new_node)
{
fprintf (stderr, "Out of memory in %s\n", __func__);
return NULL;
}
new_node->next_node = NULL;
new_node->used_size = 0;
return new_node;
}
static __not_inline int ByteSeqStore_create (ByteSeqStore *bss, s32 node_size)
{
if (!bss || node_size < (s32) sizeof (ByteSeqStoreNode) + BYTE_SEQ_STORE_MIN_NODE_DATA_SIZE)
return ffsc (__func__);
ByteSeqStore_preinit (bss);
bss->first_node = ByteSeqStore_alloc_node (node_size);
bss->last_node = bss->first_node;
bss->node_data_size = node_size - sizeof (ByteSeqStoreNode);
bss->memory_usage = (s64) node_size;
bss->seq_count = 0;
if (!bss->first_node)
{
ByteSeqStore_free (bss);
return FALSE;
}
return TRUE;
}
static __not_inline void ByteSeqStore_clear (ByteSeqStore *bss)
{
if (!bss || !bss->last_node)
return (void) ffsc (__func__);
while (bss->first_node->next_node)
{
ByteSeqStoreNode *del_node = bss->first_node->next_node;
bss->first_node->next_node = del_node->next_node;
free (del_node);
}
bss->last_node = bss->first_node;
bss->memory_usage = ((s64) sizeof (ByteSeqStoreNode)) + (s64) bss->node_data_size;
bss->seq_count = 0;
bss->first_node->used_size = 0;
}
static __force_inline int ByteSeqStore_store (ByteSeqStore *bss, const u8 *byte_seq, s32 seq_size)
{
if (!bss || !bss->last_node || !byte_seq || seq_size > BYTE_SEQ_STORE_MAX_BYTE_SEQ_SIZE)
return ffsc (__func__);
s32 needed_space = 2 + seq_size;
if (needed_space > bss->node_data_size)
return ffsc (__func__);
if (bss->last_node->used_size + needed_space > bss->node_data_size)
{
ByteSeqStoreNode *new_node = ByteSeqStore_alloc_node ((s32) sizeof (ByteSeqStoreNode) + bss->node_data_size);
if (!new_node)
return FALSE;
bss->last_node->next_node = new_node;
bss->last_node = new_node;
bss->memory_usage += ((s64) sizeof (ByteSeqStoreNode)) + (s64) bss->node_data_size;
}
bss->last_node->data [bss->last_node->used_size++] = seq_size >> 8;
bss->last_node->data [bss->last_node->used_size++] = seq_size & 0x00ffu;
s32 seq_ix;
for (seq_ix = 0; seq_ix < seq_size; seq_ix++)
bss->last_node->data [bss->last_node->used_size++] = byte_seq [seq_ix];
bss->seq_count++;
return TRUE;
}
static __not_inline void ByteSeqStore_start_get_iteration (const ByteSeqStore *bss, ByteSeqStoreNode **bss_node, s32 *node_data_offset)
{
if (bss_node)
*bss_node = NULL;
if (node_data_offset)
*node_data_offset = 0;
if (!bss || !bss->last_node || !bss_node || !node_data_offset)
return (void) ffsc (__func__);
*bss_node = bss->first_node;
*node_data_offset = 0;
}
static __force_inline int ByteSeqStore_get_next (const ByteSeqStore *bss, ByteSeqStoreNode **bss_node, s32 *node_data_offset, u8 *byte_seq, s32 max_seq_size, s32 *seq_size)
{
if (seq_size)
*seq_size = 0;
if (!bss || !bss->last_node || !bss_node || !node_data_offset || !byte_seq)
return ffsc (__func__);
if (*bss_node == NULL)
return FALSE;
if (*node_data_offset >= (*bss_node)->used_size)
{
*bss_node = (*bss_node)->next_node;
*node_data_offset = 0;
if (*bss_node == NULL)
return FALSE;
}
s32 size = (((s32) (*bss_node)->data [*node_data_offset]) << 8) | (s32) (*bss_node)->data [(*node_data_offset) + 1];
int success = TRUE;
if (size > max_seq_size)
success = FALSE;
else
{
if (seq_size)
*seq_size = size;
s32 seq_ix;
for (seq_ix = 0; seq_ix < size; seq_ix++)
byte_seq [seq_ix] = (*bss_node)->data [(*node_data_offset) + (2 + seq_ix)];
}
*node_data_offset += (2 + size);
return success;
}