Skip to content

Instantly share code, notes, and snippets.

@tadfmac
Created Aug 5, 2016
Embed
What would you like to do?
Queue32
// Queue32.c
// 4byte ring buffer (32bit version)
//
// 2016.07.27 : D.F.Mac.
#include "queue32.h"
#ifdef __cplusplus
extern "C"{
#endif
void b4arrq_init(stB4Arrq *qp){
qp->top = 0;
qp->last = 0;
qp->num = 0;
qp->queue_size = B4QUEUE_SIZE;
}
uint32_t b4arrq_next(uint32_t value){
return (value + 1) % B4QUEUE_SIZE;
}
uint32_t b4arrq_push(stB4Arrq *qp, uint32_t *p){
uint32_t *q = qp->queue;
uint32_t last = qp->last;
if(b4arrq_next(last) == qp->top){
return 0;
}
*(q+last) = *p;
qp->last = b4arrq_next(last);
qp->num ++;
return 1;
}
uint32_t *b4arrq_pop(stB4Arrq *qp){
uint32_t *pRes = (uint32_t *)0;
uint32_t *p = qp->queue;
uint32_t top = qp->top;
if(top != qp->last){
pRes = p+top;
qp->top = b4arrq_next(top);
qp->num --;
}
return pRes;
}
#ifdef __cplusplus
} // extern "C"
#endif
// Queue32.h
// 4byte ring buffer (32bit version)
//
// 2016.07.27 : D.F.Mac.
#ifndef __queue32_h__
#define __queue32_h__
#include <stdint.h>
#ifdef __cplusplus
extern "C"{
#endif
#define B4QUEUE_SIZE 512
typedef struct{
uint32_t top;
uint32_t last;
uint32_t num;
uint32_t queue_size; // not use yet.
uint32_t queue[B4QUEUE_SIZE];
}stB4Arrq;
void b4arrq_init(stB4Arrq *qp);
uint32_t b4arrq_push(stB4Arrq *qp, uint32_t *p);
uint32_t *b4arrq_pop(stB4Arrq *qp);
#ifdef __cplusplus
} // extern "C"
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment