AlignPtr
Align the input address to the specified number of bytes:
void* HMPP_AlignPtr(void *ptr, int32_t align);
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
ptr |
Source address |
The value cannot be NULL. |
Input |
align |
Number of bytes to be aligned |
Integral power of 2 |
Input |
Return Value
- Success: The number of bytes to be aligned is an integral power of 2, and the returned address is aligned by align.
- Failure: The number of bytes to be aligned is not an integral power of 2, and the returned address may not be aligned by align.
Error Codes
None
Note
- Ensure that the input parameters are correct because the function does not check their validity. Check the return value after calling the function. The operation result may be different from the expected.
- The interface does not provide out-of-bounds protection. Ensure that no memory overwrites occur when calling the code at the upper layer.
Example
#define BUFFER_SIZE 100
#define ALIGN_BYTE 64
void AlignPtr_Example() {
void *ptr = malloc(BUFFER_SIZE);
if (ptr != NULL) {
void *alignPtr = HMPP_AlignPtr(ptr, ALIGN_BYTE);
if ((uint64_t)alignPtr % ALIGN_BYTE == 0) {
printf("%d byte align\n", ALIGN_BYTE);
} else {
printf("not byte align\n");
}
}
}
Output:
64 byte align
Parent topic: Basic Functions