-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufferqueue.h
More file actions
200 lines (142 loc) · 5.37 KB
/
bufferqueue.h
File metadata and controls
200 lines (142 loc) · 5.37 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
/**
* MIT License
*
* Copyright (c) 2023 Alex Chen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __BQUE_H__
#define __BQUE_H__
#include <stdint.h>
#ifdef BQUE_DEBUG
#include <stdarg.h>
#include <stdio.h>
#endif
/* Basic data types. */
typedef int8_t bque_s8_t;
typedef uint8_t bque_u8_t;
typedef int16_t bque_s16_t;
typedef uint16_t bque_u16_t;
typedef int32_t bque_s32_t;
typedef uint32_t bque_u32_t;
typedef int64_t bque_s64_t;
typedef uint64_t bque_u64_t;
typedef uint32_t bque_size_t;
enum _bque_res {
/* all is well, so far :) */
BQUE_OK = 0,
/* generic error occurred. */
BQUE_ERR = -1,
/* failed to allocate memory. */
BQUE_ERR_NO_MEM = -2,
/* invalid offset. */
BQUE_ERR_BAD_OFFS = -3,
/* invalid size. */
BQUE_ERR_BAD_SIZE = -4,
/* full queue. */
BQUE_ERR_FULL_QUE = -5,
/* empty queue. */
BQUE_ERR_EMPTY_QUE = -6,
/* invalid index. */
BQUE_ERR_BAD_IDX = -7,
/* iterating stoped. */
BQUE_ERR_ITER_STOP = -8,
/* Invalid option. */
BQUE_ERR_BAD_OPT = -9,
};
/* Option enumeration type */
typedef enum _bque_opt {
BQUE_OPT_GET_MAX_BUFF_NUM,
BQUE_OPT_SET_MAX_BUFF_NUM,
BQUE_OPT_GET_MAX_BUFF_SIZE,
BQUE_OPT_SET_MAX_BUFF_SIZE,
/* Set customized buffer freeing callback.
This is used when your buffer structure contains pointers
that need to be freed before the buffer is deleted. */
BQUE_OPT_SET_FREE_BUFF_CB,
} bque_opt_t;
/* iterating order. */
typedef enum _bque_iter_order {
BQUE_ITER_FORWARD = 0,
BQUE_ITER_BACKWARD = 1,
} bque_iter_order_t;
/* sorting order. */
typedef enum _bque_sort_order {
BQUE_SORT_ASCENDING = 0,
BQUE_SORT_DESCENDING = 1,
} bque_sort_order_t;
/* sorting result. */
typedef enum _bque_sort_res {
/* a is less than b. */
BQUE_SORT_LESS = -1,
/* a is equal to b. */
BQUE_SORT_EQUAL = 0,
/* a is greater than b. */
BQUE_SORT_GREATER = 1,
} bque_sort_res_t;
#ifdef BQUE_DEBUG
/* logging function for debug. */
#define BQUE_LOG(fmt, ...) printf(fmt, ##__VA_ARGS__)
/* assertion macro used in the APIs. */
#define BQUE_ASSERT(expr) \
if (!(expr)) { BQUE_LOG("[BQUE] %s:%d: assertion failed: \"%s\"\n", \
__FILE__, __LINE__, #expr); while (1);};
/* returned result used in the APIs. */
typedef enum _bque_res bque_res_t;
#else
/* returned result used in the APIs. */
typedef bque_s32_t bque_res_t;
/* assertion macro used in the APIs. */
#define BQUE_ASSERT(expr)
#endif
/* Buffer freeing callback. */
typedef bque_res_t (*bque_free_buff_cb_t)(void *buff, bque_size_t size);
/* Configuration of the buffer queue. */
typedef struct _bque_conf {
bque_u32_t buff_num_max;
bque_u32_t buff_size_max;
bque_free_buff_cb_t free_buff_cb;
} bque_conf_t;
/* status of the buffer queue. */
typedef struct _bque_stat {
bque_u32_t buff_num;
} bque_stat_t;
/* context of the buffer queue. */
typedef struct _bque_ctx bque_ctx_t;
/* Iterating callback. */
typedef bque_res_t (*bque_iter_cb_t)(bque_u32_t idx, bque_u32_t num,
void *buff, bque_size_t size);
/* Sorting callback. */
typedef bque_sort_res_t (*bque_sort_cb_t)(const void *buff_a, bque_size_t size_a,
const void *buff_b, bque_size_t size_b);
bque_res_t bque_new(bque_ctx_t **ctx, bque_conf_t *conf);
bque_res_t bque_free(bque_ctx_t *ctx);
bque_res_t bque_stat(bque_ctx_t *ctx, bque_stat_t *stat);
bque_res_t bque_enqueue(bque_ctx_t *ctx, const void *buff, bque_u32_t size);
bque_res_t bque_preempt(bque_ctx_t *ctx, const void *buff, bque_u32_t size);
bque_res_t bque_insert(bque_ctx_t *ctx, bque_u32_t idx, const void *buff, bque_u32_t size);
bque_res_t bque_dequeue(bque_ctx_t *ctx, void *buff, bque_u32_t *size);
bque_res_t bque_forfeit(bque_ctx_t *ctx, void *buff, bque_u32_t *size);
bque_res_t bque_drop(bque_ctx_t *ctx, bque_u32_t idx, void *buff, bque_u32_t *size);
bque_res_t bque_empty(bque_ctx_t *ctx);
bque_res_t bque_item(bque_ctx_t *ctx, bque_s32_t idx, void **buff, bque_size_t *size);
bque_res_t bque_sort(bque_ctx_t *ctx, bque_sort_cb_t cb, bque_sort_order_t order);
bque_res_t bque_foreach(bque_ctx_t *ctx, bque_iter_cb_t cb, bque_iter_order_t order);
bque_res_t bque_adjust(bque_ctx_t *ctx, bque_opt_t opt, void *arg);
#endif