9 #define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
12 static void printvm() {
13 int fd
= open("/proc/self/smaps", O_RDONLY
);
17 printf("/proc/self/smaps:\n");
19 while ((n
= read(fd
, buf
, sizeof buf
)) > 0)
24 static size_t mmapmax(int fd
, void **p
) {
27 for (i
=j
=SIZE_MAX
/2+1; i
>=PAGE_SIZE
; i
/=2) {
28 if ((*p
=mmap(NULL
, j
, PROT_NONE
, MAP_PRIVATE
, fd
, 0)) == MAP_FAILED
)
36 if (n
&& (*p
=mmap(NULL
, n
, PROT_NONE
, MAP_PRIVATE
, fd
, 0)) == MAP_FAILED
) {
37 fprintf(stderr
, "failed to mmap the same amount again.\n");
43 int main(int argc
, char *argv
[]) {
44 const int fd
= open("/dev/zero", O_RDWR
);
51 if (argc
== 2 && argv
[1][0]=='-' && argv
[1][1]=='v')
54 for (i
=0; i
<sizeof p
/sizeof *p
; i
++) {
55 n
[i
] = mmapmax(fd
, p
+i
);
59 printf("%d %16zu B %012zx-%012zx\n", i
, n
[i
], (size_t)p
[i
], (size_t)p
[i
]+n
[i
]);
61 printf("mmaped %zu B in %d blocks.\n", sum
, i
);
63 printf("try malloc:");
64 for(i
=0; i
< 1000 && malloc(PAGE_SIZE
) != NULL
; i
++)
66 printf(" %zu B\n", i
*(size_t)PAGE_SIZE
);