paul@0 | 1 | #include "memory_utils.h" |
paul@0 | 2 | |
paul@0 | 3 | |
paul@0 | 4 | |
paul@0 | 5 | /* Return page 'n' for the configured page size. */ |
paul@0 | 6 | |
paul@0 | 7 | offset_t page(unsigned int n) |
paul@0 | 8 | { |
paul@0 | 9 | return PAGE_SIZE * n; |
paul@0 | 10 | } |
paul@0 | 11 | |
paul@0 | 12 | /* Return the order of 'size', where 2 ** order yields the size. */ |
paul@0 | 13 | |
paul@0 | 14 | unsigned int page_order(offset_t size) |
paul@0 | 15 | { |
paul@0 | 16 | /* Count zeros from the left, stopping at the first set bit, using the width |
paul@0 | 17 | of the size value (in bits, starting with the width in bytes) to |
paul@0 | 18 | calculate the position of this bit and thus the order of the value. */ |
paul@0 | 19 | |
paul@0 | 20 | return sizeof(unsigned long) * 8 - 1 - __builtin_clzl(size); |
paul@0 | 21 | } |
paul@0 | 22 | |
paul@0 | 23 | /* Return 'value' rounded up to the nearest 'increment'. */ |
paul@0 | 24 | |
paul@0 | 25 | offset_t round(offset_t value, offset_t increment) |
paul@0 | 26 | { |
paul@0 | 27 | return trunc(value + increment - 1, increment); |
paul@0 | 28 | } |
paul@0 | 29 | |
paul@0 | 30 | /* Return 'value' rounded up to the nearest multiple of 'increment'. */ |
paul@0 | 31 | |
paul@0 | 32 | offset_t round_multiple(offset_t value, offset_t increment) |
paul@0 | 33 | { |
paul@0 | 34 | offset_t last = increment; |
paul@0 | 35 | |
paul@0 | 36 | while (1) |
paul@0 | 37 | { |
paul@0 | 38 | if (value < increment) |
paul@0 | 39 | return round(value, last); |
paul@0 | 40 | |
paul@0 | 41 | last = increment; |
paul@0 | 42 | increment *= 2; |
paul@0 | 43 | } |
paul@0 | 44 | } |
paul@0 | 45 | |
paul@0 | 46 | /* Return 'value' rounded down (or truncated) to the nearest 'increment'. */ |
paul@0 | 47 | |
paul@0 | 48 | offset_t trunc(offset_t value, offset_t increment) |
paul@0 | 49 | { |
paul@0 | 50 | return (value / increment) * increment; |
paul@0 | 51 | } |
paul@0 | 52 | |
paul@0 | 53 | /* Return 'value' rounded down (or truncated) to the nearest multiple of |
paul@0 | 54 | 'increment'. */ |
paul@0 | 55 | |
paul@0 | 56 | offset_t trunc_multiple(offset_t value, offset_t increment) |
paul@0 | 57 | { |
paul@0 | 58 | offset_t last = increment; |
paul@0 | 59 | |
paul@0 | 60 | while (1) |
paul@0 | 61 | { |
paul@0 | 62 | if (value < increment) |
paul@0 | 63 | return trunc(value, last); |
paul@0 | 64 | |
paul@0 | 65 | last = increment; |
paul@0 | 66 | increment *= 2; |
paul@0 | 67 | } |
paul@0 | 68 | } |
paul@0 | 69 | |
paul@0 | 70 | /* Find the maximum size aligned region within the region from 'start' to (but |
paul@0 | 71 | not including) 'end', with the given initial 'increment'. */ |
paul@0 | 72 | |
paul@0 | 73 | offset_t max_multiple(offset_t start, offset_t end, offset_t increment) |
paul@0 | 74 | { |
paul@0 | 75 | /* The largest possible aligned region is derived from the region size. */ |
paul@0 | 76 | |
paul@0 | 77 | offset_t size = trunc_multiple(end - start, increment); |
paul@0 | 78 | |
paul@0 | 79 | /* Apply the alignment to the start. */ |
paul@0 | 80 | |
paul@0 | 81 | offset_t aligned = round(start, size); |
paul@0 | 82 | |
paul@0 | 83 | /* If the region is aligned, return the size. */ |
paul@0 | 84 | |
paul@0 | 85 | if (aligned == start) |
paul@0 | 86 | return size; |
paul@0 | 87 | |
paul@0 | 88 | /* If the region is not aligned to the current size, recalculate the aligned |
paul@0 | 89 | size. */ |
paul@0 | 90 | |
paul@0 | 91 | offset_t aligned_size; |
paul@0 | 92 | |
paul@0 | 93 | do |
paul@0 | 94 | { |
paul@0 | 95 | aligned_size = trunc_multiple(end - aligned, increment); |
paul@0 | 96 | size /= 2; |
paul@0 | 97 | |
paul@0 | 98 | /* Determine whether a smaller alignment could yield a larger aligned |
paul@0 | 99 | size. */ |
paul@0 | 100 | |
paul@0 | 101 | if (aligned_size >= size) |
paul@0 | 102 | return aligned_size; |
paul@0 | 103 | |
paul@0 | 104 | aligned = round(start, size); |
paul@0 | 105 | } |
paul@0 | 106 | while (aligned > start); |
paul@0 | 107 | |
paul@0 | 108 | return aligned_size; |
paul@0 | 109 | } |
paul@0 | 110 | |
paul@0 | 111 | // vim: tabstop=4 expandtab shiftwidth=4 |