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

Add EVENTS_EVENT_SIZE define for calculated queue allocations #8

Merged
merged 3 commits into from
Jul 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions EventQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@
namespace events {


/** EVENTS_EVENT_SIZE
* Minimum size of an event
* This size fits a Callback<void()> at minimum
*/
#undef EVENTS_EVENT_SIZE
#define EVENTS_EVENT_SIZE (sizeof(struct event) + sizeof(void*) + sizeof(mbed::Callback<void()>))

/** DEFAULT_QUEUE_SIZE
* default size of buffer for events
*/
#define DEFAULT_QUEUE_SIZE \
(32*(sizeof(struct event) + sizeof(mbed::Callback<void()>)))
#define DEFAULT_QUEUE_SIZE (32*EVENTS_EVENT_SIZE)


/** EventQueue
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ The core API of the events library is contained in the

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

// Enqueues events on the underlying event queue
Expand Down
4 changes: 2 additions & 2 deletions events-c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ void print(void *s) {
}

int main() {
// creates a queue with 32 events with default size
// creates a queue with space for 32 basic events
equeue_t queue;
equeue_create(&queue, 32, 0);
equeue_create(&queue, 32*EVENTS_EVENT_SIZE);

// events are simple callbacks
event_call(&queue, print, "called immediately");
Expand Down
5 changes: 5 additions & 0 deletions events-c/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ extern "C" {
#include "events_sema.h"


// Definition of the minimum size of an event
// This size fits the events created in the event_call set of functions.
#define EVENTS_EVENT_SIZE (sizeof(struct event) + 3*sizeof(void*))

// Event/queue structures
struct event {
struct event *next;
Expand Down Expand Up @@ -51,6 +55,7 @@ typedef struct equeue {
events_mutex_t freelock;
} equeue_t;


// Queue operations
//
// Creation results in negative value on failure.
Expand Down
26 changes: 13 additions & 13 deletions events-c/tests/prof.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void events_tick_prof(void) {

void event_alloc_prof(void) {
struct equeue q;
equeue_create(&q, 2*32*sizeof(struct event));
equeue_create(&q, 32*EVENTS_EVENT_SIZE);

prof_loop() {
prof_start();
Expand All @@ -141,7 +141,7 @@ void event_alloc_prof(void) {

void event_alloc_many_prof(int count) {
struct equeue q;
equeue_create(&q, 2*count*sizeof(struct event));
equeue_create(&q, count*EVENTS_EVENT_SIZE);

void *es[count];

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

void event_post_prof(void) {
struct equeue q;
equeue_create(&q, 2*sizeof(struct event));
equeue_create(&q, EVENTS_EVENT_SIZE);

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

void event_post_many_prof(int count) {
struct equeue q;
equeue_create(&q, 2*count*sizeof(struct event));
equeue_create(&q, count*EVENTS_EVENT_SIZE);

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

void event_post_future_prof(void) {
struct equeue q;
equeue_create(&q, 2*sizeof(struct event));
equeue_create(&q, EVENTS_EVENT_SIZE);

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

void event_post_future_many_prof(int count) {
struct equeue q;
equeue_create(&q, 2*count*sizeof(struct event));
equeue_create(&q, count*EVENTS_EVENT_SIZE);

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

void equeue_dispatch_prof(void) {
struct equeue q;
equeue_create(&q, 2*sizeof(struct event));
equeue_create(&q, EVENTS_EVENT_SIZE);

prof_loop() {
event_call(&q, no_func, 0);
Expand All @@ -259,7 +259,7 @@ void equeue_dispatch_prof(void) {

void equeue_dispatch_many_prof(int count) {
struct equeue q;
equeue_create(&q, 2*count*sizeof(struct event));
equeue_create(&q, count*EVENTS_EVENT_SIZE);

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

void event_cancel_prof(void) {
struct equeue q;
equeue_create(&q, 2*sizeof(struct event));
equeue_create(&q, EVENTS_EVENT_SIZE);

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

void event_cancel_many_prof(int count) {
struct equeue q;
equeue_create(&q, 2*count*sizeof(struct event));
equeue_create(&q, count*EVENTS_EVENT_SIZE);

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

void event_alloc_size_prof(void) {
size_t size = 2*32*sizeof(struct event);
size_t size = 32*EVENTS_EVENT_SIZE;

struct equeue q;
equeue_create(&q, size);
Expand All @@ -321,7 +321,7 @@ void event_alloc_size_prof(void) {
}

void event_alloc_many_size_prof(int count) {
size_t size = 2*count*sizeof(struct event);
size_t size = count*EVENTS_EVENT_SIZE;

struct equeue q;
equeue_create(&q, size);
Expand All @@ -336,7 +336,7 @@ void event_alloc_many_size_prof(int count) {
}

void event_alloc_fragmented_size_prof(int count) {
size_t size = 2*count*sizeof(struct event);
size_t size = count*EVENTS_EVENT_SIZE;

struct equeue q;
equeue_create(&q, size);
Expand Down
7 changes: 4 additions & 3 deletions events-c/tests/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ void break_test(void) {
// Barrage tests
void simple_barrage_test(int N) {
equeue_t q;
int err = equeue_create(&q, N * 56);
int err = equeue_create(&q, N*(EVENTS_EVENT_SIZE+sizeof(struct timing)));
test_assert(!err);

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

void fragmenting_barrage_test(int N) {
equeue_t q;
int err = equeue_create(&q, N * 1000);
int err = equeue_create(&q,
2*N*(EVENTS_EVENT_SIZE+sizeof(struct fragment)+N*sizeof(int)));
test_assert(!err);

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

void multithreaded_barrage_test(int N) {
equeue_t q;
int err = equeue_create(&q, N * 56);
int err = equeue_create(&q, N*(EVENTS_EVENT_SIZE+sizeof(struct timing)));
test_assert(!err);

struct ethread t;
Expand Down