Commit Graph
987 Commits
Author SHA1 Message Date
Mike Hommey e73bdbf1a7 Bug 1409294 - Make arena_t::Init return whether initialization succeeded. r=njn 2017-10-17 17:37:50 +09:00
Mike Hommey 7717c3e01c Bug 1409273 - Replace malloc_mutex_* with class methods. r=njn
Ideally, we'd be reusing some Mutex class we have in Gecko, the base one
in mozglue/misc being the best candidate. However, the contraints in
mozjemalloc make that unconvenient:
- Can't have a constructor because malloc_init() would likely run before
  it, and that would mean the mutexes would be re-initialized.
- Can't have a destructor because code will run after static
  destructors, and some of that code likely will invoke the allocator,
  and we can't have destructed mutexes by then.
- Can't use pthread_mutex on OSX because that loops back into the
  allocator.

Accomodating the use of Gecko mutexes around those constraints would
mean much more code than just implementing a new mutex class, so the
latter is preferred.
2017-10-06 17:20:04 +09:00
Mike Hommey c25e74979d Bug 1407468 - Replace multiple !JS_STANDALONE with MOZ_WIDGET_TOOLKIT. r=mshal 2017-10-05 14:50:01 +09:00
Tom Ritter 748f666851 Bug 1406197 Declare sized deallocators (that ignore the size) to correct a MinGW warning r=glandium
MozReview-Commit-ID: Dl4uu6evlbs
2017-10-10 13:16:10 -05:00
Mike Hommey 2fc89c8b16 Bug 1407469 - Remove malloc_spin_*. r=njn
The malloc_spin_* functions have ended up being strictly identical to
the malloc_mutex_* functions, so use the latter instead of the former.
2017-10-06 16:50:17 +09:00
Mike Hommey d94b3ac6a5 Bug 1406303 - Don't heap-allocate the global chunk radix tree. r=njn
Now that the radix tree structure has a fixed size, we can just allocate
the chunk radix tree object statically.
2017-10-06 16:18:01 +09:00
Mike Hommey ec54fa6c80 Bug 1406303 - Make the number of significant bits used by the radix tree a template parameter. r=njn
All the parameters of the radix tree (bits per level, height) are
derived from the aBits argument to ::Create in a straightforward way.
aBits itself is a constant at the call point, making them all constants,
so we can turn all of them as constants at compile time instead of
storing as data.
2017-10-06 15:50:00 +09:00
Mike Hommey 8ebeff0a0a Bug 1406303 - Only store 2 levels of bit sizes for the radix tree. r=njn
All levels except the first are using the same size, and in some cases,
even the first uses the same size. Only storing those two different
sizes allows to fix the class size, while not making the code
significantly more complex.
2017-10-06 15:24:07 +09:00
Mike Hommey e0f80419bf Bug 1406303 - Simplify the calculation of AddressRadixTree's height. r=njn
The tree height was defined as:
  height = aBits / bits_per_level;
  if (height * bits_per_level != aBits) {
    height++;
  }

What's wanted here is a height that covers all the bits, where the first
level might cover less than bits_per_level.

So aBits / bits_per_level gets us the height covered by levels with
exactly bits_per_level bits. The tree height is one more when there
are remaining bits.

Put differently, we can write aBits as:
  aBits = bits_per_level * x + y

with y < bits_per_level.

We have:
  aBits / bits_per_level = x.
  height = x when y = 0, and x + 1 when y > 0.

We're looking for a number z such that
  height = (aBits + z) / bits_per_level.

Or:
  height = (bits_per_level * x + y + z) / bits_per_level.
         = x + (y + z) / bits_per_level.

So we're looking for a z such that
  (y + z) / bits_per_level = 0 when y = 0
                           = 1 when y > 0

The properties of the integer division are such that the above means:
  0 <= y + z < bits_per_level when y = 0
  bits_per_level <= y + z < 2 * bits_per_level when y > 0

Which gives us:
  0 <= z < bits_per_level
  bits_per_level - y <= z < 2 * bits_per_level - y when y > 0

