-
Notifications
You must be signed in to change notification settings - Fork 813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
aligned memory allocaion #4277
base: master
Are you sure you want to change the base?
aligned memory allocaion #4277
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the patch. You beat me to it! Pls find my review, sorry there are many of them!
Plus pls find patch to make PJ_HAS_POOL_ALT_API
work (at least compilable):
alt_pool_diff.txt
@@ -20,6 +20,8 @@ | |||
|
|||
#include <pj/string.h> | |||
|
|||
#define ALIGN_PTR(PTR,ALIGNMENT) (PTR + (-(pj_ssize_t)(PTR) & (ALIGNMENT-1))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to PJ_POOL_ALIGN_PTR, since this potentially be included as public API
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#define IS_ALIGNED(p, a) (!((uintptr_t)(p) & ((a)-1)))
this is from row.h libyuv
Shouldn't this be public too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The objective here is to prevent conflict from symbols that we (pjsip) declare with symbols from other software when the users include our header files mixed with header files from other software. That's why we put pj
/PJ
prefix everywhere.
SInce pool_i.h
can potentially be included by user (i.e. when PJ_FUNCTIONS_ARE_INLINED
is set to nonzero), all symbols declared here must be properly prefixed with pj
/PJ
.
As for IS_ALIGNED
in libyuv/row.h
, since this is not our software (although it is included in our distribution), we can't control what naming they use, and we don't want to modify it, so it is what it is.
if ((pj_size_t)(block->end - block->cur) >= size) { | ||
void *ptr = block->cur; | ||
block->cur += size; | ||
unsigned char *ptr = ALIGN_PTR(block->cur, alignment); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pls move ptr
declaration to before first statement (re: coding style)
@@ -37,28 +39,38 @@ PJ_IDEF(pj_size_t) pj_pool_get_used_size( pj_pool_t *pool ) | |||
return used_size; | |||
} | |||
|
|||
PJ_IDEF(void*) pj_pool_alloc_from_block( pj_pool_block *block, pj_size_t size ) | |||
PJ_IDEF(void*) pj_pool_alloc_from_block( pj_pool_block *block, pj_size_t alignment, | |||
pj_size_t size ) | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to check (PJ_ASSERT_RETURN
) either here or in pj_pool_aligned_alloc()
that the value of alignment
is really power of two. Sample implementation:
#define IS_POWER_OF_TWO(val) (((val)>0) && ((val) & ((val)-1))==0)
Also in pj_pool_aligned_create()
, pj_pool_allocate_find()
, and possibly others.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NB!
/* The operation below is valid for size==0.
* When size==0, the function will return the pointer to the pool
* memory address, but no memory will be allocated.
*/
c++ new() returns uniqiue address for each allocation (even for size==0). This may be better than pjsip's behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO the existing spec is okay, it can be used for example to check the pointer belongs to some buffer.
{ | ||
if (alignment < pool->alignment) | ||
alignment = pool->alignment; | ||
void *ptr = pj_pool_alloc_from_block(pool->block_list.next, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pls move ptr
declaration to before first statement (re: coding style)
{ | ||
pj_caching_pool *cp = (pj_caching_pool*)pf; | ||
pj_pool_t *pool; | ||
int idx; | ||
|
||
PJ_CHECK_STACK(); | ||
PJ_UNUSED_ARG(alignment); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be necessary since alignment
is used
if (i % 2) | ||
{ | ||
ptr = pj_pool_aligned_alloc(pool, POOL_ALIGNMENT_TEST, 1); | ||
if (!IS_ALIGNED2(ptr)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we're at it, I would like to introduce you to the new unit test API (pj/unittest.h
). :)
This block:
if (!IS_ALIGNED2(ptr))
{
pj_pool_release(pool);
pj_pool_release(pool2);
return -312;
}
can be replaced with:
PJ_TEST_TRUE(IS_ALIGNED2(ptr), NULL, {rc=-312; goto on_return; });
and replace this at the end of the function:
/* Done */
pj_pool_release(pool);
pj_pool_release(pool2);
return 0;
with:
/* Done */
on_return:
pj_pool_release(pool);
pj_pool_release(pool2);
return rc;
Plus declare and initialize rc
at the start of the function, and #define THIS_FILE "pool.c"
in the file (needed by unit test macros).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in my PR (in your repo)
{ | ||
pj_pool_release(pool); | ||
pj_pool_release(pool2); | ||
return -312; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate error value. Also consider re-sorting them, and give some space between the values (so that if we insert new test item, there is no need to replace other return values).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in my PR (in your repo)
{ | ||
pj_size_t count; | ||
count = (size + pool->increment_size + sizeof(pj_pool_block) + | ||
PJ_POOL_ALIGNMENT) / | ||
alignment) / |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same bug as above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like GH losts my long written comment... :(
It's supposed to be commenting line 134. I think there is a bug, it's not your bug but it's an existing bug. I think it should be added by 2*alignment
instead of just one alignment
. The rationale is, in worst case, the alignment is done twice. First to align start of buffer, then to align the size itself.
I can reproduce the bug with this code (it's producing assertion error in pj/pool.c:157
):
int init_size = 50 + sizeof(pj_pool_t) + sizeof(pj_pool_block);
int pool_alignment = 4;
int huge_alignment = 256;
int inc_size = 50;
void *ptr;
pj_pool_t *pool = pj_pool_aligned_create(mem, NULL, init_size, inc_size, pool_alignment,
&null_callback);
ptr = pj_pool_aligned_alloc(pool, huge_alignment, 1);
pj_pool_release(pool);
PJ_TEST_NOT_NULL(ptr, NULL, return -220);
If you don't mind you can add that to the pool's unit test
Note: the API compatibility is broken (potentially) for pool implementors and not pool users. |
Hi! I'm currently "out of my virtual office" and will be back early next week. Regarding alt_pool_diff.txt: is it possible to find this commit somewhere on GH? Or can you push it to my fork, please? |
No worries.
Done, created PR in your fork. I probably can create PR for the other fixes too. |
Currently pjsip library supports only global (library level) memory allocation alignment control.
This PR introduce more granular control of this process.
This feature allows to set the default alignment for all subsequent allocations from the pool created here.
The actual allocation alignment will be at least equal to the alignment argument of the function, but not less than PJ_POOL_ALIGNMENT. (Pool created by pj_pool_create() has the default alignment equal to PJ_POOL_ALIGNMENT.)
The second API allow to control the alignment of memory allocated by the current allocation request:
The actual alignment of the allocation will be at least equal to the alignment argument of the function, but not less than the default pool alignment specified when the pool was created.