| 29 | const bool USE_SHIFTS = false; |
| 30 | |
| 31 | void randoms() { |
| 32 | size_t before = (size_t)sbrk(0); |
| 33 | double sum_sbrk = 0; |
| 34 | size_t max_sbrk = before; |
| 35 | void* bins[BINS]; |
| 36 | size_t allocated[BINS]; |
| 37 | size_t total_allocated = 0; |
| 38 | size_t max_allocated = 0; |
| 39 | size_t checksum = 0; |
| 40 | size_t sizes = 0; |
| 41 | size_t allocations = 0; |
| 42 | for (int i = 0; i < BINS; i++) { |
| 43 | bins[i] = NULL; |
| 44 | } |
| 45 | for (int i = 0; i < ITERS; i++) { |
| 46 | int bin = random() & BIN_MASK; |
| 47 | unsigned int r = random(); |
| 48 | int alloc = r & 1; |
| 49 | r >>= 1; |
| 50 | int calloc_ = r & 1; |
| 51 | r >>= 1; |
| 52 | unsigned int size = r & 65535; |
| 53 | r >>= 16; |
| 54 | int useShifts = r & 1; |
| 55 | r >>= 1; |
| 56 | unsigned int shifts = r & 15; |
| 57 | r >>= 4; |
| 58 | if (MAX_SIZE) { |
| 59 | size = size % (MAX_SIZE + 1); |
| 60 | } |
| 61 | if (USE_SHIFTS && useShifts) { |
| 62 | size >>= shifts; // spread out values logarithmically |
| 63 | } |
| 64 | if (SIZE_MASK) size = size & ~SIZE_MASK; |
| 65 | if (MIN_SIZE && size < MIN_SIZE) size = MIN_SIZE; |
| 66 | if (MAX_SIZE && size > MAX_SIZE) size = MAX_SIZE; |
| 67 | //printf("%d\n", size); |
| 68 | if (alloc || !bins[bin]) { |
| 69 | if (bins[bin]) { |
| 70 | bool up = size >= allocated[bin]; |
| 71 | if ((up && USE_REALLOC_UP) || (!up && USE_REALLOC_DOWN)) { |
| 72 | total_allocated -= allocated[bin]; |
| 73 | bins[bin] = realloc(bins[bin], size); |
| 74 | allocated[bin] = size; |
| 75 | total_allocated += size; |
| 76 | } else { |
| 77 | // malloc and free manually |
| 78 | free(bins[bin]); |
| 79 | bins[bin] = NULL; |
| 80 | total_allocated -= allocated[bin]; |
| 81 | allocated[bin] = 0; |
| 82 | bins[bin] = malloc(size); |
| 83 | allocated[bin] = size; |
| 84 | total_allocated += size; |
| 85 | } |
| 86 | } else { |
| 87 | if (calloc_ && USE_CALLOC) { |
| 88 | bins[bin] = calloc(size, 1); |