Bonjour,
J'ai quelques soucis quand j'essaie d'utiliser mmap()
Voici mon code :
mon main :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 puts("use mmap"); void *p; p = mmap(0, s, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); if (p != MAP_FAILED) { puts("mmap success"); return (p); } return (NULL);
ma compil : gcc -g blocks.c getlimits.c my_free.c my_malloc.c && valgrind ./a.out
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 int main() { printf("MMAP THRESHOLD : %d\n", MY_MMAP_THRESHOLD); base = NULL; size_t s = 262150; char *d; if ((d = my_malloc(s)) == NULL) printf("mmap casse\n"); exit(1); char *src = "test1\n"; printf("this is d : %s\n", strcpy(d, src)); munmap(d, s); //my_free(d); char *d1; if ((d1 = my_malloc(20)) == NULL) printf("malloc casse\n"); exit(1); char *s1 = "test 2\n"; printf("%s\n", strcpy(d1, s1)); my_free(d1); }
et mon output:
│==6878== Memcheck, a memory error detector
│==6878== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
│==6878== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
│==6878== Command: ./a.out
│==6878==
│MMAP THRESHOLD : 262144
│use mmap
│mmap success
│==6878==
│==6878== HEAP SUMMARY:
│==6878== in use at exit: 0 bytes in 0 blocks
│==6878== total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
│==6878==
│==6878== All heap blocks were freed -- no leaks are possible
│==6878==
│==6878== For counts of detected and suppressed errors, rerun with: -v
│==6878== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Pourquoi le 1er strcpy n'arrive pas à récupérer la région mappée ?
Ca fonctionne bien avec sbrk(), j'aimerais reproduire ce comportement avec mmap.
Merci d'avance
Partager