Home | History | Annotate | Line # | Download | only in stress
      1 #include "test/jemalloc_test.h"
      2 #include "test/bench.h"
      3 
      4 #define SMALL_ALLOC_SIZE 128
      5 #define LARGE_ALLOC_SIZE SC_LARGE_MINCLASS
      6 #define NALLOCS 1000
      7 
      8 /*
      9  * We make this volatile so the 1-at-a-time variants can't leave the allocation
     10  * in a register, just to try to get the cache behavior closer.
     11  */
     12 void *volatile allocs[NALLOCS];
     13 
     14 static void
     15 array_alloc_dalloc_small(void) {
     16 	for (int i = 0; i < NALLOCS; i++) {
     17 		void *p = mallocx(SMALL_ALLOC_SIZE, 0);
     18 		assert_ptr_not_null(p, "mallocx shouldn't fail");
     19 		allocs[i] = p;
     20 	}
     21 	for (int i = 0; i < NALLOCS; i++) {
     22 		sdallocx(allocs[i], SMALL_ALLOC_SIZE, 0);
     23 	}
     24 }
     25 
     26 static void
     27 item_alloc_dalloc_small(void) {
     28 	for (int i = 0; i < NALLOCS; i++) {
     29 		void *p = mallocx(SMALL_ALLOC_SIZE, 0);
     30 		assert_ptr_not_null(p, "mallocx shouldn't fail");
     31 		allocs[i] = p;
     32 		sdallocx(allocs[i], SMALL_ALLOC_SIZE, 0);
     33 	}
     34 }
     35 
     36 TEST_BEGIN(test_array_vs_item_small) {
     37 	compare_funcs(1 * 1000, 10 * 1000,
     38 	    "array of small allocations", array_alloc_dalloc_small,
     39 	    "small item allocation", item_alloc_dalloc_small);
     40 }
     41 TEST_END
     42 
     43 static void
     44 array_alloc_dalloc_large(void) {
     45 	for (int i = 0; i < NALLOCS; i++) {
     46 		void *p = mallocx(LARGE_ALLOC_SIZE, 0);
     47 		assert_ptr_not_null(p, "mallocx shouldn't fail");
     48 		allocs[i] = p;
     49 	}
     50 	for (int i = 0; i < NALLOCS; i++) {
     51 		sdallocx(allocs[i], LARGE_ALLOC_SIZE, 0);
     52 	}
     53 }
     54 
     55 static void
     56 item_alloc_dalloc_large(void) {
     57 	for (int i = 0; i < NALLOCS; i++) {
     58 		void *p = mallocx(LARGE_ALLOC_SIZE, 0);
     59 		assert_ptr_not_null(p, "mallocx shouldn't fail");
     60 		allocs[i] = p;
     61 		sdallocx(allocs[i], LARGE_ALLOC_SIZE, 0);
     62 	}
     63 }
     64 
     65 TEST_BEGIN(test_array_vs_item_large) {
     66 	compare_funcs(100, 1000,
     67 	    "array of large allocations", array_alloc_dalloc_large,
     68 	    "large item allocation", item_alloc_dalloc_large);
     69 }
     70 TEST_END
     71 
     72 int main(void) {
     73 	return test_no_reentrancy(
     74 	    test_array_vs_item_small,
     75 	    test_array_vs_item_large);
     76 }
     77