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

Commit 7c44753

Browse files
authored
Merge pull request #8 from ARMmbed/event-size
Add EVENTS_EVENT_SIZE define for calculated queue allocations
2 parents e605f0d + a3acccc commit 7c44753

File tree

6 files changed

+34
-21
lines changed

6 files changed

+34
-21
lines changed

EventQueue.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@
2424
namespace events {
2525

2626

27+
/** EVENTS_EVENT_SIZE
28+
* Minimum size of an event
29+
* This size fits a Callback<void()> at minimum
30+
*/
31+
#undef EVENTS_EVENT_SIZE
32+
#define EVENTS_EVENT_SIZE (sizeof(struct event) + sizeof(void*) + sizeof(mbed::Callback<void()>))
33+
2734
/** DEFAULT_QUEUE_SIZE
2835
* default size of buffer for events
2936
*/
30-
#define DEFAULT_QUEUE_SIZE \
31-
(32*(sizeof(struct event) + sizeof(mbed::Callback<void()>)))
37+
#define DEFAULT_QUEUE_SIZE (32*EVENTS_EVENT_SIZE)
3238

3339

3440
/** EventQueue

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ The core API of the events library is contained in the
2828

2929
``` cpp
3030
// Creates an event queue with 2048 bytes of buffer space to use
31-
// for enqueueing events. The default is enough for 32 callbacks.
31+
// for enqueueing events. With no argument, the default buffer is
32+
// allocated with enough space for 32 Callback classes.
3233
EventQueue queue(2048);
3334

3435
// Enqueues events on the underlying event queue

events-c/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-c/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.

events-c/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);

events-c/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)