Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1 | #define JEMALLOC_EXTENT_C_ |
Jason Evans | 376b152 | 2010-02-11 14:45:59 -0800 | [diff] [blame] | 2 | #include "jemalloc/internal/jemalloc_internal.h" |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3 | |
| 4 | /******************************************************************************/ |
| 5 | |
Jason Evans | 2cdf07a | 2016-11-11 21:14:29 -0800 | [diff] [blame] | 6 | /* |
| 7 | * Round down to the nearest chunk size that can actually be requested during |
| 8 | * normal huge allocation. |
| 9 | */ |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 10 | JEMALLOC_INLINE_C size_t |
| 11 | extent_quantize(size_t size) |
| 12 | { |
Jason Evans | 2cdf07a | 2016-11-11 21:14:29 -0800 | [diff] [blame] | 13 | size_t ret; |
| 14 | szind_t ind; |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 15 | |
Jason Evans | 2cdf07a | 2016-11-11 21:14:29 -0800 | [diff] [blame] | 16 | assert(size > 0); |
| 17 | |
| 18 | ind = size2index(size + 1); |
Jason Evans | 1aeea0f | 2016-11-11 22:46:55 -0800 | [diff] [blame] | 19 | if (ind == 0) { |
| 20 | /* Avoid underflow. */ |
| 21 | return (index2size(0)); |
Jason Evans | 2cdf07a | 2016-11-11 21:14:29 -0800 | [diff] [blame] | 22 | } |
| 23 | ret = index2size(ind - 1); |
| 24 | assert(ret <= size); |
| 25 | return (ret); |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Jason Evans | af1f592 | 2014-10-30 16:38:08 -0700 | [diff] [blame] | 28 | JEMALLOC_INLINE_C int |
Jason Evans | 5c77af9 | 2016-11-14 18:27:23 -0800 | [diff] [blame] | 29 | extent_sz_comp(const extent_node_t *a, const extent_node_t *b) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 30 | { |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 31 | size_t a_qsize = extent_quantize(extent_node_size_get(a)); |
| 32 | size_t b_qsize = extent_quantize(extent_node_size_get(b)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 33 | |
Jason Evans | 5c77af9 | 2016-11-14 18:27:23 -0800 | [diff] [blame] | 34 | return ((a_qsize > b_qsize) - (a_qsize < b_qsize)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Jason Evans | 5c77af9 | 2016-11-14 18:27:23 -0800 | [diff] [blame] | 37 | JEMALLOC_INLINE_C int |
| 38 | extent_sn_comp(const extent_node_t *a, const extent_node_t *b) |
| 39 | { |
| 40 | uint64_t a_sn = (uintptr_t)extent_node_sn_get(a); |
| 41 | uint64_t b_sn = (uintptr_t)extent_node_sn_get(b); |
| 42 | |
| 43 | return ((a_sn > b_sn) - (a_sn < b_sn)); |
| 44 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 45 | |
Jason Evans | af1f592 | 2014-10-30 16:38:08 -0700 | [diff] [blame] | 46 | JEMALLOC_INLINE_C int |
Joshua Kahn | 13b4015 | 2015-09-18 16:58:17 -0400 | [diff] [blame] | 47 | extent_ad_comp(const extent_node_t *a, const extent_node_t *b) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 48 | { |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 49 | uintptr_t a_addr = (uintptr_t)extent_node_addr_get(a); |
| 50 | uintptr_t b_addr = (uintptr_t)extent_node_addr_get(b); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 51 | |
| 52 | return ((a_addr > b_addr) - (a_addr < b_addr)); |
| 53 | } |
| 54 | |
Jason Evans | 5c77af9 | 2016-11-14 18:27:23 -0800 | [diff] [blame] | 55 | JEMALLOC_INLINE_C int |
| 56 | extent_szsnad_comp(const extent_node_t *a, const extent_node_t *b) |
| 57 | { |
| 58 | int ret; |
| 59 | |
| 60 | ret = extent_sz_comp(a, b); |
| 61 | if (ret != 0) |
| 62 | return (ret); |
| 63 | |
| 64 | ret = extent_sn_comp(a, b); |
| 65 | if (ret != 0) |
| 66 | return (ret); |
| 67 | |
| 68 | ret = extent_ad_comp(a, b); |
| 69 | return (ret); |
| 70 | } |
| 71 | |
| 72 | /* Generate red-black tree functions. */ |
| 73 | rb_gen(, extent_tree_szsnad_, extent_tree_t, extent_node_t, szsnad_link, |
| 74 | extent_szsnad_comp) |
| 75 | |
Jason Evans | f3ff752 | 2010-02-28 15:00:18 -0800 | [diff] [blame] | 76 | /* Generate red-black tree functions. */ |
Jason Evans | 2195ba4 | 2015-02-15 16:43:52 -0800 | [diff] [blame] | 77 | rb_gen(, extent_tree_ad_, extent_tree_t, extent_node_t, ad_link, extent_ad_comp) |