Skip to content
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 allocation #4277

Merged
merged 7 commits into from
Feb 6, 2025

Conversation

LeonidGoltsblat
Copy link
Contributor

Currently pjsip library supports only global (library level) memory allocation alignment control.
This PR introduce more granular control of this process.

PJ_IDECL(pj_pool_t*) pj_pool_aligned_create(pj_pool_factory *factory,
                                            const char *name,
                                            pj_size_t initial_size,
                                            pj_size_t increment_size,
                                            pj_size_t alignment,
                                            pj_pool_callback *callback);

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:

PJ_IDECL(void *) pj_pool_aligned_alloc(pj_pool_t *pool, pj_size_t alignment,
                                       pj_size_t size);

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.

Copy link
Member

@bennylp bennylp left a 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

pjlib/include/pj/pool_i.h Outdated Show resolved Hide resolved
pjlib/include/pj/pool_i.h Outdated Show resolved Hide resolved
pjlib/include/pj/pool_i.h Show resolved Hide resolved
pjlib/include/pj/pool_i.h Outdated Show resolved Hide resolved
pjlib/src/pj/pool_caching.c Outdated Show resolved Hide resolved
pjlib/src/pjlib-test/pool.c Outdated Show resolved Hide resolved
pjlib/src/pjlib-test/pool.c Outdated Show resolved Hide resolved
pjlib/src/pj/pool.c Outdated Show resolved Hide resolved
@bennylp
Copy link
Member

bennylp commented Jan 30, 2025

Note: the API compatibility is broken (potentially) for pool implementors and not pool users.

@LeonidGoltsblat
Copy link
Contributor Author

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?

@bennylp
Copy link
Member

bennylp commented Jan 30, 2025

Hi! I'm currently "out of my virtual office" and will be back early next week.

No worries.

Regarding alt_pool_diff.txt: is it possible to find this commit somewhere on GH? Or can you push it to my fork, please?

Done, created PR in your fork. I probably can create PR for the other fixes too.

bennylp and others added 4 commits February 1, 2025 08:53
Pool Alt API and unit testing
…ligned-memory-allocaion

# Conflicts:
#	pjlib/src/pjlib-test/pool.c
@LeonidGoltsblat
Copy link
Contributor Author

please review

@bennylp bennylp changed the title aligned memory allocaion Aligned memory allocation Feb 6, 2025
Copy link
Member

@sauwming sauwming left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor only

@@ -331,6 +331,10 @@ struct pj_pool_t
/** The callback to be called when the pool is unable to allocate memory. */
pj_pool_callback *callback;

/** The default alignment of memory block allocated from this pool
* (must be power of 2). */
size_t alignment;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pj_size_t ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! However see also line 952

    /**
     * Internal pool.
     */
    char            pool_buf[256 * (sizeof(size_t) / 4)];

@@ -46,6 +46,7 @@ struct pj_pool_t
char obj_name[32];
pj_size_t used_size;
pj_pool_callback *cb;
size_t alignment;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pj_size_t

PJ_ASSERT_RETURN(!alignment || PJ_IS_POWER_OF_TWO(alignment), NULL);

if (alignment < PJ_POOL_ALIGNMENT)
alignment = PJ_POOL_ALIGNMENT;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this is not specified in the doc, that alignment can't be smaller than PJ_POOL_ALIGNMENT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added to doc

@LeonidGoltsblat
Copy link
Contributor Author

  • pool_dbg alignment support
  • incompatible tests disabled for PJ_HAS_POOL_ALT_API

@bennylp bennylp merged commit e6196ad into pjsip:master Feb 6, 2025
39 of 42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants