Align Pointer to Boundary

Easy C Memory Asked at Western Digital
Solve this problem

DMA engines usually require buffers aligned to a power-of-two boundary. Write a helper that rounds a pointer up to the next aligned address.

Constraints

  • Signature: `void *align_up(void *ptr, size_t alignment)`.
  • alignment is guaranteed to be a power of two.
  • An already-aligned pointer must be returned unchanged.
  • Use uintptr_t for the arithmetic, not casting through int.
  • No branches in the alignment computation.
Starter codeC
1#include <stdint.h>2#include <stddef.h>3 4void *align_up(void *ptr, size_t alignment) {5    // TODO6    return ptr;7}