File: queue.c

package info (click to toggle)
ubertooth 2018.12.R1-5.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,320 kB
  • sloc: ansic: 18,653; cpp: 2,549; python: 604; makefile: 474; asm: 113; perl: 86; sh: 63; ruby: 43; xml: 20
file content (51 lines) | stat: -rw-r--r-- 997 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
 * Copyright 2017 Mike Ryan
 *
 * This file is part of Project Ubertooth and is released under the
 * terms of the GPL. Refer to COPYING for more information.
 */

#include "queue.h"

// queue implementation is based heavily on Koopman's "Better Embedded
// Systems Software" section 20.3.3.1 pg 209

void queue_init(queue_t *f) {
	f->head = 0;
	f->tail = 0;
}

// insert
int queue_insert(queue_t *f, void *x) {
	unsigned newtail;
	// access next free element
	newtail = f->tail + 1;

	// wrap around to beginning if needed
	if (newtail >= FIFOSIZE) { newtail = 0; }

	// if head and tail are equal, queue is full
	if (newtail == f->head) { return 0; }

	// write data before updating pointer
	f->data[newtail] = x;
	f->tail = newtail;

	return 1;
}

// TODO remove
int queue_remove(queue_t *f, void **x) {
	unsigned newhead;

	if (f->head == f->tail) { return 0; }

	newhead = f->head + 1;

	if (newhead >= FIFOSIZE) { newhead = 0; }

	*x = f->data[newhead];
	f->head = newhead;

	return 1;
}