y being < bit_per_level per the constraint further above,
  2 * bits_per_level - y > bits_per_level.

So all in all, we want a z such that
  bits_per_level - y <= z < bits_per_level with 0 < y < bits_per_level

The largest value where this is true is z = bits_per_level - 1.

In summary,
  height = (aBits + bits_per_level - 1) / bits_per_level

is the same as the height as originally defined.

With that formula, it's self evident that height * bits_per_level is
always >= aBits, so we remove the assertion.
2017-10-06 11:32:27 +09:00
Mike Hommey a73d563c9a Bug 1406303 - Simplify the calculation of AddressRadixTree's bits_per_level. r=njn
bits_per_level was defined as:
  ffs(pow2_ceil((kNodeSize / sizeof(void*)))) - 1

kNodeSize is (1U << 14) when SIZEOF_PTR is 4 (sizeof(void*) being the
same). Otherwise, it's CACHELINE, which is (1U << 6).
The most important part, though, is that it's always a power of 2.
And it's divided by sizeof(void*) which is always a power or 2.
The result of that division is thus always a power of 2, as long as
kNodeSize is larger than the size of a pointer, which it is.

The argument to pow2_ceil being a power of 2, pow2_ceil is a noop,
so it can go away. And the argument to ffs being a power of 2, it
returns one more than n that matches 1 << n == value. So overall
the expression returns the number of shifts for
kNodeSize / SIZEOF_PTR.

Transforming kNodeSize to a number of shifts/power of 2, the expression
can then be simplified as kNodeSize2Pow - SIZEOF_PTR_2POW.
2017-09-28 15:35:21 +09:00
Mike Hommey d90ae1f7a7 Bug 1406303 - Turn malloc_rtree_t into a C++ class. r=njn
The only semantic change is in the value returned by Set, which now
returns whether the value could be set or not.
2017-10-06 10:49:24 +09:00
Mike Hommey de13edd3bd Bug 1406303 - Refactor malloc_rtree_get/set. r=njn
There is a lot of redundancy between malloc_rtree_get and
malloc_rtree_set. Essentially, they both look up a slot, and either get
a value or set a value in that slot. malloc_rtree_get doesn't create a
tree path for the slot when it doesn't exist. And the
MALLOC_RTREE_GET_GENERATE macro machinery makes malloc_rtree_get retry
with a lock and validate both results agree in debug builds.

By introducing a malloc_rtree_get_slot function that returns a slot,
optionally creating a tree path to it, we remove the redundancy between
_get and _set, and we can avoid the macro machinery as well.
2017-09-28 12:18:14 +09:00
Emanuel Hoogeveen a8d4a08a29 Bug 1405159 - Assert against freeing incorrectly offset pointers. r=glandium 2017-10-02 15:39:00 -04:00
Nicholas Nethercote 3e4e536306 Bug 1384814 - Remove critical address machinery from Mac implementation of MozStackWalk(). r=glandium.
It seemingly hasn't been needed since Mac OS 10.7. A diagnostic assertion that
has been in place for a while hasn't caught any uses of it.
2017-10-03 13:53:14 +11:00
Jan Keromnes bb8d8c9779 Bug 1403660 - Do not use 'else' after 'break' in /memory/build/rb.h. r=njn 2017-09-28 08:01:00 -04:00
Mike Hommey 5182c9ae3d Bug 1403843 - Disable static sizes on debug builds. r=njn
Because why not, especially now that this only concerns the page size.
2017-09-28 15:15:23 +09:00
Mike Hommey 176c546eb6 Bug 1403843 - Make quantum, small and chunk sizes always static. r=njn
Bug 571209 made many different kinds of sizes static at build time, as
opposed to configurable at run-time. While the dynamic sizes can be
useful to quickly test tweaks to e.g. quantum sizes, a
replace-malloc-built allocator could just as well do the same. This
need, however, is very rare, and doesn't justify keeping the sizes
dynamic on platforms where static sizes can't be used for the page size
because page size may vary depending on kernel options.

