Memory cache is a simple memory for reading and writing operation of a transmission, any time to write data on the above is put into the cache, any time read the operation data is also to the cache. The allocation of memory cache uses the C language malloc class function, the allocation length is twice times the length, needs to consider these memory cache usage scope.
The same class inherits to the cache base class Tbufferbase, the default cache size is 1024 (static const uint32_t defaultsize = 1024;), and all constructors call function Initcommon, which is implemented as follows:
Voidinitcommon (uint8_t* buf, uint32_t size, bool
owner, uint32_t wpos) {
if (buf== NULL && size!= 0) {//AS The BUF passed over is null and the size is not 0
assert (owner),//asserts that the memory cache is not all of its own, not the error does not continue to execute the following code
BUF = (uint8_t*) std::malloc ( size);/is the memory cache that you own is allocating the size of the memory itself as cache
if (buf = = NULL) {
throw std::bad_alloc ();//allocation failure throw exception
}
}
buffer_= buf;//Initialize the cache member variable
buffersize_ = size;//size
rbase_= buffer_; base Address
rbound_= Buffer_ + wpos;//bounds
wbase_= Buffer_ + wpos;//Write cache base Address
wbound_= Buffer_ + buffersize_;//Write boundaries
owner_= Whether you own this memory cache
}
The code above needs to explain that when initializing this class there is a different approach to whether the memory cache belongs to the class itself, followed by the guidelines, and the point is that the boundary of the reading is actually the starting address of the write, because the place where the writing begins is where there is no valid data, is a problem for the survivor and the consumer, and a function described later in this computeread updates the relevant content. Let's take a look at several configurable guidelines for the memory cache (how to treat the memory cache), which defines an enumeration to identify each guideline, defined as follows:
Enummemorypolicy
{observe= 1
, COPY =2
, take_ownership = 3
};
The following distribution analysis of these three approaches, the first of which is called the observation guideline, the memory cache transport class simply stores a pointer to memory, which is the caller to ensure that the memory cache points to a valid memory for the remainder of the time, and cleans it up at the appropriate time (note: This memory cache is not allowed to write data) The second is a copy of the inside of the memory cache class, the caller has no responsibility; the last is the memory cache that the memory cache class owns, which must be released itself, and it needs to be allocated using malloc before use. Understand the three of memory guidelines, you know when you need which. Memory usage In addition to being used by constructors, we also use it when resetting memory buffers, while the reset cache function is Resetbuffer and has two function overloads. In the implementation of the overloaded function is a little bit of optimization, is the exchange of memory when the use of Std::swap, the advantage of this function is if the same memory allocator allocated on the direct exchange of pointers to the head.