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); |
| 19 | if (ind == NSIZES) { |
| 20 | /* |
| 21 | * Allocation requests can't directly create extents that exceed |
| 22 | * HUGE_MAXCLASS, but extent merging can create them. |
| 23 | */ |
| 24 | return (HUGE_MAXCLASS); |
| 25 | } |
| 26 | ret = index2size(ind - 1); |
| 27 | assert(ret <= size); |
| 28 | return (ret); |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Jason Evans | af1f592 | 2014-10-30 16:38:08 -0700 | [diff] [blame] | 31 | JEMALLOC_INLINE_C int |
Joshua Kahn | 13b4015 | 2015-09-18 16:58:17 -0400 | [diff] [blame] | 32 | extent_szad_comp(const extent_node_t *a, const extent_node_t *b) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 33 | { |
| 34 | int ret; |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 35 | size_t a_qsize = extent_quantize(extent_node_size_get(a)); |
| 36 | size_t b_qsize = extent_quantize(extent_node_size_get(b)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 37 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 38 | /* |
| 39 | * Compare based on quantized size rather than size, in order to sort |
| 40 | * equally useful extents only by address. |
| 41 | */ |
| 42 | ret = (a_qsize > b_qsize) - (a_qsize < b_qsize); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 43 | if (ret == 0) { |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 44 | uintptr_t a_addr = (uintptr_t)extent_node_addr_get(a); |
| 45 | uintptr_t b_addr = (uintptr_t)extent_node_addr_get(b); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 46 | |
| 47 | ret = (a_addr > b_addr) - (a_addr < b_addr); |
| 48 | } |
| 49 | |
| 50 | return (ret); |
| 51 | } |
| 52 | |
Jason Evans | f3ff752 | 2010-02-28 15:00:18 -0800 | [diff] [blame] | 53 | /* Generate red-black tree functions. */ |
Jason Evans | 2195ba4 | 2015-02-15 16:43:52 -0800 | [diff] [blame] | 54 | rb_gen(, extent_tree_szad_, extent_tree_t, extent_node_t, szad_link, |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 55 | extent_szad_comp) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 56 | |
Jason Evans | af1f592 | 2014-10-30 16:38:08 -0700 | [diff] [blame] | 57 | JEMALLOC_INLINE_C int |
Joshua Kahn | 13b4015 | 2015-09-18 16:58:17 -0400 | [diff] [blame] | 58 | 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] | 59 | { |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 60 | uintptr_t a_addr = (uintptr_t)extent_node_addr_get(a); |
| 61 | uintptr_t b_addr = (uintptr_t)extent_node_addr_get(b); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 62 | |
| 63 | return ((a_addr > b_addr) - (a_addr < b_addr)); |
| 64 | } |
| 65 | |
Jason Evans | f3ff752 | 2010-02-28 15:00:18 -0800 | [diff] [blame] | 66 | /* Generate red-black tree functions. */ |
Jason Evans | 2195ba4 | 2015-02-15 16:43:52 -0800 | [diff] [blame] | 67 | rb_gen(, extent_tree_ad_, extent_tree_t, extent_node_t, ad_link, extent_ad_comp) |