2 #include "tests/sys_mman.h"
6 #include "../memcheck.h"
8 #define SUPERBLOCK_SIZE 100000
11 static const int USE_MMAP
= 0;
13 typedef struct _level_list
15 struct _level_list
*next
;
17 // Padding ensures the struct is the same size on 32-bit and 64-bit
19 char padding
[16 - 2*sizeof(char*)];
22 typedef struct _pool
{
27 // Padding ensures the struct is the same size on 32-bit and 64-bit
29 char padding
[24 - 3*sizeof(char*)];
37 p
= (pool
*)mmap(0, sizeof(pool
), PROT_READ
|PROT_WRITE
|PROT_EXEC
,
38 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0);
39 p
->where
= p
->mem
= (char *)mmap(NULL
, SUPERBLOCK_SIZE
,
40 PROT_READ
|PROT_WRITE
|PROT_EXEC
,
41 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0);
43 p
= (pool
*)malloc(sizeof(pool
));
44 p
->where
= p
->mem
= (char *)malloc(SUPERBLOCK_SIZE
);
47 p
->size
= p
->left
= SUPERBLOCK_SIZE
;
49 (void) VALGRIND_MAKE_MEM_NOACCESS(p
->where
, SUPERBLOCK_SIZE
);
58 l
= (level_list
*)mmap(0, sizeof(level_list
),
59 PROT_READ
|PROT_WRITE
|PROT_EXEC
,
60 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0);
62 l
= (level_list
*)malloc(sizeof(level_list
));
66 VALGRIND_CREATE_MEMPOOL(l
->where
, REDZONE_SIZE
, 0);
72 level_list
*l
= p
->levels
;
74 VALGRIND_DESTROY_MEMPOOL(l
->where
);
75 (void) VALGRIND_MAKE_MEM_NOACCESS(l
->where
, p
->where
-l
->where
);
78 munmap(l
, sizeof(level_list
));
83 void destroy_pool(pool
*p
)
85 level_list
*l
= p
->levels
;
91 munmap(p
->mem
, SUPERBLOCK_SIZE
);
92 munmap(p
, sizeof(pool
));
99 char *allocate(pool
*p
, int size
)
102 p
->left
-= size
+ (REDZONE_SIZE
*2);
103 where
= p
->where
+ REDZONE_SIZE
;
104 p
->where
+= size
+ (REDZONE_SIZE
*2);
105 VALGRIND_MEMPOOL_ALLOC(p
->levels
->where
, where
, size
);
109 //-------------------------------------------------------------------------
111 //-------------------------------------------------------------------------
115 char *x1
, *x2
, *x3
, *x4
, *x5
;
117 pool
*p
= make_pool();
121 x1
= allocate(p
, 10);
122 x2
= allocate(p
, 20);
124 x3
= allocate(p
, 10);
125 x4
= allocate(p
, 20);
130 x1
[-1] = 'h'; // invalid
131 x1
[10] = 'i'; // invalid
135 *x3
= 'c'; // invalid
136 *x4
= 'd'; // invalid
141 x5
= allocate(p
, 10);
147 // *x5 = 'g'; // invalid