Open Bug 666088 Opened 14 years ago Updated 1 year ago

Write an Aligned Allocator helper

Categories

(Core :: General, defect)

Other Branch
defect

Tracking

()

People

(Reporter: azakai, Unassigned)

References

Details

We sometimes need aligned allocations for things like SIMD. Doing this manually in each case (that is, allocating a bit more than needed, then working on an aligned address) is possible but a helper class could make our lives easier. For example, here is how Bullet does this: http://www.bulletphysics.com/Bullet/BulletFull/btAlignedAllocator_8h.html Basically, a class that holds the raw result of malloc, and is templated on the alignment. Then it can return a pointer to the aligned address. Any objections?
Isn't this what memalign is for? It would certainly be nice if we could statically type aligned pointers so we don't have to guess their alignment.
Yes, memalign does exactly this. But just on *NIX I believe.
There's _aligned_malloc on win32.
jemalloc also exports a memalign-type thing. Since we're going to have jemalloc on all tier-1 platforms RSN, I don't care if we need extra machinery in this smart pointer to support other allocators if we can make it just as fast with jemalloc as if we didn't have the wrapper.
We discussed on IRC whether __attribute__((aligned(X))) would work, but it won't. <glandium> "Note that normal allocators, such as malloc, C++ operator new, and the Win32 allocators return memory that will most likely not be sufficiently aligned for __declspec(align(#)) structures or arrays of structures." So I think the way to do this is to have a function which delegates to memalign / jemalloc's memalign when it's available, or does what's suggested in comment 0 when it's not. That class would return a smart pointer which knows how to free itself. When memalign is available, it just calls free(), otherwise it does what's in comment 0.
memalign will always use the default malloc though, won't it? Seems like we should allow customizing that I think.
(In reply to comment #6) > memalign will always use the default malloc though, won't it? Seems like we > should allow customizing that I think. I guess what you're saying is that both types of smart pointers (the one which is just a wrapper around memalign/free and the one which calls malloc, or custom_malloc, and adjusts the pointer) should exist, regardless of whether we're building with jemalloc. Then you can hook up your custom allocator to whichever type is appropriate. That doesn't sound so bad, but I think YAGNI, and this would just make designing a fast thin aligned pointer class more difficult. Do you have a custom allocator in mind that you want to hook up to? I'd rather implement what we need now, rather than what we might need.
I was thinking about custom allocators in the JS engine. A list of our custom allocators appears in one of the commands here: http://blog.mozilla.com/nnethercote/2011/01/07/memory-profiling-firefox-with-massif-part-2/
See Also: → 1772175
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.