memory/jemalloc/src/test/bitmap.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 #define JEMALLOC_MANGLE
michael@0 2 #include "jemalloc_test.h"
michael@0 3
michael@0 4 #if (LG_BITMAP_MAXBITS > 12)
michael@0 5 # define MAXBITS 4500
michael@0 6 #else
michael@0 7 # define MAXBITS (1U << LG_BITMAP_MAXBITS)
michael@0 8 #endif
michael@0 9
michael@0 10 static void
michael@0 11 test_bitmap_size(void)
michael@0 12 {
michael@0 13 size_t i, prev_size;
michael@0 14
michael@0 15 prev_size = 0;
michael@0 16 for (i = 1; i <= MAXBITS; i++) {
michael@0 17 size_t size = bitmap_size(i);
michael@0 18 assert(size >= prev_size);
michael@0 19 prev_size = size;
michael@0 20 }
michael@0 21 }
michael@0 22
michael@0 23 static void
michael@0 24 test_bitmap_init(void)
michael@0 25 {
michael@0 26 size_t i;
michael@0 27
michael@0 28 for (i = 1; i <= MAXBITS; i++) {
michael@0 29 bitmap_info_t binfo;
michael@0 30 bitmap_info_init(&binfo, i);
michael@0 31 {
michael@0 32 size_t j;
michael@0 33 bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
michael@0 34 bitmap_info_ngroups(&binfo));
michael@0 35 bitmap_init(bitmap, &binfo);
michael@0 36
michael@0 37 for (j = 0; j < i; j++)
michael@0 38 assert(bitmap_get(bitmap, &binfo, j) == false);
michael@0 39 free(bitmap);
michael@0 40
michael@0 41 }
michael@0 42 }
michael@0 43 }
michael@0 44
michael@0 45 static void
michael@0 46 test_bitmap_set(void)
michael@0 47 {
michael@0 48 size_t i;
michael@0 49
michael@0 50 for (i = 1; i <= MAXBITS; i++) {
michael@0 51 bitmap_info_t binfo;
michael@0 52 bitmap_info_init(&binfo, i);
michael@0 53 {
michael@0 54 size_t j;
michael@0 55 bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
michael@0 56 bitmap_info_ngroups(&binfo));
michael@0 57 bitmap_init(bitmap, &binfo);
michael@0 58
michael@0 59 for (j = 0; j < i; j++)
michael@0 60 bitmap_set(bitmap, &binfo, j);
michael@0 61 assert(bitmap_full(bitmap, &binfo));
michael@0 62 free(bitmap);
michael@0 63 }
michael@0 64 }
michael@0 65 }
michael@0 66
michael@0 67 static void
michael@0 68 test_bitmap_unset(void)
michael@0 69 {
michael@0 70 size_t i;
michael@0 71
michael@0 72 for (i = 1; i <= MAXBITS; i++) {
michael@0 73 bitmap_info_t binfo;
michael@0 74 bitmap_info_init(&binfo, i);
michael@0 75 {
michael@0 76 size_t j;
michael@0 77 bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
michael@0 78 bitmap_info_ngroups(&binfo));
michael@0 79 bitmap_init(bitmap, &binfo);
michael@0 80
michael@0 81 for (j = 0; j < i; j++)
michael@0 82 bitmap_set(bitmap, &binfo, j);
michael@0 83 assert(bitmap_full(bitmap, &binfo));
michael@0 84 for (j = 0; j < i; j++)
michael@0 85 bitmap_unset(bitmap, &binfo, j);
michael@0 86 for (j = 0; j < i; j++)
michael@0 87 bitmap_set(bitmap, &binfo, j);
michael@0 88 assert(bitmap_full(bitmap, &binfo));
michael@0 89 free(bitmap);
michael@0 90 }
michael@0 91 }
michael@0 92 }
michael@0 93
michael@0 94 static void
michael@0 95 test_bitmap_sfu(void)
michael@0 96 {
michael@0 97 size_t i;
michael@0 98
michael@0 99 for (i = 1; i <= MAXBITS; i++) {
michael@0 100 bitmap_info_t binfo;
michael@0 101 bitmap_info_init(&binfo, i);
michael@0 102 {
michael@0 103 ssize_t j;
michael@0 104 bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
michael@0 105 bitmap_info_ngroups(&binfo));
michael@0 106 bitmap_init(bitmap, &binfo);
michael@0 107
michael@0 108 /* Iteratively set bits starting at the beginning. */
michael@0 109 for (j = 0; j < i; j++)
michael@0 110 assert(bitmap_sfu(bitmap, &binfo) == j);
michael@0 111 assert(bitmap_full(bitmap, &binfo));
michael@0 112
michael@0 113 /*
michael@0 114 * Iteratively unset bits starting at the end, and
michael@0 115 * verify that bitmap_sfu() reaches the unset bits.
michael@0 116 */
michael@0 117 for (j = i - 1; j >= 0; j--) {
michael@0 118 bitmap_unset(bitmap, &binfo, j);
michael@0 119 assert(bitmap_sfu(bitmap, &binfo) == j);
michael@0 120 bitmap_unset(bitmap, &binfo, j);
michael@0 121 }
michael@0 122 assert(bitmap_get(bitmap, &binfo, 0) == false);
michael@0 123
michael@0 124 /*
michael@0 125 * Iteratively set bits starting at the beginning, and
michael@0 126 * verify that bitmap_sfu() looks past them.
michael@0 127 */
michael@0 128 for (j = 1; j < i; j++) {
michael@0 129 bitmap_set(bitmap, &binfo, j - 1);
michael@0 130 assert(bitmap_sfu(bitmap, &binfo) == j);
michael@0 131 bitmap_unset(bitmap, &binfo, j);
michael@0 132 }
michael@0 133 assert(bitmap_sfu(bitmap, &binfo) == i - 1);
michael@0 134 assert(bitmap_full(bitmap, &binfo));
michael@0 135 free(bitmap);
michael@0 136 }
michael@0 137 }
michael@0 138 }
michael@0 139
michael@0 140 int
michael@0 141 main(void)
michael@0 142 {
michael@0 143 malloc_printf("Test begin\n");
michael@0 144
michael@0 145 test_bitmap_size();
michael@0 146 test_bitmap_init();
michael@0 147 test_bitmap_set();
michael@0 148 test_bitmap_unset();
michael@0 149 test_bitmap_sfu();
michael@0 150
michael@0 151 malloc_printf("Test end\n");
michael@0 152 return (0);
michael@0 153 }

mercurial