Page 1 of 1
Memorypolls from an array
Posted: Wed Jul 27, 2011 10:26 am
by CalM
Hi
for a small CAN driver for the Kinetis K40/K60 i woud like to use a Memorypoll with a fixed amount RAM given by uint32_t Array. As i have seen in the Sourcecode, this is mentioned to be possible, but i can't figure out how this must be done. I guess the MEMORYPOOL_DECL mus t be used. I haven't figured out how to provide a pointer or something to the Array in RAM. Is this kind of memorypool possible ? Sorry for this kind of questions, but the "handbooks" are a bit weak at this point.
CalM
Re: Memorypolls from an array
Posted: Wed Jul 27, 2011 1:28 pm
by Giovanni
Do you want to add the whole array as a single object in the memory pool or each element of the array is a separate object of the pool?
If you look in the testpools.c there is an example on how adding the elements of an array into a pool:
Code: Select all
/* Pool declaration.*/
MemoryPool mp1;
...
/* Pool initialization.*/
chPoolInit(&mp1, THD_WA_SIZE(THREADS_STACK_SIZE), NULL);
...
/* Adding the WAs to the pool. */
for (i = 0; i < MAX_THREADS; i++)
chPoolFree(&mp1, wa[i]);
...
/* Empting the pool again. */
for (i = 0; i < MAX_THREADS; i++)
p = chPoolAlloc(&mp1);
In general the test code is a good place for examples, there is at least an example for each primitive.
Giovanni
Re: Memorypolls from an array
Posted: Wed Jul 27, 2011 2:46 pm
by CalM
Thanks again for the fast answare
i'll walk through the code.
CalM
Re: Memorypolls from an array
Posted: Thu Nov 17, 2011 3:14 am
by Xamusk
Sorry, but I couldn't quite grasp the Memory Pool concept from either the manuals or the test code.
I'm trying to make a malloc() alternative for a library I'm trying to incorporate (protobuf-c) which uses a lot of dynamic stuff (unnecessarily for embedded code).
First, I couldn't understand what sizes should be passed. During initialization, if we have a lot of objects to alloc/free, should we pass the maximum size of all these objects together or the size of one object?
Then, the docs say that chPoolInit isn't needed when MEMORYPOOL_DECL is used for static objects, but the example uses it anyway. I guess this is just for example and testing purposes, but maybe that should be written down in comments for the example.
Also, in my understanding of memory allocations, we should first alloc the memory and later free it. In the example, chPoolFree is called first, and chPoolAlloc is called after. I'm confused about this. To make things worse, I didn't understand why we should be messing with WAs.
In the end, I didn't really understand the part about the Memory Pool growing automatically with a provider. There's really no useful example on this.
Can you offer some insight on those topics?
Re: Memorypolls from an array
Posted: Fri Nov 18, 2011 8:35 am
by Giovanni
Probably this should be matter for a new article, it can be a bit confusing initially but it is surprisingly simple.
A pool is just a linked list of blocks with equal size. This list acts like a stack, chPoolFree()=PUSH, chPoolAlloc()=POP.
chPoolFree() <- INSERTS a block in a memory pool, so it is also used to fill a pool.
chPoolAlloc() <- REMOVES a block from a pool.
The use requires the following steps:
1) Creating a pool: chPoolInit() or MEMORYPOOL_DECL() if a static initialization is preferred. The test suite uses both for testing purposes, it is not required.
2) Preloading a pool with blocks: chPoolFree(). This can be skipped if a memory provider has been defined, in that case the pool will ask the provider for blocks when needed. chCoreAllocI() is usually used as provider.
3) Getting a block from a pool: chPoolAlloc(). The block size is intrinsically the size defined for the pool.
4) Returning a block to a pool: chPoolFree(). The pool assumes that the block is of the correct size.
WAs are not related to pools, that code simply uses the existing WAs as blocks for the pool in order to not waste memory, the test suite barely fits in the lesser 8bits micros so it is written to not waste RAM.
The advantage of pools is the very high speed when freeing and allocating blocks and the constant time execution (the time of the optional memory provided should be added).
Memorypolls with malloc() / free()
Posted: Thu May 16, 2013 11:47 am
by eric
Hi,
I've tried ChibiOS for a couple of weeks, now touching the Memorypool concept. Perhaps, I am misunderstanding about the ChibiOS'es memory management, I see that memorypool doesn't help to dynamically grow and shrink the memory, because it is created from the static memory chunks. Here is what I plan to do, I dynamically allocate each chunk with malloc() whenever I need and add it to the memorypool, use it and free() each chunk when some or all of them are no longer need. What could go wrong with this approach?
Thanks in advance,
Eric
Re: Memorypolls from an array
Posted: Thu May 16, 2013 12:18 pm
by Giovanni
Hi,
The approach is correct, memory pools support a "provider" mechanism, if you associate a provider to a pool then the pool grows automatically by querying the provider function.
The function chCoreAllocI() can be used as provider for a memory pool and will supply memory blocks taken from the common heap.
Giovanni