Skip to content

Conversation

TingDaoK
Copy link
Contributor

@TingDaoK TingDaoK commented Aug 22, 2024

Issue #, if available:

  • Add aws_explicit_aligned_allocator_new to support customized aligned instead of the default one.

Description of changes:

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

* Create an aligned allocator with customized alignment.
*/
AWS_COMMON_API
struct aws_allocator *aws_customized_aligned_allocator_new(
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe just aws_custom_alignment_allocator_new?

Copy link
Contributor

Choose a reason for hiding this comment

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

I assume it's named this way because the nice simple name aws_aligned_allocator() is already taken, but that one automagically determines the alignment, and it differs based on allocation size.

Move these new functions to be right next to the existing aws_aligned_allocator() (instead of the end of the file) so that someone searching "align" is more likely to see both options and pick the right one.

I like "explicit" more than "custom". "custom" sounds like "you can do whatever you want like via a callback" while "explicit" sounds to me like "we simply use this 1 number you give us".

I'd suggest aws_explicit_aligned_allocator() (sounds like the pre-existing allocator) or aws_explicit_alignment_allocator() (nicer sounding gramatically)

struct aws_allocator *allocator;
size_t alignment_size;
};

static void *s_aligned_malloc(struct aws_allocator *allocator, size_t size) {
(void)allocator;
Copy link
Contributor

Choose a reason for hiding this comment

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

probably not needed anymore?

*/
const size_t alignment = sizeof(void *) * (size > (size_t)PAGE_SIZE ? 8 : 2);
size_t alignment = sizeof(void *) * (size > (size_t)PAGE_SIZE ? 8 : 2);
if (allocator->impl) {
Copy link
Contributor

Choose a reason for hiding this comment

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

allocator will not have impl if its aligned by default?

Copy link
Contributor

Choose a reason for hiding this comment

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

dumb idea, but maybe just let impl be the actual alignment (i.e. just cast impl pointer to size_t and back) instead of allocating another chunk of mem for it?

@@ -34,6 +34,11 @@ bool aws_allocator_is_valid(const struct aws_allocator *alloc) {
return alloc && AWS_OBJECT_PTR_IS_READABLE(alloc) && alloc->mem_acquire && alloc->mem_release;
}

struct aws_aligned_allocator_impl {
struct aws_allocator *allocator;
Copy link
Contributor

Choose a reason for hiding this comment

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

it's really confusing to see code that's like allocator->allocator name this like underlying_allocator or parent_allocator or something

Copy link
Contributor

Choose a reason for hiding this comment

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

oh wait, reading more code ... this allocator is only used to create this one single aws_aligned_allocator_impl struct? It's not actually used for the underlying allocations, those come from posix_memalign().

Maybe just don't even bother passing an allocator here, just use aws_default_allocator() to create the impl. That's what aws_mem_tracer_new() does.

When the user passes an allocator, there's a general understanding that THIS is the allocator that will be used for further allocations. But that's not the case here. If aligned-allocations leaks, the passed-in allocator has no idea, it wasn't involved at all.

* Create an aligned allocator with customized alignment.
*/
AWS_COMMON_API
struct aws_allocator *aws_customized_aligned_allocator_new(
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume it's named this way because the nice simple name aws_aligned_allocator() is already taken, but that one automagically determines the alignment, and it differs based on allocation size.

Move these new functions to be right next to the existing aws_aligned_allocator() (instead of the end of the file) so that someone searching "align" is more likely to see both options and pick the right one.

I like "explicit" more than "custom". "custom" sounds like "you can do whatever you want like via a callback" while "explicit" sounds to me like "we simply use this 1 number you give us".

I'd suggest aws_explicit_aligned_allocator() (sounds like the pre-existing allocator) or aws_explicit_alignment_allocator() (nicer sounding gramatically)

@@ -34,6 +34,11 @@ bool aws_allocator_is_valid(const struct aws_allocator *alloc) {
return alloc && AWS_OBJECT_PTR_IS_READABLE(alloc) && alloc->mem_acquire && alloc->mem_release;
}

struct aws_aligned_allocator_impl {
struct aws_allocator *allocator;
size_t alignment_size;
Copy link
Contributor

Choose a reason for hiding this comment

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

trivial:

Suggested change
size_t alignment_size;
size_t alignment;

@@ -34,6 +34,11 @@ bool aws_allocator_is_valid(const struct aws_allocator *alloc) {
return alloc && AWS_OBJECT_PTR_IS_READABLE(alloc) && alloc->mem_acquire && alloc->mem_release;
}

struct aws_aligned_allocator_impl {
Copy link
Contributor

Choose a reason for hiding this comment

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

use "explicit" or "custom" in this struct name (match the public function name)

Otherwise, one would assume this is for aws_aligned_allocator()

Which is extra confusing because both share the s_aligned_malloc() call, and it's tough to explain "you can tell that it's NOT the aws_aligned_allocator() because it uses the aws_aligned_allocator_impl" 🤯🤯🤯

Copy link
Contributor

@graebm graebm Aug 23, 2024

Choose a reason for hiding this comment

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

OR make it so that both aws_aligned_allocator() and the new one BOTH use this struct. They both have an impl but member size_t explicit_alignment is 0 for aws_aligned_allocator().

then in s_aligned_malloc(), you can simply branch on whether or not impl->explicit_alignment is set

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Go with @DmitriyMusatkin 's suggestion to remove the whole impl, and use the point address to store the size_t for the alignment.

@codecov-commenter
Copy link

codecov-commenter commented Aug 23, 2024

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.26%. Comparing base (7b24a8a) to head (4b30c38).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1147      +/-   ##
==========================================
+ Coverage   84.22%   84.26%   +0.03%     
==========================================
  Files          57       57              
  Lines        5992     6006      +14     
==========================================
+ Hits         5047     5061      +14     
  Misses        945      945              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TingDaoK TingDaoK changed the title aws_customized_aligned_allocator aws_explicit_aligned_allocator_new Aug 6, 2025
@TingDaoK TingDaoK merged commit cde20c6 into main Aug 6, 2025
57 checks passed
@TingDaoK TingDaoK deleted the align-allocator-size branch August 6, 2025 23:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants