forked from StevenBaby/onix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9cb880a
commit e8459bb
Showing
6 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# 数据结构循环队列 | ||
|
||
```c++ | ||
typedef struct fifo_t | ||
{ | ||
char *buf; | ||
u32 length; | ||
u32 head; | ||
u32 tail; | ||
} fifo_t; | ||
|
||
void fifo_init(fifo_t *fifo, char *buf, u32 length); | ||
bool fifo_full(fifo_t *fifo); | ||
bool fifo_empty(fifo_t *fifo); | ||
char fifo_get(fifo_t *fifo); | ||
void fifo_put(fifo_t *fifo, char byte); | ||
``` | ||
queue / fifo First in First out | ||
生产者 - 消费者 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef ONIX_FIFO_H | ||
#define ONIX_FIFO_H | ||
|
||
#include <onix/types.h> | ||
|
||
typedef struct fifo_t | ||
{ | ||
char *buf; | ||
u32 length; | ||
u32 head; | ||
u32 tail; | ||
} fifo_t; | ||
|
||
void fifo_init(fifo_t *fifo, char *buf, u32 length); | ||
bool fifo_full(fifo_t *fifo); | ||
bool fifo_empty(fifo_t *fifo); | ||
char fifo_get(fifo_t *fifo); | ||
void fifo_put(fifo_t *fifo, char byte); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <onix/fifo.h> | ||
#include <onix/assert.h> | ||
|
||
static _inline u32 fifo_next(fifo_t *fifo, u32 pos) | ||
{ | ||
return (pos + 1) % fifo->length; | ||
} | ||
|
||
void fifo_init(fifo_t *fifo, char *buf, u32 length) | ||
{ | ||
fifo->buf = buf; | ||
fifo->length = length; | ||
fifo->head = 0; | ||
fifo->tail = 0; | ||
} | ||
|
||
bool fifo_full(fifo_t *fifo) | ||
{ | ||
bool full = (fifo_next(fifo, fifo->head) == fifo->tail); | ||
return full; | ||
} | ||
|
||
bool fifo_empty(fifo_t *fifo) | ||
{ | ||
return (fifo->head == fifo->tail); | ||
} | ||
|
||
char fifo_get(fifo_t *fifo) | ||
{ | ||
assert(!fifo_empty(fifo)); | ||
char byte = fifo->buf[fifo->tail]; | ||
fifo->tail = fifo_next(fifo, fifo->tail); | ||
return byte; | ||
} | ||
|
||
void fifo_put(fifo_t *fifo, char byte) | ||
{ | ||
while (fifo_full(fifo)) | ||
{ | ||
fifo_get(fifo); | ||
} | ||
fifo->buf[fifo->head] = byte; | ||
fifo->head = fifo_next(fifo, fifo->head); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters