Realization of ring memory.

xiaoxiao2021-03-06  63

There are two threads, one is reading a buf, and the other is writing this BUF.

The most beginning is like this: Define a tail pointer, write the tail pointer every time you write, when you read it, you will read the rest after reading the rest, and modify the tail pointer. .

Considering that the memory copy (Memcpy) is required for each reading, so I want to use the ring memory.

Two pointers are needed, a thief (Thief_PTR), another called the police (Police_PTR), where the role of the thief played, and the police played the role of reading. So actually on a ring runway, the police chased the thief, the thief will always be in front of the police, but it cannot exceed the police more than the police.

There is also a detail problem to handle, that is, when the thief runs to the starting line (memory boundary) When the step size exceeds the rest of the memory,

Step_LENTH> MAX_BUFFER_LEN - THIEF_PTR

That is to say, when the length of the data is greater than the remaining space, this time, the size of the runway can be changed, I will set the length of this runway to the thief during the critical area, so you can Effectively solve this problem.

After testing, the efficiency of this method is about 10% higher than that of Memcpy each time, because it is a relatively core of the network layer engine, 10% efficiency can bring very large changes.

The following is a pseudo code:

/ / Set to double size, facilitating the growth of each circle of runways Unsigned char buf [buf_size * 2]; unsigned char * thief_ptr; unsigned char * policy_ptr; int raceTrack_len; // Judging whether the police and thieves are in the same circle The boundary is the starting line BOOL INCATCHCLECLE;

Void init () {// police and thieves on the same running line thief_ptr = police_ptr = BUF; // Police and thief start in the same lap in the same circle;}

Bool Checkwriteable (Int Len) {// If you are not in the same turn, the thief cannot exceed the police IF ((! incatchcycle && (police_ptr - thief_ptr

INT Sendmsg (Const Unsigned Char * MSG, Const Int & Len) {if (! Checkwriteable (LEN) || LEN> BUF_SIZE) RETURN 0;

// To receive the message, helpless, only MemcPymemcpy (Thief_PTR, MSG, LEN) can be used;

// Police and thief are not in the same turn (! Incatchcycle || (buf_size - (Thief_PTR - BUF)> = LEN)) {// Thief Run LEN THIEF_PTR = LEN; RETURN 1;}

/ / Fixed the length of this circle RaceTrack_len = (Thief_PTR - WBUF) Len; // Run a circle, returned to the end point thief_ptr = BUF; // Thief this time and the police are definitely not in the same lap IncatchCycle = false; return 1 }

Int Readmsg () {INT LEN;

// If the police and thieves are in the same turn, the difference between the police and thieves is the difference in IF (incatchcycle) len = thief_ptr - police_ptr; / / Otherwise, the police is not the end point of the police, the end point of the correction is too much else len = raceTrack_len - (Police_PTR - BUF); if (len <= 0) returnography;

INT N; // Because many factors, N is not equal to LEN / / especially in the case. n = get_msg (police_ptr, len); // Police running n step length police_ptr = n; if (n == lentcycle) {// If the police run, return to the starting point POLICE_PTR = WBUF; // At this time, the police and thieves were definitely in the same loop in incatchcycle = true;

Return n;

转载请注明原文地址:https://www.9cbs.com/read-113144.html

New Post(0)