Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preallocating memory with C++ in realtime environment

I'm having a function which gets an input buffer of n bytes, and needs an auxillary buffer of n bytes in order to process the given input buffer.

(I know vector is allocating memory at runtime, let's say that I'm using a vector which uses static preallocated memory. Imagine this is NOT an STL vector.)

The usual approach is

void processData(vector<T> &vec) {
    vector<T> &aux = new vector<T>(vec.size()); //dynamically allocate memory
    // process data
}
//usage:
processData(v)

Since I'm working in a real time environment, I wish to preallocate all the memory I'll ever need in advance.

The buffer is allocated only once at startup. I want that whenever I'm allocating a vector, I'll automatically allocate auxillary buffer for my processData function.

I can do something similar with a template function

static void _processData(vector<T> &vec,vector<T> &aux) {
     // process data
}
template<size_t sz>
void processData(vector<T> &vec) {
    static aux_buffer[sz];
    vector aux(vec.size(),aux_buffer); // use aux_buffer for the vector
    _processData(vec,aux);
}
// usage:
processData<V_MAX_SIZE>(v);

However working alot with templates is not much fun (now let's recompile everything since I changed a comment!), and it forces me to do some bookkeeping whenever I use this function.

Are there any nicer designs around this problem?

like image 296
Elazar Leibovich Avatar asked May 12 '10 13:05

Elazar Leibovich


People also ask

What does Preallocating memory do?

Memory Preallocation This allocation was added to prevent players from starting the game with low memory, but this allocation is kept until its almost the memory reaching its limit. This was made just for players using modpacks, where memory might be a problem.

How do I allocate more memory to CPP?

You can allocate memory at run time within the heap for the variable of a given type using a special operator in C++ which returns the address of the space allocated. This operator is called new operator.

Is dynamic memory allocation slow?

Dynamic memory allocation and deallocation are very slow operations when compared to automatic memory allocation and deallocation. In other words, the heap is much slower than the stack.

How do you allocate memory in AC program vs a C++ program?

C uses malloc and free for memory allocation while C++ uses new and delete for memory allocation. C does not provide direct support for error handling, while C++ supports exception handling that helps in error detection and smooth handling.


3 Answers

I don't see how you can get precisely what you describe. Something like this could be a good compromise for you.

void processData(vector<T>& vec)
{
    static vector<T> aux(vec.size());
    if (vec.size() > aux.size()) {
       aux.resize(vec.size());
    }
    ...
}
like image 142
AProgrammer Avatar answered Oct 11 '22 17:10

AProgrammer


I think you could pre-allocate and mlock() a sufficiently large memory pool on startup, and then use regular STL containers with memory pool allocators (Boost of FSBA or your own).

I was looking into that for our real-time software, but tests showed that memory allocation is fast enough on our hardware for our purposes.

like image 22
Cubbi Avatar answered Oct 11 '22 18:10

Cubbi


vector aux(vec.size(),aux_buffer); // use aux_buffer for the vector

Is that new in STL? Or a custom extension?

The typical solution would be using a custom allocator. However, this isn't necessarily "more pretty" in code.

Some intro slides (warning: powerpoint!)
Wikipedia
Google

like image 39
peterchen Avatar answered Oct 11 '22 17:10

peterchen