Single-byte FIFO cache (30-day homemade operating system – reading notes)

Source: Internet
Author: User

From today onwards, write some reading notes. In recent months have been watching the "30-day homemade operating system this book", although the book is read ebook, but can pay to buy the original book, since the cost of money, there must be some harvest.

No one can always stand still, want to progress to learn other people's knowledge, for programmers, the simplest way is to learn the code of others.

Today's title is "single-byte FIFO cache", actually is to do a FIFO, look at the name to know. There are 4 functions and a related structure, such small code in the embedded system is very common, it will be very useful.

1, the related data structure body

struct FIFO8 {        char *buf;     // Memory that actually holds the data        int w_pos,r_pos,size,free_left,flags;         // W_pos is a record        of where the cache is written // R_pos is a record        of the cache readout location // size is the cached data sizes         // Free_left is the amount        of space available to write data in the cache // flags is a flag record }

2. Initialize a cache space

void fifo8_init (struct FIFO8 *fifo,intChar *buf) {        FIFO->buf = buf;            FIFO->size = size;        FIFO0;        FIFO0;        FIFO->free_left = size;        FIFO0;  

3. Add a data to the cache

intFifo8_put (structFIFO8 *fifo,unsignedChardata) {        if(Fifo->free_left = =0) {FIFO->flags |=Flag_overrun; Define Flag_overrun 1return-1; }               //Free_left's standard of judgment is//write people a data free_left minus one, read out a data free_left plus one,//At the very beginning, Free_left is the cache size, that is, the entire cache can be writtenFifo->buf[fifo->w_pos] =data; W_pos++; if(W_pos = = fifo->size) {W_pos=0;//positional qualification of a write pointer} free_left--; return 0;}

4. Remove a data from the cache

intFifo8_get (structFIFO8 *FIFO) {       intdata; if(Fifo->free_left = = fifo->size) {              return-1;//The entire buffer is empty and there is no data to read} data= fifo->buf[fifo->R_pos]; FIFO->r_pos + +; if(Fifo->r_pos = = fifo->size) {FIFO->r_pos =0;//Read location Relocation} FIFO->free_left++; returnData//Note that data is unsigned char type, and the return value is int}

5. Get the state of the buffer (there are countless data-readable buffers)

int fifo8_status (struct FIFO8 *FIFO) {       return (fifo->size-fifo->  Free_left);        // cache size minus empty size, which is the number  of data stored in the cache }

How to use the "single byte FIFO cache" code above, this is very simple:

1. Initialize first

unsigned char buf_for_fifo[32];//defines the cache itself

struct FIFO8 Testfifo; Define the cache structure body

Fifo8_init (&TESTFIFO,32,BUF_FOR_FIFO); This function is called to complete the initialization

2. Write data to the buffer area where needed

ret = Fifo8_put (&testfifo,1); Write data to the buffer 1

3, where needed to query the cache there is no storage data, and read out

if (Fifo8_status (&TESTFIFO) > 0)

{

data = Fifo8_get (&TESTFIFO);

Processing this data can

}

The main point of the above code design is the data structure design, in addition the code has good portability, its key operation code, the incoming parameters are the core data structure of the pointer, this is not much like C + + in the this pointer.

So far today.

Single-byte FIFO cache (30-day homemade operating system – reading notes)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.