So we make every size that doesn't depend on the page size static,
whether MALLOC_STATIC_SIZES is enabled or not.

This makes no practical difference on tier-1 platforms, except Android
aarch64, which will benefit from more static sizes.
2017-09-28 15:27:59 +09:00
Mike Hommey 692ab6f0a3 Bug 1403821 - Don't force the allocator to be always compiled with optimizations. r=froydnj
This was done in bug 1104634 because back then the Android NDK had a
broken combination of compiler and libc, where the compiler would emit
calls to the ffs function, but the libc wouldn't contain them, but only
when building without optimization.

Things have changed in the meanwhile, and recent NDK doesn't have this
problem. So we can remove the hack.
2017-09-28 11:27:43 +09:00
Mike Hommey b5cfaca116 Bug 1403824 - Keep track of arenas in the arena tree. r=njn
Bug 1402174 made all arenas registered in a Red-Black tree. Which means
they are iterable through that tree, making the arenas list now redundant.
The list is also inconvenient, since it needs to be constantly
reallocated, and the allocator in charge of the list doesn't know how to
free things.

Iteration of arenas is not on any hot path anyways, so even though
iterating the RB tree is slower, it doesn't matter.

So we remove the arenas list, and keep a direct pointer to the main
arena for convenience (instead of calling First() on the RB tree every
time)
2017-09-28 08:06:23 +09:00
Mike Hommey 7e2a2bca6e Bug 1403766 - Always use some sort of thread local storage in the allocator. r=njn
As we're going to enable stylo on Android at some point, we'll have to
have thread local arenas there, which means Android needs to be using
thread local storage. Since Android is the last use of NO_TLS in the
allocator code base, remove it.
2017-09-28 08:28:52 +09:00
Mike Hommey e23eb61053 Bug 1403765 - Use native TLS in the allocator on Windows. r=njn 2017-09-25 06:47:44 +09:00
Mike Hommey 53757597bf Bug 1403444 - Remove typedefs for RedBlackTrees. r=njn 2017-09-28 06:58:37 +09:00
Mike Hommey 4a90d13141 Bug 1403444 - Rename the RedBlackTree member variables. r=njn 2017-09-27 16:11:11 +09:00
Mike Hommey 98b4823fe6 Bug 1403444 - Add a helper class to avoid the tree traversal code having to use Trait::GetTreeNode noisily. r=njn 2017-09-27 16:07:22 +09:00
Mike Hommey 86dca6ded3 Bug 1403444 - Replace the rb_foreach_* macros with a range iterator. r=njn 2017-09-27 15:25:19 +09:00
Mike Hommey e1a50a9503 Bug 1403444 - Remove rbp_f_synced. r=njn
Bug 1365460 removed the macros that used it.
2017-09-27 13:50:50 +09:00
Mike Hommey 59fa5865a1 Bug 1403444 - Replace rbp_move_red_left and rbp_move_red_right with private methods. r=njn 2017-09-26 21:21:13 +09:00
Mike Hommey 39aa5dbdaf Bug 1403444 - Replace rbp_lean_left and rbp_lean_right with private methods. r=njn 2017-09-26 21:14:12 +09:00
Mike Hommey 21be374bf1 Bug 1403444 - Replace rbp_rotate_left and rbp_rotate_right with private methods. r=njn 2017-09-26 21:08:29 +09:00
Mike Hommey 04a93a4b7f Bug 1403444 - Expand rb_remove. r=njn
At the same time, simplify the expanded code to better fit in the
template.
2017-09-26 17:13:53 +09:00
Mike Hommey 6e8bfeb217 Bug 1403444 - Expand rb_insert. r=njn
At the same time, simplify the expanded code to better fit in the
template.
2017-09-26 17:07:35 +09:00
Mike Hommey 9ec3f2abf5 Bug 1403444 - Expand rbp_first and rbp_last. r=njn
At the same time, simplify the expanded code to better fit in the
template.
2017-09-26 16:59:02 +09:00
Mike Hommey 98e3089b7f Bug 1403444 - Allow First and Last to take a start node, and use that in Next and Prev. r=njn 2017-09-26 16:55:31 +09:00
Mike Hommey 5100c87e31 Bug 1403444 - Expand rb_search and rb_nsearch. r=njn
At the same time, simplify the expanded code to better fit in the
template.
2017-09-26 16:46:43 +09:00
Mike Hommey 0f3cde0d7d Bug 1403444 - Expand rbp_next and rbp_prev. r=njn
At the same time, simplify the expanded code to better fit in the
template.
2017-09-26 16:39:03 +09:00
Mike Hommey 2d5b527d24 Bug 1403444 - Trivially expand rb_node_field. r=njn
Also replace `a_field((foo))` with `a_field(foo)` in the resulting code.
2017-09-26 16:36:24 +09:00
Mike Hommey 0f7175a868 Bug 1403444 - Expand rbp_node_new. r=njn
At the same time, simplify the expanded code to better fit in the
template.
2017-09-26 16:00:27 +09:00
Mike Hommey 13721ae05c Bug 1403444 - Expand rb_new, rb_first, rb_last, rb_next and rb_prev. r=njn
At the same time, simplify the expanded code to better fit in the
template.
2017-09-26 15:47:50 +09:00
Mike Hommey 4a9948582c Bug 1403444 - Add methods to the RedBlackTree template class, replacing rb_wrap. r=njn 2017-09-26 15:06:00 +09:00
Mike Hommey 7d59774209 Bug 1403444 - Move miscellaneous size related constants and macros earlier in mozjemalloc.cpp. r=njn 2017-09-26 09:08:00 +09:00
Mike Hommey f1b5aca306 Bug 1403444 - Replace some uses of IsRed() with Color() or IsBlack(). r=njn
The trivial expansion of macros ended up creating cases like
  expr.IsRed() ? NodeColor::Red : NodeColor::Black
