blob: 828f627c8dbe69f9a9f3bbbc57c2278869ff9568 [file] [log] [blame]
Jason Evanse476f8a2010-01-16 09:53:50 -08001#define JEMALLOC_EXTENT_C_
Jason Evans376b1522010-02-11 14:45:59 -08002#include "jemalloc/internal/jemalloc_internal.h"
Jason Evanse476f8a2010-01-16 09:53:50 -08003
4/******************************************************************************/
5
Jason Evans2cdf07a2016-11-11 21:14:29 -08006/*
7 * Round down to the nearest chunk size that can actually be requested during
8 * normal huge allocation.
9 */
Jason Evans8a03cf02015-05-04 09:58:36 -070010JEMALLOC_INLINE_C size_t
11extent_quantize(size_t size)
12{
Jason Evans2cdf07a2016-11-11 21:14:29 -080013 size_t ret;
14 szind_t ind;
Jason Evans8a03cf02015-05-04 09:58:36 -070015
Jason Evans2cdf07a2016-11-11 21:14:29 -080016 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 Evans8a03cf02015-05-04 09:58:36 -070029}
30
Jason Evansaf1f5922014-10-30 16:38:08 -070031JEMALLOC_INLINE_C int
Joshua Kahn13b40152015-09-18 16:58:17 -040032extent_szad_comp(const extent_node_t *a, const extent_node_t *b)
Jason Evanse476f8a2010-01-16 09:53:50 -080033{
34 int ret;
Jason Evans8a03cf02015-05-04 09:58:36 -070035 size_t a_qsize = extent_quantize(extent_node_size_get(a));
36 size_t b_qsize = extent_quantize(extent_node_size_get(b));
Jason Evanse476f8a2010-01-16 09:53:50 -080037
Jason Evans8a03cf02015-05-04 09:58:36 -070038 /*
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 Evanse476f8a2010-01-16 09:53:50 -080043 if (ret == 0) {
Jason Evansee41ad42015-02-15 18:04:46 -080044 uintptr_t a_addr = (uintptr_t)extent_node_addr_get(a);
45 uintptr_t b_addr = (uintptr_t)extent_node_addr_get(b);
Jason Evanse476f8a2010-01-16 09:53:50 -080046
47 ret = (a_addr > b_addr) - (a_addr < b_addr);
48 }
49
50 return (ret);
51}
52
Jason Evansf3ff7522010-02-28 15:00:18 -080053/* Generate red-black tree functions. */
Jason Evans2195ba42015-02-15 16:43:52 -080054rb_gen(, extent_tree_szad_, extent_tree_t, extent_node_t, szad_link,
Jason Evanse476f8a2010-01-16 09:53:50 -080055 extent_szad_comp)
Jason Evanse476f8a2010-01-16 09:53:50 -080056
Jason Evansaf1f5922014-10-30 16:38:08 -070057JEMALLOC_INLINE_C int
Joshua Kahn13b40152015-09-18 16:58:17 -040058extent_ad_comp(const extent_node_t *a, const extent_node_t *b)
Jason Evanse476f8a2010-01-16 09:53:50 -080059{
Jason Evansee41ad42015-02-15 18:04:46 -080060 uintptr_t a_addr = (uintptr_t)extent_node_addr_get(a);
61 uintptr_t b_addr = (uintptr_t)extent_node_addr_get(b);
Jason Evanse476f8a2010-01-16 09:53:50 -080062
63 return ((a_addr > b_addr) - (a_addr < b_addr));
64}
65
Jason Evansf3ff7522010-02-28 15:00:18 -080066/* Generate red-black tree functions. */
Jason Evans2195ba42015-02-15 16:43:52 -080067rb_gen(, extent_tree_ad_, extent_tree_t, extent_node_t, ad_link, extent_ad_comp)