1 #include "test/jemalloc_test.h" 2 3 static const bool config_stats = 4 #ifdef JEMALLOC_STATS 5 true 6 #else 7 false 8 #endif 9 ; 10 11 void * 12 thd_start(void *arg) { 13 int err; 14 void *p; 15 uint64_t a0, a1, d0, d1; 16 uint64_t *ap0, *ap1, *dp0, *dp1; 17 size_t sz, usize; 18 19 sz = sizeof(a0); 20 if ((err = mallctl("thread.allocated", (void *)&a0, &sz, NULL, 0))) { 21 if (err == ENOENT) { 22 goto label_ENOENT; 23 } 24 test_fail("%s(): Error in mallctl(): %s", __func__, 25 strerror(err)); 26 } 27 sz = sizeof(ap0); 28 if ((err = mallctl("thread.allocatedp", (void *)&ap0, &sz, NULL, 0))) { 29 if (err == ENOENT) { 30 goto label_ENOENT; 31 } 32 test_fail("%s(): Error in mallctl(): %s", __func__, 33 strerror(err)); 34 } 35 assert_u64_eq(*ap0, a0, 36 "\"thread.allocatedp\" should provide a pointer to internal " 37 "storage"); 38 39 sz = sizeof(d0); 40 if ((err = mallctl("thread.deallocated", (void *)&d0, &sz, NULL, 0))) { 41 if (err == ENOENT) { 42 goto label_ENOENT; 43 } 44 test_fail("%s(): Error in mallctl(): %s", __func__, 45 strerror(err)); 46 } 47 sz = sizeof(dp0); 48 if ((err = mallctl("thread.deallocatedp", (void *)&dp0, &sz, NULL, 49 0))) { 50 if (err == ENOENT) { 51 goto label_ENOENT; 52 } 53 test_fail("%s(): Error in mallctl(): %s", __func__, 54 strerror(err)); 55 } 56 assert_u64_eq(*dp0, d0, 57 "\"thread.deallocatedp\" should provide a pointer to internal " 58 "storage"); 59 60 p = malloc(1); 61 assert_ptr_not_null(p, "Unexpected malloc() error"); 62 63 sz = sizeof(a1); 64 mallctl("thread.allocated", (void *)&a1, &sz, NULL, 0); 65 sz = sizeof(ap1); 66 mallctl("thread.allocatedp", (void *)&ap1, &sz, NULL, 0); 67 assert_u64_eq(*ap1, a1, 68 "Dereferenced \"thread.allocatedp\" value should equal " 69 "\"thread.allocated\" value"); 70 assert_ptr_eq(ap0, ap1, 71 "Pointer returned by \"thread.allocatedp\" should not change"); 72 73 usize = malloc_usable_size(p); 74 assert_u64_le(a0 + usize, a1, 75 "Allocated memory counter should increase by at least the amount " 76 "explicitly allocated"); 77 78 free(p); 79 80 sz = sizeof(d1); 81 mallctl("thread.deallocated", (void *)&d1, &sz, NULL, 0); 82 sz = sizeof(dp1); 83 mallctl("thread.deallocatedp", (void *)&dp1, &sz, NULL, 0); 84 assert_u64_eq(*dp1, d1, 85 "Dereferenced \"thread.deallocatedp\" value should equal " 86 "\"thread.deallocated\" value"); 87 assert_ptr_eq(dp0, dp1, 88 "Pointer returned by \"thread.deallocatedp\" should not change"); 89 90 assert_u64_le(d0 + usize, d1, 91 "Deallocated memory counter should increase by at least the amount " 92 "explicitly deallocated"); 93 94 return NULL; 95 label_ENOENT: 96 assert_false(config_stats, 97 "ENOENT should only be returned if stats are disabled"); 98 test_skip("\"thread.allocated\" mallctl not available"); 99 return NULL; 100 } 101 102 TEST_BEGIN(test_main_thread) { 103 thd_start(NULL); 104 } 105 TEST_END 106 107 TEST_BEGIN(test_subthread) { 108 thd_t thd; 109 110 thd_create(&thd, thd_start, NULL); 111 thd_join(thd, NULL); 112 } 113 TEST_END 114 115 int 116 main(void) { 117 /* Run tests multiple times to check for bad interactions. */ 118 return test( 119 test_main_thread, 120 test_subthread, 121 test_main_thread, 122 test_subthread, 123 test_main_thread); 124 } 125