which practically speaking, is the same as
  expr.Color()
so we replace those.

There are also a bunch of expr.IsRed() == false, which are replaced with
expr.IsBlack() (adding that method at the same time)
2017-09-27 08:54:49 +09:00
Mike Hommey 46184b79d0 Bug 1403444 - Trivially expand rbp_{color,red,black}_set. r=njn 2017-09-25 10:03:56 +09:00
Mike Hommey 3dac762d6a Bug 1403444 - Trivially expand rbp_right_set. r=njn 2017-09-25 10:05:07 +09:00
Mike Hommey 53403d3f96 Bug 1403444 - Trivially expand rbp_left_set. r=njn 2017-09-25 10:04:32 +09:00
Mike Hommey 6990d4f748 Bug 1403444 - Trivially expand rbp_red_get. r=njn 2017-09-25 10:02:48 +09:00
Mike Hommey 49c8dcb1fd Bug 1403444 - Trivially expand rbp_right_get. r=njn 2017-09-25 10:02:22 +09:00
Mike Hommey 541bc1f2c0 Bug 1403444 - Trivially expand rbp_left_get. r=njn 2017-09-25 10:02:06 +09:00
Mike Hommey f5b13c424d Bug 1403444 - Abstract red-black-tree link field reference with a new macro. r=njn
While we're going in the opposite direction, moving away from macros,
upcoming intermediate steps are going to "manually" expand macros, but
later steps will require changing how the link field reference is done,
and having it in a single location then will be more convenient.
2017-09-25 07:03:37 +09:00
Mike Hommey ae052f0676 Bug 1403444 - Apply clang-format to the rb.h macros. r=njn 2017-09-25 06:59:30 +09:00
Mike Hommey 5205e2f2e7 Bug 1403444 - Use a fixed size for the stack space used during rb_foreach. r=njn
That stack space would matter if recursion was involved, but there
isn't any, and a max of 1440 bytes temporarily allocated on the stack
is not really a problem.
2017-09-25 06:57:09 +09:00