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

aws_customized_aligned_allocator #1147

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

aws_customized_aligned_allocator #1147

wants to merge 7 commits into from

Conversation

TingDaoK
Copy link
Contributor

Issue #, if available:

  • Add aws_customized_aligned_allocator 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.

source/allocator.c Outdated Show resolved Hide resolved
* 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 83.86%. Comparing base (88f0ea3) to head (095b34f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1147      +/-   ##
==========================================
+ Coverage   83.82%   83.86%   +0.03%     
==========================================
  Files          57       57              
  Lines        5992     6005      +13     
==========================================
+ Hits         5023     5036      +13     
  Misses        969      969              

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

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