[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
scrypt's malloc() alignment
A further change I may contribute is removing the dependency on having
an integer type matching the pointer size (uintptr_t) by implementing
the alignment with pointer math instead.
Currently, the scrypt code uses:
if ((B0 = malloc(128 * r * p + 63)) == NULL)
B = (uint8_t *)(((uintptr_t)(B0) + 63) & ~ (uintptr_t)(63));
and so on for other aligned allocations.
We can do:
void * B0;
uint8_t * B;
if ((B = B0 = malloc(128 * r * p + 63)) == NULL)
B += 63;
B -= (size_t)B & 63;
Any other integer type will do in place of size_t, although the compiler
may print a warning if there's a size mismatch (but the code will work
I think this is cleaner and safer (a mere warning on size mismatch
rather than program misbehavior at runtime).
I'd also move this into a function similar to posix_memalign() rather
than have it repeated for every allocation made by crypto_scrypt().
The "#ifdef HAVE_POSIX_MEMALIGN ... #else ... #endif" will be inside
that new function then.
Would you accept these changes?
I'll keep the mmap() for V (page alignment with no effort, guaranteed
ability to release memory back to the OS, MAP_NOCORE where available).
Overall, I am trying to implement changes that I think you're likely to
accept upstream before I proceed to hack this code harder. ;-)