C ++ - Memory Alignment

I really want to understand what is happening on these two lines.

const int PAGES = 8 * 1024;

// PAGES + extra 4KiB for alignment
uint8_t * mem = new uint8_t [ PAGES * CCPU::PAGE_SIZE + CCPU::PAGE_SIZE ];

// align to a mutiple of 4KiB
uint8_t * memAligned = (uint8_t *) (( ((uintptr_t) mem) + CCPU::PAGE_SIZE - 1) & ~(uintptr_t) ~CCPU::ADDR_MASK );

especially the last line, I don’t understand anything ...

+4
source share
1 answer

It selects a pointer to a block of aligned pages, i.e. the PAGESnumber of pages using C ++ dispatchers instead of the more specialized dedicated highlighting functions (e.g. POSIX posix_memalignor C11aligned_alloc ).

PAGES + 1 ( ), , , . , , , , , PAGES . , delete mem, , memAligned ( , , , - , undefined, ).

; PAGE_SIZE - 1 (, , , ), ( " " , , mem).

: ~ , ADDR_MASK, , , - 0x00000FFF 4096 , 0xFFFFF000 ( ). & - , , . : 32- , new 0xDEADBEEF, PAGE_SIZE - 4096. 4095 (0xFFF) , "0xDEADCEEE" . 0xFFFFF000, , 0xDEADC000, , 0xDEADBEEF. , , new.

, , 0xDEADB000, 4095/0xFFF 0xDEADBFFF ( , 0xDEADB ), , , , 0xDEADB000 , .

uintptr_t , , , ( , , , , , , ).

+6

All Articles