Skip to content
This repository was archived by the owner on Aug 19, 2021. It is now read-only.

Commit 04af2b4

Browse files
committed
Added EVENTS_EVENT_SIZE define for calculated queue allocations
In the events library, the structure of an event is correctly not part of the public api. Unfortunately, this makes it difficult to calculate the space needed for a finite set of events. EVENTS_EVENT_SIZE is defined as the space needed for the smallest event, the event allocated in event_call. This allows queues to be created based on a number of events: equeue_create(&q, 12 * (EVENTS_EVENT_SIZE+sizeof(struct context)));
1 parent 418a56a commit 04af2b4

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ void print(void *s) {
1313
}
1414

1515
int main() {
16-
// creates a queue with 32 events with default size
16+
// creates a queue with space for 32 basic events
1717
equeue_t queue;
18-
equeue_create(&queue, 32, 0);
18+
equeue_create(&queue, 32*EVENTS_EVENT_SIZE);
1919

2020
// events are simple callbacks
2121
event_call(&queue, print, "called immediately");

events.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ extern "C" {
1717
#include "events_sema.h"
1818

1919

20+
// Definition of the minimum size of an event
21+
// This size fits the events created in the event_call set of functions.
22+
#define EVENTS_EVENT_SIZE (sizeof(struct event) + 3*sizeof(void*))
23+
2024
// Event/queue structures
2125
struct event {
2226
struct event *next;
@@ -51,6 +55,7 @@ typedef struct equeue {
5155
events_mutex_t freelock;
5256
} equeue_t;
5357

58+
5459
// Queue operations
5560
//
5661
// Creation results in negative value on failure.

tests/prof.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void events_tick_prof(void) {
126126

127127
void event_alloc_prof(void) {
128128
struct equeue q;
129-
equeue_create(&q, 2*32*sizeof(struct event));
129+
equeue_create(&q, 32*EVENTS_EVENT_SIZE);
130130

131131
prof_loop() {
132132
prof_start();
@@ -141,7 +141,7 @@ void event_alloc_prof(void) {
141141

142142
void event_alloc_many_prof(int count) {
143143
struct equeue q;
144-
equeue_create(&q, 2*count*sizeof(struct event));
144+
equeue_create(&q, count*EVENTS_EVENT_SIZE);
145145

146146
void *es[count];
147147

@@ -166,7 +166,7 @@ void event_alloc_many_prof(int count) {
166166

167167
void event_post_prof(void) {
168168
struct equeue q;
169-
equeue_create(&q, 2*sizeof(struct event));
169+
equeue_create(&q, EVENTS_EVENT_SIZE);
170170

171171
prof_loop() {
172172
void *e = event_alloc(&q, 0);
@@ -183,7 +183,7 @@ void event_post_prof(void) {
183183

184184
void event_post_many_prof(int count) {
185185
struct equeue q;
186-
equeue_create(&q, 2*count*sizeof(struct event));
186+
equeue_create(&q, count*EVENTS_EVENT_SIZE);
187187

188188
for (int i = 0; i < count; i++) {
189189
event_call(&q, no_func, 0);
@@ -204,7 +204,7 @@ void event_post_many_prof(int count) {
204204

205205
void event_post_future_prof(void) {
206206
struct equeue q;
207-
equeue_create(&q, 2*sizeof(struct event));
207+
equeue_create(&q, EVENTS_EVENT_SIZE);
208208

209209
prof_loop() {
210210
void *e = event_alloc(&q, 0);
@@ -222,7 +222,7 @@ void event_post_future_prof(void) {
222222

223223
void event_post_future_many_prof(int count) {
224224
struct equeue q;
225-
equeue_create(&q, 2*count*sizeof(struct event));
225+
equeue_create(&q, count*EVENTS_EVENT_SIZE);
226226

227227
for (int i = 0; i < count; i++) {
228228
event_call(&q, no_func, 0);
@@ -244,7 +244,7 @@ void event_post_future_many_prof(int count) {
244244

245245
void equeue_dispatch_prof(void) {
246246
struct equeue q;
247-
equeue_create(&q, 2*sizeof(struct event));
247+
equeue_create(&q, EVENTS_EVENT_SIZE);
248248

249249
prof_loop() {
250250
event_call(&q, no_func, 0);
@@ -259,7 +259,7 @@ void equeue_dispatch_prof(void) {
259259

260260
void equeue_dispatch_many_prof(int count) {
261261
struct equeue q;
262-
equeue_create(&q, 2*count*sizeof(struct event));
262+
equeue_create(&q, count*EVENTS_EVENT_SIZE);
263263

264264
prof_loop() {
265265
for (int i = 0; i < count; i++) {
@@ -276,7 +276,7 @@ void equeue_dispatch_many_prof(int count) {
276276

277277
void event_cancel_prof(void) {
278278
struct equeue q;
279-
equeue_create(&q, 2*sizeof(struct event));
279+
equeue_create(&q, EVENTS_EVENT_SIZE);
280280

281281
prof_loop() {
282282
int id = event_call(&q, no_func, 0);
@@ -291,7 +291,7 @@ void event_cancel_prof(void) {
291291

292292
void event_cancel_many_prof(int count) {
293293
struct equeue q;
294-
equeue_create(&q, 2*count*sizeof(struct event));
294+
equeue_create(&q, count*EVENTS_EVENT_SIZE);
295295

296296
for (int i = 0; i < count; i++) {
297297
event_call(&q, no_func, 0);
@@ -309,7 +309,7 @@ void event_cancel_many_prof(int count) {
309309
}
310310

311311
void event_alloc_size_prof(void) {
312-
size_t size = 2*32*sizeof(struct event);
312+
size_t size = 32*EVENTS_EVENT_SIZE;
313313

314314
struct equeue q;
315315
equeue_create(&q, size);
@@ -321,7 +321,7 @@ void event_alloc_size_prof(void) {
321321
}
322322

323323
void event_alloc_many_size_prof(int count) {
324-
size_t size = 2*count*sizeof(struct event);
324+
size_t size = count*EVENTS_EVENT_SIZE;
325325

326326
struct equeue q;
327327
equeue_create(&q, size);
@@ -336,7 +336,7 @@ void event_alloc_many_size_prof(int count) {
336336
}
337337

338338
void event_alloc_fragmented_size_prof(int count) {
339-
size_t size = 2*count*sizeof(struct event);
339+
size_t size = count*EVENTS_EVENT_SIZE;
340340

341341
struct equeue q;
342342
equeue_create(&q, size);

tests/tests.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ void break_test(void) {
265265
// Barrage tests
266266
void simple_barrage_test(int N) {
267267
equeue_t q;
268-
int err = equeue_create(&q, N * 56);
268+
int err = equeue_create(&q, N*(EVENTS_EVENT_SIZE+sizeof(struct timing)));
269269
test_assert(!err);
270270

271271
for (int i = 0; i < N; i++) {
@@ -288,7 +288,8 @@ void simple_barrage_test(int N) {
288288

289289
void fragmenting_barrage_test(int N) {
290290
equeue_t q;
291-
int err = equeue_create(&q, N * 1000);
291+
int err = equeue_create(&q,
292+
2*N*(EVENTS_EVENT_SIZE+sizeof(struct fragment)+N*sizeof(int)));
292293
test_assert(!err);
293294

294295
for (int i = 0; i < N; i++) {
@@ -325,7 +326,7 @@ static void *ethread_dispatch(void *p) {
325326

326327
void multithreaded_barrage_test(int N) {
327328
equeue_t q;
328-
int err = equeue_create(&q, N * 56);
329+
int err = equeue_create(&q, N*(EVENTS_EVENT_SIZE+sizeof(struct timing)));
329330
test_assert(!err);
330331

331332
struct ethread t;

0 commit comments

Comments
 (0)