Replies: 5 comments
-
Hi, thanks for your suggestions. This it is one thing I have considered for a while. Thanks for reminding. Basically all heap memory allocated by STC is already stored in the containers which allocate, so it should be fairly straight forward to upgrade the API for this, but still a bit work. Also it means some API changes, although API breaks are accepable when there are good reasons for it. I will likely go with malloc(sz) / realloc2(p, prev_sz, sz) / free2(p, sz) variants with sizes, because I still prefer to use realloc as default due to performance. realloc2() can be implemented by free2() + malloc() anyway. |
Beta Was this translation helpful? Give feedback.
-
Added old-size parameters for i_realloc() and size for i_free(). This will break some code if they use custom allocators, but is reasonable, as it should affect very little code in most cases, and is a trivial change. |
Beta Was this translation helpful? Give feedback.
-
I coded a simple test for |
Beta Was this translation helpful? Give feedback.
-
Should be fixed in in this commit. I have tested with the demos.c example which uses most of the containers. PS. If you use a custom allocator that uses a global context, you can define STC_ALLOCATOR instead of i_allocator. Include my_alloc.h on top of your file and it will be used by all containers implemented in the TU. |
Beta Was this translation helpful? Give feedback.
-
You forgot to multiply n*sz in calloc, so it works fine now. Reopen this if you find other issues. |
Beta Was this translation helpful? Give feedback.
-
Hi,
It seems STC interface to memory allocators is composed of the functions:
malloc(), calloc(), realloc()
andfree()
.I saw that the vector implementation uses
realloc()
.The problem with
realloc()
is that it could be difficult toimplement in simple memory allocators such as bump, pool or arena allocators,
because the size of the allocated area is not stored by the allocator.
So its
realloc()
will not be capable ofmemcpy()
the previous area to the new one.One possible solution is to STC to use only
malloc()
andfree()
and implement reallocation by itself.
C++ memory resources abstract interface only uses:
do_allocate()
anddo_deallocate()
.Other alternative is a weaker form of
realloc()
where the size ofprevious area is supplied:
void* realloc2(void* p, size_t prev_size, size_t new_size);
I think that support for these simple memory allocators are important for
real-time applications and games.
They also could simplify memory management for any application
if the lifetime of the allocations can be grouped.
Please, see:
Untangling Lifetimes: The Arena Allocator
Arena allocator tips and tricks
Writing custom memory allocators in C++
Also, it would be great to include in the Algorithms section
implementation of common simple memory allocators.
What are your thoughts ?
Regards.
Beta Was this translation helpful? Give feedback.
All reactions