Add a new module implementing a queue data structure.
Since the functions are simple make them inline functions. This provides a small code size saving.
This commit is contained in:
14
module_queue/module_build_info
Normal file
14
module_queue/module_build_info
Normal file
@@ -0,0 +1,14 @@
|
||||
# You can set flags specifically for your module by using the MODULE_XCC_FLAGS
|
||||
# variable. So the following
|
||||
#
|
||||
# MODULE_XCC_FLAGS = $(XCC_FLAGS) -O3
|
||||
#
|
||||
# specifies that everything in the modules should have the application
|
||||
# build flags with -O3 appended (so the files will build at
|
||||
# optimization level -O3).
|
||||
#
|
||||
# You can also set MODULE_XCC_C_FLAGS, MODULE_XCC_XC_FLAGS etc..
|
||||
|
||||
MODULE_XCC_XC_FLAGS = $(XCC_XC_FLAGS)
|
||||
|
||||
DEPENDENT_MODULES = module_xassert
|
||||
1
module_queue/module_description
Normal file
1
module_queue/module_description
Normal file
@@ -0,0 +1 @@
|
||||
One line module description.
|
||||
54
module_queue/src/queue.h
Normal file
54
module_queue/src/queue.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#ifndef QUEUE_H_
|
||||
#define QUEUE_H_
|
||||
|
||||
#include <xassert.h>
|
||||
|
||||
typedef struct queue_t {
|
||||
/// Read index.
|
||||
unsigned rdptr;
|
||||
/// Write index.
|
||||
unsigned wrptr;
|
||||
unsigned size;
|
||||
unsigned mask;
|
||||
} queue_t;
|
||||
|
||||
inline int is_power_of_2(unsigned x) {
|
||||
return x != 0 && (x & (x - 1)) == 0;
|
||||
}
|
||||
|
||||
inline void queue_init(queue_t &q, unsigned size) {
|
||||
assert(is_power_of_2(size));
|
||||
q.rdptr = 0;
|
||||
q.wrptr = 0;
|
||||
q.size = size;
|
||||
q.mask = size - 1; // Assumes power of two.
|
||||
}
|
||||
|
||||
inline int queue_is_empty(const queue_t &q) {
|
||||
return q.wrptr == q.rdptr;
|
||||
}
|
||||
|
||||
inline int queue_is_full(const queue_t &q) {
|
||||
return q.wrptr - q.rdptr == q.size;
|
||||
}
|
||||
|
||||
inline void queue_push_word(queue_t &q, unsigned array[], unsigned data)
|
||||
{
|
||||
assert(!queue_is_full(q));
|
||||
array[q.wrptr++ & q.mask] = data;
|
||||
}
|
||||
|
||||
inline unsigned queue_pop_word(queue_t &q, unsigned array[]) {
|
||||
assert(!queue_is_empty(q));
|
||||
return array[q.rdptr++ & q.mask];
|
||||
}
|
||||
|
||||
inline unsigned queue_items(const queue_t &q) {
|
||||
return q.wrptr - q.rdptr;
|
||||
}
|
||||
|
||||
inline unsigned queue_space(const queue_t &q) {
|
||||
return q.size - queue_items(q);
|
||||
}
|
||||
|
||||
#endif /* QUEUE_H_ */
|
||||
11
module_queue/src/queue.xc
Normal file
11
module_queue/src/queue.xc
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "queue.h"
|
||||
|
||||
// Force external definitions of inline functions.
|
||||
extern inline int is_power_of_2(unsigned x);
|
||||
extern inline void queue_init(queue_t &q, unsigned size);
|
||||
extern inline int queue_is_empty(const queue_t &q);
|
||||
extern inline int queue_is_full(const queue_t &q);
|
||||
extern inline void queue_push_word(queue_t &q, unsigned array[], unsigned data);
|
||||
extern inline unsigned queue_pop_word(queue_t &q, unsigned array[]);
|
||||
extern inline unsigned queue_space(const queue_t &q);
|
||||
extern inline unsigned queue_items(const queue_t &q);
|
||||
Reference in New Issue
Block a user