77#include  <errno.h>   
88
99#define  MAP_HUGE_2MB     (21 << MAP_HUGE_SHIFT)  
10- #define  MAP_HUGE_1GB     (30 << MAP_HUGE_SHIFT)  
1110
1211// 内存读写测试函数   
1312void  test_memory_rw (void  * ptr , size_t  size , const  char  * page_type ) {  
@@ -47,17 +46,8 @@ void test_individual_alloc_rw_free() {
4746    printf ("2MB page allocated at %p\n" , ptr_2m );  
4847    test_memory_rw (ptr_2m , 2  *  1024  *  1024 , "2MB" );  
4948    assert (munmap (ptr_2m , 2  *  1024  *  1024 ) ==  0 );  
50-     printf ("2MB page freed\n" );  
51-       
52-     // 1GB 页面测试   
53-     void  * ptr_1g  =  mmap (NULL , 1024  *  1024  *  1024 , PROT_READ  | PROT_WRITE ,  
54-                         MAP_PRIVATE  | MAP_ANONYMOUS  | MAP_HUGETLB  | MAP_HUGE_1GB , -1 , 0 );  
55-     assert (ptr_1g  !=  MAP_FAILED );  
56-     printf ("1GB page allocated at %p\n" , ptr_1g );  
57-     test_memory_rw (ptr_1g , 1024  *  1024  *  1024 , "1GB" );  
58-     assert (munmap (ptr_1g , 1024  *  1024  *  1024 ) ==  0 );  
59-     printf ("1GB page freed\n" );  
60-       
49+     printf ("2MB page freed\n" );        
50+      
6151    printf ("========== END test_individual_alloc_rw_free ==========\n" );  
6252}  
6353
@@ -78,23 +68,15 @@ void test_batch_alloc_rw_free() {
7868    assert (ptr_2m  !=  MAP_FAILED );  
7969    printf ("Batch allocated 2MB page at %p\n" , ptr_2m );  
8070
81-     ptr_1g  =  mmap (NULL , 1024  *  1024  *  1024 , PROT_READ  | PROT_WRITE ,  
82-                   MAP_PRIVATE  | MAP_ANONYMOUS  | MAP_HUGETLB  | MAP_HUGE_1GB , -1 , 0 );  
83-     assert (ptr_1g  !=  MAP_FAILED );  
84-     printf ("Batch allocated 1GB page at %p\n" , ptr_1g );  
85-       
8671    // 统一读写测试   
8772    test_memory_rw (ptr_4k , 4096 , "4KB batch" );  
8873    test_memory_rw (ptr_2m , 2  *  1024  *  1024 , "2MB batch" );  
89-     test_memory_rw (ptr_1g , 1024  *  1024  *  1024 , "1GB batch" );  
9074
9175    // 统一释放   
9276    assert (munmap (ptr_4k , 4096 ) ==  0 );  
9377    printf ("Batch freed 4KB page\n" );  
9478    assert (munmap (ptr_2m , 2  *  1024  *  1024 ) ==  0 );  
9579    printf ("Batch freed 2MB page\n" );  
96-     assert (munmap (ptr_1g , 1024  *  1024  *  1024 ) ==  0 );  
97-     printf ("Batch freed 1GB page\n" );  
9880
9981    printf ("========== END test_batch_alloc_rw_free ==========\n" );  
10082}  
@@ -103,7 +85,7 @@ void test_batch_alloc_rw_free() {
10385void  test_interleaved_alloc_rw_free () {  
10486    printf ("========== START test_interleaved_alloc_rw_free ==========\n" );  
10587
106-     // 4KB -> 读写 -> 2MB -> 读写 -> 释放4KB  -> 1GB -> 读写 ->  释放2MB -> 释放1GB    
88+     // 4KB -> 读写 -> 2MB -> 读写 -> 释放4KB-> 释放2MB 
10789    void  * ptr_4k  =  mmap (NULL , 4096 , PROT_READ  | PROT_WRITE ,   
10890                        MAP_PRIVATE  | MAP_ANONYMOUS , -1 , 0 );  
10991    assert (ptr_4k  !=  MAP_FAILED );  
@@ -117,19 +99,10 @@ void test_interleaved_alloc_rw_free() {
11799    test_memory_rw (ptr_2m , 2  *  1024  *  1024 , "2MB interleaved" );  
118100
119101    assert (munmap (ptr_4k , 4096 ) ==  0 );  
120-     printf ("Interleaved: freed 4KB page\n" );  
121-       
122-     void  * ptr_1g  =  mmap (NULL , 1024  *  1024  *  1024 , PROT_READ  | PROT_WRITE ,  
123-                         MAP_PRIVATE  | MAP_ANONYMOUS  | MAP_HUGETLB  | MAP_HUGE_1GB , -1 , 0 );  
124-     assert (ptr_1g  !=  MAP_FAILED );  
125-     printf ("Interleaved: allocated 1GB page\n" );  
126-     test_memory_rw (ptr_1g , 1024  *  1024  *  1024 , "1GB interleaved" );  
127-       
102+     printf ("Interleaved: freed 4KB page\n" );        
103+      
128104    assert (munmap (ptr_2m , 2  *  1024  *  1024 ) ==  0 );  
129-     printf ("Interleaved: freed 2MB page\n" );  
130-       
131-     assert (munmap (ptr_1g , 1024  *  1024  *  1024 ) ==  0 );  
132-     printf ("Interleaved: freed 1GB page\n" );  
105+     printf ("Interleaved: freed 2MB page\n" );        
133106
134107    printf ("========== END test_interleaved_alloc_rw_free ==========\n" );  
135108}  
@@ -166,23 +139,8 @@ void test_eager_vs_lazy_allocation() {
166139    assert (ptr_2m_lazy  !=  MAP_FAILED );  
167140    printf ("2MB lazy allocation completed\n" );  
168141    test_memory_rw (ptr_2m_lazy , 2  *  1024  *  1024 , "2MB lazy" );  
169-     assert (munmap (ptr_2m_lazy , 2  *  1024  *  1024 ) ==  0 );  
170-       
171-     // 1GB 立即分配和懒分配   
172-     void  * ptr_1g_eager  =  mmap (NULL , 1024  *  1024  *  1024 , PROT_READ  | PROT_WRITE ,  
173-                               MAP_PRIVATE  | MAP_ANONYMOUS  | MAP_HUGETLB  | MAP_HUGE_1GB  | MAP_POPULATE , -1 , 0 );  
174-     assert (ptr_1g_eager  !=  MAP_FAILED );  
175-     printf ("1GB eager allocation completed\n" );  
176-     test_memory_rw (ptr_1g_eager , 1024  *  1024  *  1024 , "1GB eager" );  
177-     assert (munmap (ptr_1g_eager , 1024  *  1024  *  1024 ) ==  0 );  
178-       
179-     void  * ptr_1g_lazy  =  mmap (NULL , 1024  *  1024  *  1024 , PROT_READ  | PROT_WRITE ,  
180-                              MAP_PRIVATE  | MAP_ANONYMOUS  | MAP_HUGETLB  | MAP_HUGE_1GB , -1 , 0 );  
181-     assert (ptr_1g_lazy  !=  MAP_FAILED );  
182-     printf ("1GB lazy allocation completed\n" );  
183-     test_memory_rw (ptr_1g_lazy , 1024  *  1024  *  1024 , "1GB lazy" );  
184-     assert (munmap (ptr_1g_lazy , 1024  *  1024  *  1024 ) ==  0 );  
185-       
142+     assert (munmap (ptr_2m_lazy , 2  *  1024  *  1024 ) ==  0 );        
143+      
186144    printf ("All eager/lazy allocations freed\n" );        
187145    printf ("========== END test_eager_vs_lazy_allocation ==========\n" );  
188146}  
@@ -260,21 +218,7 @@ void test_linear_mapping() {
260218        printf ("2MB linear mapping freed\n" );  
261219    } else  {  
262220        printf ("2MB linear mapping failed\n" );  
263-     }  
264-       
265-     // 1GB 线性映射   
266-     void  * ptr_1g  =  mmap ((char * )base_addr  +  0x20000000 , 1024  *  1024  *  1024 ,   
267-                         PROT_READ  | PROT_WRITE ,  
268-                         MAP_PRIVATE  | MAP_ANONYMOUS  | MAP_FIXED  | MAP_HUGETLB  | MAP_HUGE_1GB ,   
269-                         -1 , 0 );  
270-     if  (ptr_1g  !=  MAP_FAILED  &&  ptr_1g  ==  (char * )base_addr  +  0x20000000 ) {  
271-         printf ("1GB linear mapping at %p\n" , ptr_1g );  
272-         test_memory_rw (ptr_1g , 1024  *  1024  *  1024 , "1GB linear" );  
273-         assert (munmap (ptr_1g , 1024  *  1024  *  1024 ) ==  0 );  
274-         printf ("1GB linear mapping freed\n" );  
275-     } else  {  
276-         printf ("1GB linear mapping failed\n" );  
277-     }  
221+     }      
278222
279223    printf ("========== END test_linear_mapping ==========\n" );  
280224}  
0 commit comments