2
#include <unistd.h>
(size_t) sysconf(_SC_PAGESIZE);

sysconf(_SC_PAGESIZE) tells me that my memory page size is 4096 on my operating system and processor. Of the 4096 bytes in the memory page, how many can be used for data and how much is overhead/metadata?

I have an application where I am optimizing cache locality by packing frequently accessed pointers into the same cache block and need to know if the whole memory page is usable, or if I will go over memory page boundaries by filling the whole memory page.

HaltingState
  • 1,810
  • 1
  • 20
  • 22

1 Answers1

5

No metadata. 4096 bytes are usable.
The OS does store metadata somewhere, but it's on other pages, which shouldn't bother you.

Whether or not you can access memory, however, doesn't depend on the page size. If you have allocated 100 bytes, you can access just 100 bytes. If you have allocated 4096, you can access 4096.

ugoren
  • 16,023
  • 3
  • 35
  • 65
  • Ok. The metadata is not stored in the allocated page. That is what I needed to know. Someone else suggested that the metadata was prefixed in the allocated page. – HaltingState Feb 19 '12 at 05:33
  • When using `malloc`, metadata is indeed prefixed to the allocated data. But still, if you allocated 4096 bytes, you can use 4096 bytes starting from the pointer you got. – ugoren Feb 19 '12 at 08:17
  • That contradicts the second line of your answer, where you state that the metadata is not stored in the block returned by malloc, but in other pages. – HaltingState Feb 19 '12 at 22:47
  • There's no metadata between the pointer returned by `malloc` to that pointer plus the size. I did write "on other pages", which is indeed inaccurate. I'll edit it. – ugoren Feb 20 '12 at 07:22