1 #include "test/jemalloc_test.h"
2 
3 chunk_alloc_t *old_alloc;
4 chunk_dalloc_t *old_dalloc;
5 chunk_purge_t *old_purge;
6 bool purged;
7 
8 void *
chunk_alloc(void * new_addr,size_t size,size_t alignment,bool * zero,unsigned arena_ind)9 chunk_alloc(void *new_addr, size_t size, size_t alignment, bool *zero,
10     unsigned arena_ind)
11 {
12 
13 	return (old_alloc(new_addr, size, alignment, zero, arena_ind));
14 }
15 
16 bool
chunk_dalloc(void * chunk,size_t size,unsigned arena_ind)17 chunk_dalloc(void *chunk, size_t size, unsigned arena_ind)
18 {
19 
20 	return (old_dalloc(chunk, size, arena_ind));
21 }
22 
23 bool
chunk_purge(void * chunk,size_t offset,size_t length,unsigned arena_ind)24 chunk_purge(void *chunk, size_t offset, size_t length, unsigned arena_ind)
25 {
26 
27 	purged = true;
28 	return (old_purge(chunk, offset, length, arena_ind));
29 }
30 
TEST_BEGIN(test_chunk)31 TEST_BEGIN(test_chunk)
32 {
33 	void *p;
34 	chunk_alloc_t *new_alloc;
35 	chunk_dalloc_t *new_dalloc;
36 	chunk_purge_t *new_purge;
37 	size_t old_size, new_size, huge0, huge1, huge2, sz;
38 
39 	new_alloc = chunk_alloc;
40 	new_dalloc = chunk_dalloc;
41 	new_purge = chunk_purge;
42 	old_size = sizeof(chunk_alloc_t *);
43 	new_size = sizeof(chunk_alloc_t *);
44 
45 	assert_d_eq(mallctl("arena.0.chunk.alloc", &old_alloc, &old_size,
46 	    &new_alloc, new_size), 0, "Unexpected alloc error");
47 	assert_ptr_ne(old_alloc, new_alloc, "Unexpected alloc error");
48 
49 	assert_d_eq(mallctl("arena.0.chunk.dalloc", &old_dalloc, &old_size,
50 	    &new_dalloc, new_size), 0, "Unexpected dalloc error");
51 	assert_ptr_ne(old_dalloc, new_dalloc, "Unexpected dalloc error");
52 
53 	assert_d_eq(mallctl("arena.0.chunk.purge", &old_purge, &old_size,
54 	    &new_purge, new_size), 0, "Unexpected purge error");
55 	assert_ptr_ne(old_purge, new_purge, "Unexpected purge error");
56 
57 	sz = sizeof(size_t);
58 	assert_d_eq(mallctl("arenas.hchunk.0.size", &huge0, &sz, NULL, 0), 0,
59 	    "Unexpected arenas.hchunk.0.size failure");
60 	assert_d_eq(mallctl("arenas.hchunk.1.size", &huge1, &sz, NULL, 0), 0,
61 	    "Unexpected arenas.hchunk.1.size failure");
62 	assert_d_eq(mallctl("arenas.hchunk.2.size", &huge2, &sz, NULL, 0), 0,
63 	    "Unexpected arenas.hchunk.2.size failure");
64 	if (huge0 * 2 > huge2) {
65 		/*
66 		 * There are at least four size classes per doubling, so
67 		 * xallocx() from size=huge2 to size=huge1 is guaranteed to
68 		 * leave trailing purgeable memory.
69 		 */
70 		p = mallocx(huge2, 0);
71 		assert_ptr_not_null(p, "Unexpected mallocx() error");
72 		purged = false;
73 		assert_zu_eq(xallocx(p, huge1, 0, 0), huge1,
74 		    "Unexpected xallocx() failure");
75 		assert_true(purged, "Unexpected purge");
76 		dallocx(p, 0);
77 	}
78 
79 	p = mallocx(42, 0);
80 	assert_ptr_not_null(p, "Unexpected mallocx() error");
81 	free(p);
82 
83 	assert_d_eq(mallctl("arena.0.chunk.alloc", NULL, NULL, &old_alloc,
84 	    old_size), 0, "Unexpected alloc error");
85 	assert_d_eq(mallctl("arena.0.chunk.dalloc", NULL, NULL, &old_dalloc,
86 	    old_size), 0, "Unexpected dalloc error");
87 	assert_d_eq(mallctl("arena.0.chunk.purge", NULL, NULL, &old_purge,
88 	    old_size), 0, "Unexpected purge error");
89 }
90 TEST_END
91 
92 int
main(void)93 main(void)
94 {
95 
96 	return (test(test_chunk));
97 }
98