Skip to content

Commit

Permalink
equeue: align passed-in buffer
Browse files Browse the repository at this point in the history
Make equeue_create_inplace align the passed-in buffer and size to
sizeof(void *).

Really we should be aiming to align more for ARM, as blocks should be
8-byte aligned, but the internal heap mechanisms only work to 4-byte
alignment at the moment. More work would be needed to ensure 8-byte
alignment of allocated blocks.
  • Loading branch information
kjbracey authored and geky committed Aug 4, 2019
1 parent 0a84f94 commit 0d67610
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions equeue.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#include "equeue.h"

#include <stdlib.h>
#include <stdint.h>
#include <string.h>


// calculate the relative-difference between absolute times while
// correctly handling overflow conditions
static inline int equeue_tickdiff(unsigned a, unsigned b) {
Expand Down Expand Up @@ -47,7 +47,11 @@ int equeue_create(equeue_t *q, size_t size) {

int equeue_create_inplace(equeue_t *q, size_t size, void *buffer) {
// setup queue around provided buffer
q->buffer = buffer;
// ensure buffer and size are aligned
q->buffer = (void *)(((uintptr_t) buffer + sizeof(void *) -1) & ~(sizeof(void *) -1));
size -= (char *) q->buffer - (char *) buffer;
size &= ~(sizeof(void *) -1);

q->allocated = 0;

q->npw2 = 0;
Expand All @@ -57,7 +61,7 @@ int equeue_create_inplace(equeue_t *q, size_t size, void *buffer) {

q->chunks = 0;
q->slab.size = size;
q->slab.data = buffer;
q->slab.data = q->buffer;

q->queue = 0;
q->tick = equeue_tick();
Expand Down

0 comments on commit 0d67610

Please sign in to comment.