examples

Toy examples in single C files.
git clone git://henryandlizzy.uk/examples
Log | Files | Refs

mremap.c (563B)


      1 #define _GNU_SOURCE
      2 #include <sys/mman.h>
      3 #include <stdlib.h>
      4 #include <stdio.h>
      5 
      6 int main()
      7 {
      8 	unsigned long long* mem;
      9 	size_t size = 0x1000;
     10 	mem = mmap((void*)0x1000000, size, PROT_READ | PROT_WRITE,  MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
     11 
     12 	unsigned i;
     13 	for (i = 0; i < 0x1000; ++i)
     14 	{
     15 		if (i * sizeof (*mem) >= size)
     16 		{
     17 			if(mem != mremap(mem, size, size * 2, 0))
     18 				exit(1);
     19 			size = size*2;
     20 		}
     21 		mem[i] = i % 256;
     22 	}
     23 
     24 	unsigned long long total = 0;
     25 
     26 	while (i--)
     27 		total += mem[i];
     28 	printf("total = %llx\n", total);
     29 
     30 	munmap(mem, size);
     31 }