use of MemPool

5 posts / 0 new
Last post
OldFart
OldFart's picture
Offline
Last seen: 2 days 2 hours ago
Joined: 2010-11-30 14:09
use of MemPool

Hi,

Ok, during summer months here in the Northern Hemisphere, my coding efforts are pretty darn low. However, every now and then I come up with some little things (issues) that I think need to be sorted out.

For about all of my projects I make use of a mempool, as I once came to the conclusion, that that was a good idea, mainly because it allows to integrally set the allocated area to 0x00 all over upon successfull allocation. This effectively means that in an allocated structure Booleans are set to FALSE, numbers to 0 and char arrays to 'terminated'. This part sets up the mempool:

  1. void *MemPool = IExec->AllocSysObjectTags(ASOT_MEMPOOL, ASOPOOL_MFlags , MEMF_CLEAR
  2. | MEMF_VIRTUAL
  3. , ASOPOOL_Puddle , 4096
  4. , ASOPOOL_Threshold, 1024
  5. , TAG_END);

Can this be improved for efficiency?
What is the effect of the 'Treshold'-tag? What exactly does that do?

OldFart

msteed
msteed's picture
Offline
Last seen: 4 weeks 1 day ago
Joined: 2022-01-18 08:34
Re: use of MemPool

I haven't had occasion to use memory pools myself, but the OS4 Wiki has a section on them and how they work, including what the threshold value does and how to adjust it: https://wiki.amigaos.net/wiki/Exec_Memory_Pools

trixie
trixie's picture
Offline
Last seen: 3 days 11 hours ago
Joined: 2011-02-03 13:58
Re: use of MemPool

@OldFart

Can this be improved for efficiency?

When it comes to memory allocation efficiency, the first question you need to ask yourself is: "Am I going to allocate and free memory repeatedly?" If no, then you don't need pools, and standard mem allocations using AllocVecTags() will do for you.

The second question is: "Am I going to always allocate the same size, or different sizes?" If allocating the same size, use an item pool instead, the allocations will be faster.

Other than that, I'd point out that you shouldn't use the MEMF_VIRTUAL flag. Use MEMF_PRIVATE, or MEMF_SHARED if the memory is going to be shared between different tasks.

AmigaOne X5000-020 / 2GB RAM / Sapphire Pulse Radeon RX 560 / AmigaOS 4.1 Final Edition Update 2

OldFart
OldFart's picture
Offline
Last seen: 2 days 2 hours ago
Joined: 2010-11-30 14:09
Re: use of MemPool

@all

The first and formost reason I switched to using a mempool, was the reduction of memory fragmentation, the abillity to allocate 'clean' (= set to 0) memory and, due to the 'tracking' nature of the mempool, the reduced risk of memory leakage due to memory not being freed explicitly at closing time. The mempool takes care of that, as far as I can read from the documentation.

After the mempool has been established, first thing after starting the program, I make use of one of the following macro's for allocation

  1. #define _AllocType(Type) (Type *)IExec->AllocVecPooled(xn->xn_MemPool,sizeof(Type ) )
  2. #define _AllocTypeArray(Type,Qty) (Type *)IExec->AllocVecPooled(xn->xn_MemPool,sizeof(Type )*Qty)

and of course a macro for freeing:

  1. #define _FreeAlloc(Area) IExec->FreeVecPooled(xn->xn_MemPool, Area); Area = NULL

Might my insight on the matter change, a simple replacement of these macro's would be enough, beit definitive or just one time.

Thanks for all your input. Much appreciated!

OldFart

salass00
salass00's picture
Offline
Last seen: 4 days 23 hours ago
Joined: 2011-02-03 11:27
Re: use of MemPool

What is the effect of the 'Treshold'-tag? What exactly does that do?

If an allocation is lower or equal to the threshold value then AllocPooled() and there is no existing puddle that can satisfy the request then it will allocate a new puddle of the specified puddle size and leave any extra memory to potentially be used by future AllocPooled() calls.

If the allocation exceeds the threshold value then AllocPooled will allocate exactly the amount of memory that is requested and not a byte more.

Basically you want to set the threshold to just large enough that when you subtract it from the puddle size it will leave just enough free memory to still be useful.

The main benefit of the puddles is that if the allocation request can be satisfied by an existing puddle it will be faster because it won't have to request new memory from the free store. If you only do allocations rarely and only use the memory pool to keep track of allocations you might want to set puddle and threshold sizes to zero so that it always allocates only as much memory as needed.

Log in or register to post comments