Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please explain what this piece of C code does

Tags:

c

Can someone explain what is this piece of code do?

pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1);

/* offset for mmap() must be page aligned */

I understand that here sysconf returns the page size, which lets assume that it is 4096, but after that I am not able to understand the logic. Thanks in advance.

like image 519
hue Avatar asked Dec 06 '22 23:12

hue


1 Answers

If sysconf returns the page size, which is a power of two or 00..00100..00 in binary, - 1 makes a mask of that number (that is, it makes a number of the form 00..0011..11, then ~ computes the reverse of this mask (11..1100..00). Lastly, the bitwise and & operation between the newly created mask and offset rounds offset down to the nearest multiple of the page size.

like image 118
Pascal Cuoq Avatar answered Dec 28 '22 18:12

Pascal Cuoq