History log of /haiku/src/system/kernel/heap.cpp
Revision Date Author Comments
# 70e8eacb 19-Jun-2023 Augustin Cavalier <waddlesplash@gmail.com>

kernel: Implement realloc_etc and make use of it.


# 4b6af34c 21-Jun-2022 Augustin Cavalier <waddlesplash@gmail.com>

kernel: Move aligned_alloc to heap.cpp alongside calloc.

It is independent of whatever heap implementation is actually in use,
so it belongs in here (even if this file is probably not the right
place for such functions in the first place.)

This allows the kernel to be built once again with things other than
the default slab heap.


# 2823fe54 03-Jun-2022 Augustin Cavalier <waddlesplash@gmail.com>

kernel/heap: Check for multiplication overflows in calloc.

Logic inspired by musl's.


# 4e16be4f 25-Apr-2022 Augustin Cavalier <waddlesplash@gmail.com>

kernel: Do not invoke memalign with 0 as the alignment argument.

The memalign() function has special semantics for its arguments
even when -fno-builtin is enabled, it seems (that may be a problem
on Clang's part, however.) The alloc_align attribute, which we apply
to the memalign_etc function, does not seem to have the same problems;
at least its documentation at GCC gives no indication that 0 is not a
legal value to pass.

Change-Id: Ie5ba090b924ac3577775165d20f11f9696be97f3


# 9af6dc63 09-Feb-2022 Augustin Cavalier <waddlesplash@gmail.com>

kernel: Remove malloc_referenced now that it is unused.

It was added long before we had KernelReferenceable, which should
be used instead.


# 0af21102 21-Sep-2019 Augustin Cavalier <waddlesplash@gmail.com>

kernel: Fix compiling the debug heap on x86_64.


# e6ea745e 06-Dec-2013 Pawel Dziepak <pdziepak@quarnos.org>

kernel: Use SMP_MAX_CPUS instead of B_MAX_CPU_COUNT


# 0e88a887 13-Jun-2012 Alex Smith <alex@alex-smith.me.uk>

First round of 64-bit safety fixes in the kernel.

* Most of this is incorrect printf format strings. Changed all strings
causing errors to use the B_PRI* format string definitions, which
means the strings should be correct across all platforms.
* Some other fixes for errors, casts required, etc.


# 1fe24d0c 03-Dec-2011 Michael Lotz <mmlr@mlotz.ch>

Add heap with guard pages to detect out of bound reads/writes.

This is a very simple heap implementation that allocates memory so that
the end of each allocation always coincides with a page end and is
followed by a guard page which is marked non-present. Out of bounds
access (both read and write) therefore cause a crash (unhandled page
fault).

Note that this allocator is neither speed nor space efficient, indeed it
wastes huge amounts of pages and address space so it is quite easy to
hit limits. It is intended as a pure debug feature.


# 485bb14c 01-Nov-2011 Michael Lotz <mmlr@mlotz.ch>

Prefix the heap version of the "allocations" debugger command with "heap_".


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@43068 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 5fd04168 01-Nov-2011 Michael Lotz <mmlr@mlotz.ch>

Tiny cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@43063 a95241bf-73f2-0310-859d-f6bbb57e9c96


# ffb6929a 31-Oct-2011 Michael Lotz <mmlr@mlotz.ch>

bonefish+mmlr:
Move blocking the 0xcccccccc and 0xdeadbeef address ranges from heap to VM init
so that it also works when used in the slab allocator.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@43047 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 4a3d2e78 21-Jun-2011 Michael Lotz <mmlr@mlotz.ch>

Make the heap debug functions available when USE_SLAB_ALLOCATOR_FOR_MALLOC is
enabled as well. As this heap implementation is still used for the port heap
(as it handles B_NO_LOCK areas) those are still useful.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@42278 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 29145bab 02-Aug-2010 Axel Dörfler <axeld@pinc-software.de>

* Allow heap.cpp to compile if both, KERNEL_HEAP_LEAK_CHECK, and
USE_SLAB_ALLOCATOR_FOR_MALLOC are defined.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37837 a95241bf-73f2-0310-859d-f6bbb57e9c96


# f51bcfb2 18-May-2010 Ingo Weinhold <ingo_weinhold@gmx.de>

Added a heap_init_post_area() which is called right after areas can be
created, and moved the heap's grow and VIP heap initialization to it. Should
fix #5956.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36855 a95241bf-73f2-0310-859d-f6bbb57e9c96


# d3166e46 29-Apr-2010 Ingo Weinhold <ingo_weinhold@gmx.de>

Replaced no longer necessary send_signal_etc() work-arounds for
resume_thread().


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36531 a95241bf-73f2-0310-859d-f6bbb57e9c96


# fdad233b 15-Apr-2010 Michael Lotz <mmlr@mlotz.ch>

Move the actual area deletion out of the locked section as we could otherwise
double/deadlock when we delete heap areas because of other area deletions.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36284 a95241bf-73f2-0310-859d-f6bbb57e9c96


# fc813f1a 13-Apr-2010 Ingo Weinhold <ingo_weinhold@gmx.de>

Fixed typo in area name.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36232 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 610a2eed 01-Mar-2010 Michael Lotz <mmlr@mlotz.ch>

Made heap classes const just in case. They really already were though.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35713 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 8941a2ba 17-Feb-2010 Axel Dörfler <axeld@pinc-software.de>

* Fixed compilation with PARANOID_HEAP_VALIDATION turned on.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35507 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 2cb3c4eb 15-Feb-2010 Axel Dörfler <axeld@pinc-software.de>

* Improved panic output I recently ran into.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35465 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 60fb713d 07-Feb-2010 Michael Lotz <mmlr@mlotz.ch>

Must only advance by step sizes, otherwise we may end up with misaligned pages
again.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35426 a95241bf-73f2-0310-859d-f6bbb57e9c96


# b7d3ddaa 06-Feb-2010 Michael Lotz <mmlr@mlotz.ch>

Sync kernel heap with alignment handling fixes, simplifications and performance
improvements from debug heap in r35422.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35423 a95241bf-73f2-0310-859d-f6bbb57e9c96


# deee8524 26-Jan-2010 Ingo Weinhold <ingo_weinhold@gmx.de>

* Introduced {malloc,memalign,free}_etc() which take an additional "flags"
argument. They replace the previous special-purpose allocation functions
(malloc_nogrow(), vip_io_request_malloc()).
* Moved the I/O VIP heap to heap.cpp accordingly.
* Added quite a bit of passing around of allocation flags in the VM,
particularly in the VM*AddressSpace classes.
* Fixed IOBuffer::GetNextVirtualVec(): It was ignoring the VIP flag and always
allocated on the normal heap.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35316 a95241bf-73f2-0310-859d-f6bbb57e9c96


# b4e5e498 25-Jan-2010 Ingo Weinhold <ingo_weinhold@gmx.de>

MemoryManager:
* Added support to do larger raw allocations (up to one large chunk (128 pages))
in the slab areas. For an even larger allocation an area is created (haven't
seen that happen yet, though).
* Added kernel tracing (SLAB_MEMORY_MANAGER_TRACING).
* _FreeArea(): Copy and paste bug: The meta chunks of the to be freed area
would be added to the free lists instead of being removed from them. This
would corrupt the lists and also lead to all kinds of misuse of meta chunks.

object caches:
* Implemented CACHE_ALIGN_ON_SIZE. It is no longer set for all small object
caches, but the block allocator sets it on all power of two size caches.
* object_cache_reserve_internal(): Detect recursion and don't wait in such a
case. The function could deadlock itself, since
HashedObjectCache::CreateSlab() does allocate memory, thus potentially
reentering.
* object_cache_low_memory():
- I missed some returns when reworking that one in r35254, so the function
might stop early and also leave the cache in maintenance mode, which would
cause it to be ignored by object cache resizer and low memory handler from
that point on.
- Since ReturnSlab() potentially unlocks, the conditions weren't quite correct
and too many slabs could be freed.
- Simplified things a bit.
* object_cache_alloc(): Since object_cache_reserve_internal() does potentially
unlock the cache, the situation might have changed and their might not be an
empty slab available, but a partial one. The function would crash.
* Renamed the object cache tracing variable to SLAB_OBJECT_CACHE_TRACING.
* Renamed debugger command "cache_info" to "slab_cache" to avoid confusion with
the VMCache commands.
* ObjectCache::usage was not maintained anymore since I introduced the
MemoryManager. object_cache_get_usage() would thus always return 0 and the
block cache would not be considered cached memory. This was only of
informational relevance, though.

slab allocator misc.:
* Disable the object depots of block allocator caches for object sizes > 2 KB.
Allocations of those sizes aren't so common that the object depots yield any
benefit.
* The slab allocator is now fully self-sufficient. It allocates its bootstrap
memory from the MemoryManager, and the hash tables for HashedObjectCaches use
the block allocator instead of the heap, now.
* Added option to use the slab allocator for malloc() and friends
(USE_SLAB_ALLOCATOR_FOR_MALLOC). Currently disabled. Works in principle and
has virtually no lock contention. Handling for low memory situations is yet
missing, though.
* Improved the output of some debugger commands.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35283 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 3da88370 19-Jan-2010 Ingo Weinhold <ingo_weinhold@gmx.de>

* Fixed build with PARANOID_HEAP_VALIDATION.
* heap_index_for(): Could return invalid index, if there hadn't been created
a set of heaps for each CPU.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35179 a95241bf-73f2-0310-859d-f6bbb57e9c96


# a38f8503 22-Dec-2009 Ingo Weinhold <ingo_weinhold@gmx.de>

* arch_debug_get_stack_trace():
- Replaced the "userOnly" parameter by a "flags" parameter, that allows to
specify kernel and userland stack traces individually.
- x86, m68k: Don't always skip the first frame as that prevents the caller
from being able to record its own address.
* capture_tracing_stack_trace(): Replaced the "userOnly" parameter by
"kernelOnly", since one is probably always interested in the kernel stack
trace, but might not want the userland stack trace.
* Added stack trace support for VM cache kernel tracing.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34742 a95241bf-73f2-0310-859d-f6bbb57e9c96


# e50cf876 02-Dec-2009 Ingo Weinhold <ingo_weinhold@gmx.de>

* Moved the VM headers into subdirectory vm/.
* Renamed vm_cache.h/vm_address_space.h to VMCache.h/VMAddressSpace.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34449 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 8fd850fc 17-Nov-2009 Axel Dörfler <axeld@pinc-software.de>

* Added "address" specifier for the "allocations" KDL command.
* Removed superfluous malloc.h include.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34093 a95241bf-73f2-0310-859d-f6bbb57e9c96


# f28dd36b 22-Oct-2009 Axel Dörfler <axeld@pinc-software.de>

* The alphabet is obviously hard, moved some tracing defines at their
(hopefully) correct place.
* It seems to be even harder to understand basic locking primitives: when you
think about it, it shouldn't surprise you that conditional variables never
return B_WOULD_BLOCK. This fixes gdb again.
* Added tracing support to the ports subsystem.
* get_port_message() will now resize the port heap if needed (but will also
take timeouts into account while doing so, more or less). The initial port
space is 4MB (as before), the growth rate is the same, and the system wide
limit is arbitrarily set to 64 MB (all swappable). A team limit has been set
to 8 MB, but is not enforced yet. Since ports are using up address space in
the kernel, those seems to be proper limits.
* This also fixes a strange, and rare lockup where the mouse cursor would still
move, but everything else would basically hang, but look perfectly normal from
KDL on the first look. As recently happened on Brecht's laptop, and debugged
by mmlr and me: the cbuf space got used up when lots of windows wanted to
redraw after a workspace switch. The app_server wouldn't answer anymore to
client requests, but thought it would have done so, as LinkSender::Flush()
doesn't care if it got a B_NO_MEMORY (the ports will now block until memory
is available if possible, so that should not be a problem anymore).
* Improved "port" KDL command, it now also prints the messages in the port.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33735 a95241bf-73f2-0310-859d-f6bbb57e9c96


# ee7ec205 22-Oct-2009 Michael Lotz <mmlr@mlotz.ch>

Account for single page allocations. They aren't used anywhere right now because
the bin sizes ensure that when hitting this case it always allocates multiple
pages. This makes it more flexible for other use cases though.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33726 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 4e61885e 21-Oct-2009 Axel Dörfler <axeld@pinc-software.de>

mmlr+axeld:
* Fixed some minor issues of heap_create_allocator() when it should use the
kernel heap to allocate its heap structure.
* Fixed an off by one error in the max bin check.
* Changed the KDL "heap" command to allow the "stats" for any heap as well.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33725 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 6bb41327 21-Oct-2009 Michael Lotz <mmlr@mlotz.ch>

The allocation size wasn't updated on an area based realloc that would still
fit into the existing area. In that case further reallocs could then assume the
wrong previous size and then not copy enough from the original buffer, leading
to lost bytes at the end of the new buffer.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33724 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 4bee71c2 22-Oct-2009 Michael Lotz <mmlr@mlotz.ch>

* Implement per-CPU heaps. They only get enabled in case there's enough memory.
* Allow an allocator to be created on the heap to allow for non-locked
allocators to be created.
* Some cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33721 a95241bf-73f2-0310-859d-f6bbb57e9c96


# b501a038 17-Oct-2009 Michael Lotz <mmlr@mlotz.ch>

anevilyak+korli+mmlr:
* Check for overflows in memory allocation. If someone happened to (erroneously)
try to allocate a negative amount of memory we could overflow and crash
because of the sizes getting messed up.
* Review and update the alignment logic which was a bit broken for the huge
allocation case (reaching the area threshold). Also assert the results so
next time this will be easier to spot.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33638 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 65b5e4d7 09-Sep-2009 Michael Lotz <mmlr@mlotz.ch>

Make use of the address range blocking to guarantee that accessing 0xdeadbeef
and 0xcccccccc (and 64 pages thereafter) in any way will always lead to a crash.
Before it could happen that these ranges were allocated for an area and then
accessing these would not be as evident anymore. Only enabled when the
corresponding paranoid setting is enabled (which it currently is).


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33033 a95241bf-73f2-0310-859d-f6bbb57e9c96


# da341249 05-Sep-2009 Michael Lotz <mmlr@mlotz.ch>

When we actually delete the area, we can obviously not access the next pointer
stored in there anymore.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32951 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 419f2d6f 01-Sep-2009 Michael Lotz <mmlr@mlotz.ch>

Fix errors in maintaining the area list due to assumptions that are only true
for the page list but not the area one. Since multiple pages can be allocated
at once, even an area that is not at the top of the list can become empty. In
such a case the area list would previously have lost entries. Also because
we can remove more than one page from any area, not just the top one, we may
need to move forward in the list so that it stays ordered by free pages.
+alphabranch


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32880 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 65411f56 17-Aug-2009 Michael Lotz <mmlr@mlotz.ch>

We can return directly as we use handy locker objects. Adjusted and added
comments.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32483 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 73c035dc 17-Aug-2009 Michael Lotz <mmlr@mlotz.ch>

* Set freed memory to 0xdeadbeef before returning it to page. This allows us to
not hold the bin lock while setting and only protect the actual freeing. May
reduce bin lock contention a little bit.
* Tiny optimization for force-clearing 0xdeadbeef. Do it after setting to 0xcc,
so that it's less likely we have to do it.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32473 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 255c9104 03-Aug-2009 Michael Lotz <mmlr@mlotz.ch>

* Rework the heap locking strategy. Use a read-write lock for the area lock to
allow for more parallelism. Also introduce seperate locks for the bins and
for page allocation. This greatly reduces lock contention and reduces the
duration the locks are held due to them overall protecting less code. Now only
allocations of the same size hitting the same allocator or allocating larger
chunks of memory should block. Previously, basically any allocation and also
free would be mutually exclusive, making it scale pretty badely.
* Added memalign_nogrow(). As it uses heap_memalign() anyway, there's no real
reason not to allow for an alignment.
* Some cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32074 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 5efec820 27-Jul-2009 Michael Lotz <mmlr@mlotz.ch>

* Fix KDL output to use kprintf instead of dprintf.
* Make the allocations command available even without leak checking. The sizes
that are dumped aren't as accurate without leak checking info, but having the
address and max size is already helpful.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@31811 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 3873c5f3 05-May-2009 Axel Dörfler <axeld@pinc-software.de>

* Style cleanup, mostly 80 character limit per line.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30631 a95241bf-73f2-0310-859d-f6bbb57e9c96


# f8d77bfb 13-Nov-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

* The deferred_deleter() kernel daemon dropped out, if the list with
memory to free was empty, but didn't check the list with objects to
delete. So those were queued until someone used deferred_free().
Should fix #3128.
* Run the daemon once a second instead of every five seconds, so memory
is recycled a little quicker.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28644 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 412319e8 22-Oct-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

* Use a singly linked list for the deferred free entries.
* Added deferred_delete() that takes a DeferredDeletable and deletes it
asynchronously.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28285 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 59dbd26f 20-Oct-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

* Moved more debug macros to kernel_debug_config.h.
* Turned the checks for all those macros to "#if"s instead of "#ifdef"s.
* Introduced macro KDEBUG_LEVEL which serves as a master setting.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28248 a95241bf-73f2-0310-859d-f6bbb57e9c96


# e670fc6f 15-Sep-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

Added new parameter "skipIframes" to arch_debug_get_stack_trace(). That
many iframes are supposed to be skipped before recording the stack
trace. Currently implemented for x86 only.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27529 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 9e637a6a 20-Aug-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

* Added heap_set_get_caller() which can be used when heap leak checking
is enabled to set a per-heap get_caller() function.
* Added "-h <heap>" option to the "allocations_per_caller" command. If
given only the allocation for the specified heap are considered.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27097 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 749facf1 13-Aug-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

Extended the "heap" debugger command to allow specifying the address of
the heap to print infos about.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26968 a95241bf-73f2-0310-859d-f6bbb57e9c96


# f3e82cfe 13-Aug-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

* Instead of a heap class index heap_create_allocator() gets an actual
heap_class object now. Removed unused heap_allocator::heap_class.
* Made heap_class, heap_create_allocator(), heap_memalign(), heap_free()
public, so that a specialized allocator can be used elsewhere in the
kernel.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26965 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 27245755 02-Aug-2008 Axel Dörfler <axeld@pinc-software.de>

* Renamed kernel_startup to gKernelStartup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26731 a95241bf-73f2-0310-859d-f6bbb57e9c96


# e3460748 25-Jul-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

Removed superfluous whitespace.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26630 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 432e3df3 06-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

* Change the fixed bin list to a generated one based on a few constraining
factors per heap class. The previous bin sizes would in many cases just have
wasted memory by not fully using up the available space while not providing
any more elements than a bigger size. The computed bins ensure that the best
sizes are used based on a min count, min size, alignment and max waste per
page. Also changed the heap sizes to more reasonable values and renamed them
to small, medium and large.
* Remove the shouldGrow parameter from heap_memalign() and just check it using
heap_should_grow() where appropriate.
* Fix readding a nun-full area to the area list, the added page count can be
bigger than one, therefore the check must be free_page_count == pageCount.
* Simplify ensuring correct alignment, just aligning the size instead of
checking each candidate bin for alignment is enough.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26284 a95241bf-73f2-0310-859d-f6bbb57e9c96


# dde0ba62 04-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Make the all_areas list ordered by base and take advantage of this ordering
when looking up the target area on free(). This makes free() scale better with
large area counts, as the lookup can abort early when it knows that no area
below the only candidate can contain the address.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26258 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 4d9151c4 04-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

* Give the heaps a name so the grow heap can be distinguished from the small one.
* Rename sHeapList[] to sHeaps[], as there are no heap lists anymore.
* Rename the debug defines to be even more obvious.
* Also initialize allocations by area with non-null values when being paranoid.
* Disabled some debug output.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26252 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 0ea16d8b 04-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Fix missing newline.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26251 a95241bf-73f2-0310-859d-f6bbb57e9c96


# e511f726 03-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

* Implement freeing of empty heap areas. When a heap_area becomes completely
empty and there is another empty area already, it is now removed from the
heap and the underlying area is deleted.
* Fix grow suggestions to be based on 20% of a grow size instead of 10% of the
current page count, as the current page count obviously gets larger over time
making 10% of it larger than the grow size itself.
* Fix non-urgent grow request handling so that not two areas at once get added.
* Made some of the debug output more useful, turned off some other and fixed
some comments.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26244 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 85ebbd13 02-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Another partial rewrite of the kernel heap. There are no heap lists anymore,
there is only one heap_allocator for each heap class. Instead of adding
complete allocators, individual areas can now be added to existing allocators.
This removes a lot of complexity in the frontend code and avoids the previous
duplications wasting resources (allocator lock, bin sizes, bin list).
The pages are now managed in heap_area structures that are attached to areas.
They store the page table, page free lists and local variables. The same
mechanism as used for heap_pages is now used for heap_areas too, i.e. they
are kept in a sorted list where the fullest but not yet completely full area
is always at the top. This favors areas to become completely full and other,
not as frequently used areas, to become completely empty over time so they can
be freed. Full areas are removed from the list, so they do not need to be
looked at/skipped when allocating new pages. Through this mechanism and as
there are no allocator lists anymore that grow bigger over time, allocations
should be far more scalable. Note that actually removing areas once they are
not needed anymore is still not implemented, but it is now a relatively easy
addition whereas previously it would have killed the whole design.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26237 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 321ee820 02-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

* Fix the broken size/alignment computations for area allocations by replacing
them with the ROUNDUP macro that was specifically made for this task so that
too tired developers do not mess up the calculations...
* Don't use area allocation during kernel startup (not that it really happened).
* Minor simplification of similar calculations in heap_attach().

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26221 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 84c54473 02-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Added grow request tracking again. In case an allocation fails due to lack of
contiguous pages, it will request growing even if there are still more than
10% free pages available. Previously that case was not handled anymore and the
allocation would have just failed. Note that this is a pretty rare case, as
there are no "large" allocations happening in either the small or large heap
classes, but only in the huge one for allocations between 128KB and 1MB.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26220 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 883f9063 02-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Implement transparent allocation by area for huge allocations (> 1MB). This
does in fact happen, as there are objects used in the kernel that allocate
with new, which in turn uses the heap. The OpenHashTable used from the slab
allocator hit this case during an svn checkout here, because it grows it's new
allocated list by doubling the list size. Added some unconditional debug
output for those cases to monitor the usage for now. All of that not yet really
tested though.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26216 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 7431b524 02-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Fix subtle logic error that would always cause a needless large allocation if an alignment was given.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26215 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 0aad7342 01-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

The simple alignment trick used before doesn't work with the new heap classes
(it didn't really work previously anyway). The only alignment really used seems
to be B_PAGE_SIZE, which happened to always work out before, because anything
larger would have been allocated using individual pages anyway. Since larger
bins are now available and with kernel heap leak checking enabled an allocation
of B_PAGE_SIZE with B_PAGE_SIZE alignment is actually a bit bigger than
B_PAGE_SIZE, it got into the 5KB bin, which didn't guarantee the alignment
anymore. This would have caused a tripple fault on boot when kernel heap leak
checking was enabled. The alignment is now taken into account when picking
the bin for the allocation. Added a more detailed TODO as to why this isn't
the best thing (wastes space) and how it could be changed if deemed necessary.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26212 a95241bf-73f2-0310-859d-f6bbb57e9c96


# b1f4df4a 01-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Implement heap classes that each have their own range of allocation sizes they
serve, bin sizes and page size. This minimizes the amount of "large"
allocations made in heaps that don't have a bin for the allocation size
(combining multiple pages). This is desirable as such large allocations
are generally pretty inefficient, and also because it separates larger from
smaller allocations better, making the chance of a heap becoming empty higher.
For now there are three heap classes "small", "large" and "huge", with a
predefined set of bin sizes for each. This might need some finetuning later on.
Reduce the grow size to 4MB though as the allocations should now be spread
across heap classes which each grow on their own.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26210 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 8c57aefd 01-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Remove the B_PAGE_SIZE page size and replace it by a heap->page_size. This
allows for dynamic (heap)page sizes. It's currently just set to B_PAGE_SIZE
but I'm expermienting with the creation of differently sized heaps that could
use dynamic page sizes with that.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26203 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 86a38aed 01-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Take a shortcut when freeing in heap_realloc(), we already know the address belongs to our heap.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26200 a95241bf-73f2-0310-859d-f6bbb57e9c96


# a5db16fc 28-Jun-2008 Michael Lotz <mmlr@mlotz.ch>

* Implement counting free pages in kernel heaps.
* Suggest growing when there are less than 10% free pages in the last heap.
Previously it would suggest growing when there were less than three free
pages, which wasn't really any good measure. In quite a few cases this could
have lead to too late growing and running out of heap space.
* Only panic when memory allocation fails while growing kernel heaps. Otherwise
just output a message and return NULL. Even this panic is not really
necessary and should be continueable, but for now I'd like to see if this
situation actually happens.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26157 a95241bf-73f2-0310-859d-f6bbb57e9c96


# df7096a4 18-Jun-2008 Michael Lotz <mmlr@mlotz.ch>

* Added malloc_nogrow() function that does allocation without triggering or
waiting for a heap grow.
* Use that nogrow version in the VM code to avoid a deadlock with the address
space lock when a grow operation would try to create an area while a malloc
happened from such a function in the VM.
* When waiting for a grow to happen, notify the waiting thread from the grower
also if it failed to allocate a new heap. Otherwise a thread would just sit
there and wait until another thread requested growing too and that one
succeeded (or just forever in the worst case).
* Make the dedicated grow heap growable too. If the current grow heaps run low
on memory it will instruct the grower to allocate a new grow heap. This
reduces the likelyhood of running out of memory with no way to grow to a
minimum. As the growing is done asynchronously it is still possible to
happen, but it is highly unlikely as the grow heap is solely used to
allocate memory in the process of creating new heap areas and it will even
try using normal public memory if the dedicated memory has run out.
* Reduced the dedicated grow heap from 2 to 1MB. As it can now grow itself, it
doesn't need to last so long.
* Extract heap creation into it's own function that does area creation and heap
attach and use this function for growing normal and grow heaps.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26009 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 83d45323 03-Jun-2008 Michael Lotz <mmlr@mlotz.ch>

Directly use memalign instead of malloc to safe one indirection.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25776 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 5e3a974d 23-May-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

* When KERNEL_HEAP_LEAK_CHECK is enabled we also store the calling
function requesting the allocation.
* The "allocations" commands does also print the caller and can filter
by caller, now.
* Added new "allocations_per_caller" command, which sums up allocations
per caller and prints them in a table sorted by size or count.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25632 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 0c615a01 01-May-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

* Removed old mutex implementation and renamed cutex to mutex.
* Trivial adjustments of code using mutexes. Mostly removing the
mutex_init() return value check.
* Added mutex_lock_threads_locked(), which is called with the threads
spinlock being held. The spinlock is released while waiting, of
course. This function is useful in cases where the existence of the
mutex object is ensured by holding the threads spinlock.
* Changed the two instances in the VFS code where an IO context of
another team needs to be locked to use mutex_lock_threads_locked().
Before it required a semaphore-based mutex implementation.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25283 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 873f6659 28-Apr-2008 Jérôme Duval <korli@users.berlios.de>

reverted r25227. Thanks for the hint Ingo :)


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25228 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 615ffc60 28-Apr-2008 Jérôme Duval <korli@users.berlios.de>

build fix (with no tracing conf)


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25227 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 6bf15ffc 27-Apr-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

* Changed macros that enable tracing for individual components from
defined/undefined to numeric values (0 for undefined). This allows for
trace levels.
* Set SYSCALL_TRACING_IGNORE_KTRACE_OUTPUT default to 1, since this is
what one usually wants.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25213 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 3bef7d02 29-Mar-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

Added functions malloc_referenced[_{acquire,release}]() that implement
reference counted memory allocations. Can be used for sharing immutable
structures.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24666 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 5513309c 15-Mar-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

Use a union instead of the macro.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24407 a95241bf-73f2-0310-859d-f6bbb57e9c96


# f4af1fba 15-Mar-2008 Michael Lotz <mmlr@mlotz.ch>

Use the empty_index field as the allocation_id for large allocations and just
use the index of the first page of the allocation as an id. This removes the
need for separate id generation. This also fixes the possible problem of
multiple large allocations getting the same allocation_id (due to the limited
range of possible ids), which in the worst case (i.e. for adjacent allocations)
could cause pages to be freed that were still in use.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24405 a95241bf-73f2-0310-859d-f6bbb57e9c96


# c1de3c34 09-Mar-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

Added deferred_free() function, that can be used to free allocations
in code that has interrupts disabled. The chunks of memories are queued
and free()d periodically by a kernel daemon.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24332 a95241bf-73f2-0310-859d-f6bbb57e9c96


# b024c395 18-Feb-2008 Michael Lotz <mmlr@mlotz.ch>

Fix bad error in calculating the leak check info address when updating the
size of a reallocated block. If you had kernel heap leak checking on, this
could have caused the first four bytes of the next block to be overwritten
with the size of the reallocation of the previous block.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24011 a95241bf-73f2-0310-859d-f6bbb57e9c96


# cf46897b 17-Feb-2008 Michael Lotz <mmlr@mlotz.ch>

Use a dedicated heap to allocate everything that is needed during heap growth.
This eliminates the edge case where the grow thread would not be able to create
a new area because no memory could be allocated for the allocation of the area.
As this case cannot happen anymore, it is also not possible to deadlock in
memalign. Therefore the timeout (which would only have prevented the deadlock
but wouldn't have solved the edge case anyway) has been removed too.
Add options to dump the dedicated grow heap and to only print the current heap
count to the "heap" debugger command.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@23994 a95241bf-73f2-0310-859d-f6bbb57e9c96


# aabef405 14-Feb-2008 Michael Lotz <mmlr@mlotz.ch>

* Add a "stats" argument to the kernel heap leak checker to only print the
total count of allocations and bytes.
* Also add a few more bin sizes (for 8, 24 and 48 bytes) turns out especially
allocations of 20-24 bytes are pretty common. And as it only wastes a few
bytes per page this doesn't hurt at all.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@23961 a95241bf-73f2-0310-859d-f6bbb57e9c96


# cf2687e4 12-Feb-2008 Michael Lotz <mmlr@mlotz.ch>

Only clear 0xdeadbeef if there is 0xdeadbeef present. The size field of the
heap leak check info would otherwise be overwritten for allocations that still
fit the 16 byte bin (i.e. allocations of 0-4 bytes).

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@23956 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 4af6cd5f 12-Feb-2008 Michael Lotz <mmlr@mlotz.ch>

Adding two debug features to the new heap implementation:

* Tracing of allocations, reallocations and frees
* Leak checking infrastructure to dump allocations

The leak checking code records the team and thread id when an allocation is
made as well as stores the originally requested size. It also adds the
"allocations" debugger command that can dump all current allocations (usually
a huge list) or filter by either a team or thread id. This way it's easily
possible to find leftover allocations of no more active teams/threads.
Combined with the tracing support one might be able to track down the time and
reason of an allocation and possibly find the corresponding leak if it is one.
Note that kernel heap leak checking has to be enabled manually by setting the
KERNEL_HEAP_LEAK_CHECK define to 1.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@23953 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 5c4d1c5e 10-Feb-2008 Michael Lotz <mmlr@mlotz.ch>

Complete rework of the heap implementation. Freelists are now part of the pages
and pages are now kept in lists as well. This allows to return free pages once
a bin does not need them anymore. Partially filled pages are kept in a sorted
linked list so that allocation will always happen on the fullest page - this
favours having full pages and makes it more likely lightly used pages will get
completely empty so they can be returned. Generally this now goes more in the
direction of a slab allocator.
The allocation logic has been extracted, so a heap is now simply attachable to
a region of memory. This allows for multiple heaps and for dynamic growing. In
case the allocator runs out of free pages, an asynchronous growing thread is
notified to create a new area and attach a new heap to it.
By default the kernel heap is now set to 16MB and grows by 8MB each time all
heaps run full.
This should solve quite a few issues, like certain bins just claiming all pages
so that even if there is free space nothing can be allocated. Also it obviously
does aways with filling the heap page by page until it overgrows.
I think this is now a well performing and scalable allocator we can live with
for quite some time. It is well tested under emulation and real hardware and
performs as expected. If problems come up there is an extensive sanity checker
that can be enabled by PARANOID_VALIDATION that covers most aspects of the
allocator. For normal operation this is not necessary though and is therefore
disabled by default.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@23939 a95241bf-73f2-0310-859d-f6bbb57e9c96


# e6ea745e8114d5fa05ab0ccab92c070e2dcdcfb7 06-Dec-2013 Pawel Dziepak <pdziepak@quarnos.org>

kernel: Use SMP_MAX_CPUS instead of B_MAX_CPU_COUNT


# 0e88a887b4a9ecaaf1062078d9ca9bfca78fcf3a 13-Jun-2012 Alex Smith <alex@alex-smith.me.uk>

First round of 64-bit safety fixes in the kernel.

* Most of this is incorrect printf format strings. Changed all strings
causing errors to use the B_PRI* format string definitions, which
means the strings should be correct across all platforms.
* Some other fixes for errors, casts required, etc.


# 1fe24d0cd0b547a771c00f6fca8f50ba6ca2fb2c 03-Dec-2011 Michael Lotz <mmlr@mlotz.ch>

Add heap with guard pages to detect out of bound reads/writes.

This is a very simple heap implementation that allocates memory so that
the end of each allocation always coincides with a page end and is
followed by a guard page which is marked non-present. Out of bounds
access (both read and write) therefore cause a crash (unhandled page
fault).

Note that this allocator is neither speed nor space efficient, indeed it
wastes huge amounts of pages and address space so it is quite easy to
hit limits. It is intended as a pure debug feature.


# 485bb14c36312aeeb73b25f8f389cb401ed1abbf 01-Nov-2011 Michael Lotz <mmlr@mlotz.ch>

Prefix the heap version of the "allocations" debugger command with "heap_".


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@43068 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 5fd0416842b615025017ab7df50886757063c842 01-Nov-2011 Michael Lotz <mmlr@mlotz.ch>

Tiny cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@43063 a95241bf-73f2-0310-859d-f6bbb57e9c96


# ffb6929a3b30dd9367feb64368e7c50448ff59dd 31-Oct-2011 Michael Lotz <mmlr@mlotz.ch>

bonefish+mmlr:
Move blocking the 0xcccccccc and 0xdeadbeef address ranges from heap to VM init
so that it also works when used in the slab allocator.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@43047 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 4a3d2e780899602237cdf09aa30c3ad48b01c342 21-Jun-2011 Michael Lotz <mmlr@mlotz.ch>

Make the heap debug functions available when USE_SLAB_ALLOCATOR_FOR_MALLOC is
enabled as well. As this heap implementation is still used for the port heap
(as it handles B_NO_LOCK areas) those are still useful.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@42278 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 29145bab8a8fe1dc97aa9afa2957011d3bcd16b8 02-Aug-2010 Axel Dörfler <axeld@pinc-software.de>

* Allow heap.cpp to compile if both, KERNEL_HEAP_LEAK_CHECK, and
USE_SLAB_ALLOCATOR_FOR_MALLOC are defined.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37837 a95241bf-73f2-0310-859d-f6bbb57e9c96


# f51bcfb25be138f58194c8e4fc86ae87f3ecb3c2 18-May-2010 Ingo Weinhold <ingo_weinhold@gmx.de>

Added a heap_init_post_area() which is called right after areas can be
created, and moved the heap's grow and VIP heap initialization to it. Should
fix #5956.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36855 a95241bf-73f2-0310-859d-f6bbb57e9c96


# d3166e469cd337a6910f1cb1515b74f73d36466f 29-Apr-2010 Ingo Weinhold <ingo_weinhold@gmx.de>

Replaced no longer necessary send_signal_etc() work-arounds for
resume_thread().


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36531 a95241bf-73f2-0310-859d-f6bbb57e9c96


# fdad233b47853c1e8e07d520aa6b9f21e7bca44a 15-Apr-2010 Michael Lotz <mmlr@mlotz.ch>

Move the actual area deletion out of the locked section as we could otherwise
double/deadlock when we delete heap areas because of other area deletions.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36284 a95241bf-73f2-0310-859d-f6bbb57e9c96


# fc813f1a1ace8709db08a37196aff9cfbffc5513 13-Apr-2010 Ingo Weinhold <ingo_weinhold@gmx.de>

Fixed typo in area name.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36232 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 610a2eed63be6e9b81c23bb0461ea650274973a3 01-Mar-2010 Michael Lotz <mmlr@mlotz.ch>

Made heap classes const just in case. They really already were though.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35713 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 8941a2ba08e114db56a3d90e6d479b8bbb0b9d23 17-Feb-2010 Axel Dörfler <axeld@pinc-software.de>

* Fixed compilation with PARANOID_HEAP_VALIDATION turned on.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35507 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 2cb3c4eb0607ca2d67de71871868ba7ce3b13b0a 15-Feb-2010 Axel Dörfler <axeld@pinc-software.de>

* Improved panic output I recently ran into.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35465 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 60fb713d1995a0055e203def9c4436393e6557e1 07-Feb-2010 Michael Lotz <mmlr@mlotz.ch>

Must only advance by step sizes, otherwise we may end up with misaligned pages
again.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35426 a95241bf-73f2-0310-859d-f6bbb57e9c96


# b7d3ddaa0e180f67a65cf832a430aa665870e624 06-Feb-2010 Michael Lotz <mmlr@mlotz.ch>

Sync kernel heap with alignment handling fixes, simplifications and performance
improvements from debug heap in r35422.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35423 a95241bf-73f2-0310-859d-f6bbb57e9c96


# deee8524b7534d9b586cbcbf366d0660c9769a8e 26-Jan-2010 Ingo Weinhold <ingo_weinhold@gmx.de>

* Introduced {malloc,memalign,free}_etc() which take an additional "flags"
argument. They replace the previous special-purpose allocation functions
(malloc_nogrow(), vip_io_request_malloc()).
* Moved the I/O VIP heap to heap.cpp accordingly.
* Added quite a bit of passing around of allocation flags in the VM,
particularly in the VM*AddressSpace classes.
* Fixed IOBuffer::GetNextVirtualVec(): It was ignoring the VIP flag and always
allocated on the normal heap.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35316 a95241bf-73f2-0310-859d-f6bbb57e9c96


# b4e5e4982360e684c5a13d227b9a958dbe725554 25-Jan-2010 Ingo Weinhold <ingo_weinhold@gmx.de>

MemoryManager:
* Added support to do larger raw allocations (up to one large chunk (128 pages))
in the slab areas. For an even larger allocation an area is created (haven't
seen that happen yet, though).
* Added kernel tracing (SLAB_MEMORY_MANAGER_TRACING).
* _FreeArea(): Copy and paste bug: The meta chunks of the to be freed area
would be added to the free lists instead of being removed from them. This
would corrupt the lists and also lead to all kinds of misuse of meta chunks.

object caches:
* Implemented CACHE_ALIGN_ON_SIZE. It is no longer set for all small object
caches, but the block allocator sets it on all power of two size caches.
* object_cache_reserve_internal(): Detect recursion and don't wait in such a
case. The function could deadlock itself, since
HashedObjectCache::CreateSlab() does allocate memory, thus potentially
reentering.
* object_cache_low_memory():
- I missed some returns when reworking that one in r35254, so the function
might stop early and also leave the cache in maintenance mode, which would
cause it to be ignored by object cache resizer and low memory handler from
that point on.
- Since ReturnSlab() potentially unlocks, the conditions weren't quite correct
and too many slabs could be freed.
- Simplified things a bit.
* object_cache_alloc(): Since object_cache_reserve_internal() does potentially
unlock the cache, the situation might have changed and their might not be an
empty slab available, but a partial one. The function would crash.
* Renamed the object cache tracing variable to SLAB_OBJECT_CACHE_TRACING.
* Renamed debugger command "cache_info" to "slab_cache" to avoid confusion with
the VMCache commands.
* ObjectCache::usage was not maintained anymore since I introduced the
MemoryManager. object_cache_get_usage() would thus always return 0 and the
block cache would not be considered cached memory. This was only of
informational relevance, though.

slab allocator misc.:
* Disable the object depots of block allocator caches for object sizes > 2 KB.
Allocations of those sizes aren't so common that the object depots yield any
benefit.
* The slab allocator is now fully self-sufficient. It allocates its bootstrap
memory from the MemoryManager, and the hash tables for HashedObjectCaches use
the block allocator instead of the heap, now.
* Added option to use the slab allocator for malloc() and friends
(USE_SLAB_ALLOCATOR_FOR_MALLOC). Currently disabled. Works in principle and
has virtually no lock contention. Handling for low memory situations is yet
missing, though.
* Improved the output of some debugger commands.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35283 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 3da88370694c0fa84cb7e31c70bb23042d3fe265 19-Jan-2010 Ingo Weinhold <ingo_weinhold@gmx.de>

* Fixed build with PARANOID_HEAP_VALIDATION.
* heap_index_for(): Could return invalid index, if there hadn't been created
a set of heaps for each CPU.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35179 a95241bf-73f2-0310-859d-f6bbb57e9c96


# a38f85036021b7ccaffa47fd8584afff473acf3d 22-Dec-2009 Ingo Weinhold <ingo_weinhold@gmx.de>

* arch_debug_get_stack_trace():
- Replaced the "userOnly" parameter by a "flags" parameter, that allows to
specify kernel and userland stack traces individually.
- x86, m68k: Don't always skip the first frame as that prevents the caller
from being able to record its own address.
* capture_tracing_stack_trace(): Replaced the "userOnly" parameter by
"kernelOnly", since one is probably always interested in the kernel stack
trace, but might not want the userland stack trace.
* Added stack trace support for VM cache kernel tracing.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34742 a95241bf-73f2-0310-859d-f6bbb57e9c96


# e50cf8765be50a7454c9488db38b638cf90805af 02-Dec-2009 Ingo Weinhold <ingo_weinhold@gmx.de>

* Moved the VM headers into subdirectory vm/.
* Renamed vm_cache.h/vm_address_space.h to VMCache.h/VMAddressSpace.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34449 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 8fd850fca9fcfab3782688c81a0e8397fa97455c 17-Nov-2009 Axel Dörfler <axeld@pinc-software.de>

* Added "address" specifier for the "allocations" KDL command.
* Removed superfluous malloc.h include.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34093 a95241bf-73f2-0310-859d-f6bbb57e9c96


# f28dd36b8271a80746985faef453afee6526f416 22-Oct-2009 Axel Dörfler <axeld@pinc-software.de>

* The alphabet is obviously hard, moved some tracing defines at their
(hopefully) correct place.
* It seems to be even harder to understand basic locking primitives: when you
think about it, it shouldn't surprise you that conditional variables never
return B_WOULD_BLOCK. This fixes gdb again.
* Added tracing support to the ports subsystem.
* get_port_message() will now resize the port heap if needed (but will also
take timeouts into account while doing so, more or less). The initial port
space is 4MB (as before), the growth rate is the same, and the system wide
limit is arbitrarily set to 64 MB (all swappable). A team limit has been set
to 8 MB, but is not enforced yet. Since ports are using up address space in
the kernel, those seems to be proper limits.
* This also fixes a strange, and rare lockup where the mouse cursor would still
move, but everything else would basically hang, but look perfectly normal from
KDL on the first look. As recently happened on Brecht's laptop, and debugged
by mmlr and me: the cbuf space got used up when lots of windows wanted to
redraw after a workspace switch. The app_server wouldn't answer anymore to
client requests, but thought it would have done so, as LinkSender::Flush()
doesn't care if it got a B_NO_MEMORY (the ports will now block until memory
is available if possible, so that should not be a problem anymore).
* Improved "port" KDL command, it now also prints the messages in the port.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33735 a95241bf-73f2-0310-859d-f6bbb57e9c96


# ee7ec20503558510377023a090bb36b19f7eebd3 22-Oct-2009 Michael Lotz <mmlr@mlotz.ch>

Account for single page allocations. They aren't used anywhere right now because
the bin sizes ensure that when hitting this case it always allocates multiple
pages. This makes it more flexible for other use cases though.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33726 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 4e61885e4ab1981d7983b85a64c3f56d140c4f78 21-Oct-2009 Axel Dörfler <axeld@pinc-software.de>

mmlr+axeld:
* Fixed some minor issues of heap_create_allocator() when it should use the
kernel heap to allocate its heap structure.
* Fixed an off by one error in the max bin check.
* Changed the KDL "heap" command to allow the "stats" for any heap as well.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33725 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 6bb413270ba254d2592b22cb4a1a7283e9989cbd 21-Oct-2009 Michael Lotz <mmlr@mlotz.ch>

The allocation size wasn't updated on an area based realloc that would still
fit into the existing area. In that case further reallocs could then assume the
wrong previous size and then not copy enough from the original buffer, leading
to lost bytes at the end of the new buffer.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33724 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 4bee71c211594ce0bd1b3dbb978e8a771b7e5628 22-Oct-2009 Michael Lotz <mmlr@mlotz.ch>

* Implement per-CPU heaps. They only get enabled in case there's enough memory.
* Allow an allocator to be created on the heap to allow for non-locked
allocators to be created.
* Some cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33721 a95241bf-73f2-0310-859d-f6bbb57e9c96


# b501a0387df65dd53f9d5f7edc12e9e7d7cba100 17-Oct-2009 Michael Lotz <mmlr@mlotz.ch>

anevilyak+korli+mmlr:
* Check for overflows in memory allocation. If someone happened to (erroneously)
try to allocate a negative amount of memory we could overflow and crash
because of the sizes getting messed up.
* Review and update the alignment logic which was a bit broken for the huge
allocation case (reaching the area threshold). Also assert the results so
next time this will be easier to spot.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33638 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 65b5e4d7ed880bb91a69b68e8e12b6d9178f2bb7 09-Sep-2009 Michael Lotz <mmlr@mlotz.ch>

Make use of the address range blocking to guarantee that accessing 0xdeadbeef
and 0xcccccccc (and 64 pages thereafter) in any way will always lead to a crash.
Before it could happen that these ranges were allocated for an area and then
accessing these would not be as evident anymore. Only enabled when the
corresponding paranoid setting is enabled (which it currently is).


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33033 a95241bf-73f2-0310-859d-f6bbb57e9c96


# da3412492addfd95c3fe45cf351e307ef322dcfa 05-Sep-2009 Michael Lotz <mmlr@mlotz.ch>

When we actually delete the area, we can obviously not access the next pointer
stored in there anymore.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32951 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 419f2d6f3ed3f9cddc9932829fda4f7a114c853c 01-Sep-2009 Michael Lotz <mmlr@mlotz.ch>

Fix errors in maintaining the area list due to assumptions that are only true
for the page list but not the area one. Since multiple pages can be allocated
at once, even an area that is not at the top of the list can become empty. In
such a case the area list would previously have lost entries. Also because
we can remove more than one page from any area, not just the top one, we may
need to move forward in the list so that it stays ordered by free pages.
+alphabranch


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32880 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 65411f56b753b38daa897b6982bfa6f289180951 17-Aug-2009 Michael Lotz <mmlr@mlotz.ch>

We can return directly as we use handy locker objects. Adjusted and added
comments.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32483 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 73c035dc118bbc4f160b527a94a9593d3bfc4e35 17-Aug-2009 Michael Lotz <mmlr@mlotz.ch>

* Set freed memory to 0xdeadbeef before returning it to page. This allows us to
not hold the bin lock while setting and only protect the actual freeing. May
reduce bin lock contention a little bit.
* Tiny optimization for force-clearing 0xdeadbeef. Do it after setting to 0xcc,
so that it's less likely we have to do it.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32473 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 255c9104e52fbdf2c21189fb269ba6241ebea788 03-Aug-2009 Michael Lotz <mmlr@mlotz.ch>

* Rework the heap locking strategy. Use a read-write lock for the area lock to
allow for more parallelism. Also introduce seperate locks for the bins and
for page allocation. This greatly reduces lock contention and reduces the
duration the locks are held due to them overall protecting less code. Now only
allocations of the same size hitting the same allocator or allocating larger
chunks of memory should block. Previously, basically any allocation and also
free would be mutually exclusive, making it scale pretty badely.
* Added memalign_nogrow(). As it uses heap_memalign() anyway, there's no real
reason not to allow for an alignment.
* Some cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32074 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 5efec82035e01720f95dee7c3f6dbffbe1b46ab7 27-Jul-2009 Michael Lotz <mmlr@mlotz.ch>

* Fix KDL output to use kprintf instead of dprintf.
* Make the allocations command available even without leak checking. The sizes
that are dumped aren't as accurate without leak checking info, but having the
address and max size is already helpful.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@31811 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 3873c5f3f2e53544e15425146e3a7df00a1da1b4 05-May-2009 Axel Dörfler <axeld@pinc-software.de>

* Style cleanup, mostly 80 character limit per line.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30631 a95241bf-73f2-0310-859d-f6bbb57e9c96


# f8d77bfb1c2e17274771ff800a09cb5e0bf45ecc 13-Nov-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

* The deferred_deleter() kernel daemon dropped out, if the list with
memory to free was empty, but didn't check the list with objects to
delete. So those were queued until someone used deferred_free().
Should fix #3128.
* Run the daemon once a second instead of every five seconds, so memory
is recycled a little quicker.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28644 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 412319e8eb2b8d594d23eb679223bf06be9b232b 22-Oct-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

* Use a singly linked list for the deferred free entries.
* Added deferred_delete() that takes a DeferredDeletable and deletes it
asynchronously.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28285 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 59dbd26f5f41a6c1272f6cac9c8cda4b19b79097 20-Oct-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

* Moved more debug macros to kernel_debug_config.h.
* Turned the checks for all those macros to "#if"s instead of "#ifdef"s.
* Introduced macro KDEBUG_LEVEL which serves as a master setting.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28248 a95241bf-73f2-0310-859d-f6bbb57e9c96


# e670fc6f6346a09cd96a8ef01742e835086bb458 15-Sep-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

Added new parameter "skipIframes" to arch_debug_get_stack_trace(). That
many iframes are supposed to be skipped before recording the stack
trace. Currently implemented for x86 only.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27529 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 9e637a6a842607096cef0f9303ab9edaa2e367a2 20-Aug-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

* Added heap_set_get_caller() which can be used when heap leak checking
is enabled to set a per-heap get_caller() function.
* Added "-h <heap>" option to the "allocations_per_caller" command. If
given only the allocation for the specified heap are considered.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27097 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 749facf1eb63935bdbc895858a3cc009efdc2505 13-Aug-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

Extended the "heap" debugger command to allow specifying the address of
the heap to print infos about.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26968 a95241bf-73f2-0310-859d-f6bbb57e9c96


# f3e82cfe4250e9f511ad0423831c123c72226987 13-Aug-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

* Instead of a heap class index heap_create_allocator() gets an actual
heap_class object now. Removed unused heap_allocator::heap_class.
* Made heap_class, heap_create_allocator(), heap_memalign(), heap_free()
public, so that a specialized allocator can be used elsewhere in the
kernel.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26965 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 272457553e0ff0860cf931c1d193c2c6020a7da8 02-Aug-2008 Axel Dörfler <axeld@pinc-software.de>

* Renamed kernel_startup to gKernelStartup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26731 a95241bf-73f2-0310-859d-f6bbb57e9c96


# e346074894890c95540892b366859dfa38b613c2 25-Jul-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

Removed superfluous whitespace.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26630 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 432e3df3c742791702a728c95119f4ca25b0b46a 06-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

* Change the fixed bin list to a generated one based on a few constraining
factors per heap class. The previous bin sizes would in many cases just have
wasted memory by not fully using up the available space while not providing
any more elements than a bigger size. The computed bins ensure that the best
sizes are used based on a min count, min size, alignment and max waste per
page. Also changed the heap sizes to more reasonable values and renamed them
to small, medium and large.
* Remove the shouldGrow parameter from heap_memalign() and just check it using
heap_should_grow() where appropriate.
* Fix readding a nun-full area to the area list, the added page count can be
bigger than one, therefore the check must be free_page_count == pageCount.
* Simplify ensuring correct alignment, just aligning the size instead of
checking each candidate bin for alignment is enough.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26284 a95241bf-73f2-0310-859d-f6bbb57e9c96


# dde0ba62233248d527aa7336bca599bce52b13d7 04-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Make the all_areas list ordered by base and take advantage of this ordering
when looking up the target area on free(). This makes free() scale better with
large area counts, as the lookup can abort early when it knows that no area
below the only candidate can contain the address.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26258 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 4d9151c499bf7ec7315777d11c78bfdcc568e3c3 04-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

* Give the heaps a name so the grow heap can be distinguished from the small one.
* Rename sHeapList[] to sHeaps[], as there are no heap lists anymore.
* Rename the debug defines to be even more obvious.
* Also initialize allocations by area with non-null values when being paranoid.
* Disabled some debug output.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26252 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 0ea16d8b5bbf90de364ff524ce938349422a0b81 04-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Fix missing newline.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26251 a95241bf-73f2-0310-859d-f6bbb57e9c96


# e511f726ea3a9a3de40db2b59410c2e3a478703d 03-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

* Implement freeing of empty heap areas. When a heap_area becomes completely
empty and there is another empty area already, it is now removed from the
heap and the underlying area is deleted.
* Fix grow suggestions to be based on 20% of a grow size instead of 10% of the
current page count, as the current page count obviously gets larger over time
making 10% of it larger than the grow size itself.
* Fix non-urgent grow request handling so that not two areas at once get added.
* Made some of the debug output more useful, turned off some other and fixed
some comments.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26244 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 85ebbd1318d1ceca34d30345e23a582f586cc557 02-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Another partial rewrite of the kernel heap. There are no heap lists anymore,
there is only one heap_allocator for each heap class. Instead of adding
complete allocators, individual areas can now be added to existing allocators.
This removes a lot of complexity in the frontend code and avoids the previous
duplications wasting resources (allocator lock, bin sizes, bin list).
The pages are now managed in heap_area structures that are attached to areas.
They store the page table, page free lists and local variables. The same
mechanism as used for heap_pages is now used for heap_areas too, i.e. they
are kept in a sorted list where the fullest but not yet completely full area
is always at the top. This favors areas to become completely full and other,
not as frequently used areas, to become completely empty over time so they can
be freed. Full areas are removed from the list, so they do not need to be
looked at/skipped when allocating new pages. Through this mechanism and as
there are no allocator lists anymore that grow bigger over time, allocations
should be far more scalable. Note that actually removing areas once they are
not needed anymore is still not implemented, but it is now a relatively easy
addition whereas previously it would have killed the whole design.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26237 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 321ee820ea6c03f95ab426c8d61cb63ff78c8800 02-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

* Fix the broken size/alignment computations for area allocations by replacing
them with the ROUNDUP macro that was specifically made for this task so that
too tired developers do not mess up the calculations...
* Don't use area allocation during kernel startup (not that it really happened).
* Minor simplification of similar calculations in heap_attach().

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26221 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 84c54473ebc223583b921bab14ccaf310065e298 02-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Added grow request tracking again. In case an allocation fails due to lack of
contiguous pages, it will request growing even if there are still more than
10% free pages available. Previously that case was not handled anymore and the
allocation would have just failed. Note that this is a pretty rare case, as
there are no "large" allocations happening in either the small or large heap
classes, but only in the huge one for allocations between 128KB and 1MB.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26220 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 883f9063efb52182514688a833d6a75a77cba56c 02-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Implement transparent allocation by area for huge allocations (> 1MB). This
does in fact happen, as there are objects used in the kernel that allocate
with new, which in turn uses the heap. The OpenHashTable used from the slab
allocator hit this case during an svn checkout here, because it grows it's new
allocated list by doubling the list size. Added some unconditional debug
output for those cases to monitor the usage for now. All of that not yet really
tested though.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26216 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 7431b5241ed1b6abb2827cd4aba9ee86452789ce 02-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Fix subtle logic error that would always cause a needless large allocation if an alignment was given.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26215 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 0aad7342300dbf1580ea14b01e1b52c36aba674f 01-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

The simple alignment trick used before doesn't work with the new heap classes
(it didn't really work previously anyway). The only alignment really used seems
to be B_PAGE_SIZE, which happened to always work out before, because anything
larger would have been allocated using individual pages anyway. Since larger
bins are now available and with kernel heap leak checking enabled an allocation
of B_PAGE_SIZE with B_PAGE_SIZE alignment is actually a bit bigger than
B_PAGE_SIZE, it got into the 5KB bin, which didn't guarantee the alignment
anymore. This would have caused a tripple fault on boot when kernel heap leak
checking was enabled. The alignment is now taken into account when picking
the bin for the allocation. Added a more detailed TODO as to why this isn't
the best thing (wastes space) and how it could be changed if deemed necessary.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26212 a95241bf-73f2-0310-859d-f6bbb57e9c96


# b1f4df4a62fea4bdcf165aa5f43ec16cd74ad189 01-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Implement heap classes that each have their own range of allocation sizes they
serve, bin sizes and page size. This minimizes the amount of "large"
allocations made in heaps that don't have a bin for the allocation size
(combining multiple pages). This is desirable as such large allocations
are generally pretty inefficient, and also because it separates larger from
smaller allocations better, making the chance of a heap becoming empty higher.
For now there are three heap classes "small", "large" and "huge", with a
predefined set of bin sizes for each. This might need some finetuning later on.
Reduce the grow size to 4MB though as the allocations should now be spread
across heap classes which each grow on their own.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26210 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 8c57aefdf8dd7a4099a479109ecfc33dd7c31388 01-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Remove the B_PAGE_SIZE page size and replace it by a heap->page_size. This
allows for dynamic (heap)page sizes. It's currently just set to B_PAGE_SIZE
but I'm expermienting with the creation of differently sized heaps that could
use dynamic page sizes with that.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26203 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 86a38aedf4b645a62312c29c927051bab9c4b0a1 01-Jul-2008 Michael Lotz <mmlr@mlotz.ch>

Take a shortcut when freeing in heap_realloc(), we already know the address belongs to our heap.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26200 a95241bf-73f2-0310-859d-f6bbb57e9c96


# a5db16fc793ead7a566ebaa60ccc1b3ed863f465 28-Jun-2008 Michael Lotz <mmlr@mlotz.ch>

* Implement counting free pages in kernel heaps.
* Suggest growing when there are less than 10% free pages in the last heap.
Previously it would suggest growing when there were less than three free
pages, which wasn't really any good measure. In quite a few cases this could
have lead to too late growing and running out of heap space.
* Only panic when memory allocation fails while growing kernel heaps. Otherwise
just output a message and return NULL. Even this panic is not really
necessary and should be continueable, but for now I'd like to see if this
situation actually happens.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26157 a95241bf-73f2-0310-859d-f6bbb57e9c96


# df7096a4aaa8076f7ebbc617f914700c9a5c0109 18-Jun-2008 Michael Lotz <mmlr@mlotz.ch>

* Added malloc_nogrow() function that does allocation without triggering or
waiting for a heap grow.
* Use that nogrow version in the VM code to avoid a deadlock with the address
space lock when a grow operation would try to create an area while a malloc
happened from such a function in the VM.
* When waiting for a grow to happen, notify the waiting thread from the grower
also if it failed to allocate a new heap. Otherwise a thread would just sit
there and wait until another thread requested growing too and that one
succeeded (or just forever in the worst case).
* Make the dedicated grow heap growable too. If the current grow heaps run low
on memory it will instruct the grower to allocate a new grow heap. This
reduces the likelyhood of running out of memory with no way to grow to a
minimum. As the growing is done asynchronously it is still possible to
happen, but it is highly unlikely as the grow heap is solely used to
allocate memory in the process of creating new heap areas and it will even
try using normal public memory if the dedicated memory has run out.
* Reduced the dedicated grow heap from 2 to 1MB. As it can now grow itself, it
doesn't need to last so long.
* Extract heap creation into it's own function that does area creation and heap
attach and use this function for growing normal and grow heaps.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26009 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 83d453239ce797181cb62d941e152f4263b1f052 03-Jun-2008 Michael Lotz <mmlr@mlotz.ch>

Directly use memalign instead of malloc to safe one indirection.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25776 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 5e3a974d1eb42956f729b9583429374c619c88f7 23-May-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

* When KERNEL_HEAP_LEAK_CHECK is enabled we also store the calling
function requesting the allocation.
* The "allocations" commands does also print the caller and can filter
by caller, now.
* Added new "allocations_per_caller" command, which sums up allocations
per caller and prints them in a table sorted by size or count.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25632 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 0c615a01ae49634aaf59fbe35b3d55b3bb8890df 01-May-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

* Removed old mutex implementation and renamed cutex to mutex.
* Trivial adjustments of code using mutexes. Mostly removing the
mutex_init() return value check.
* Added mutex_lock_threads_locked(), which is called with the threads
spinlock being held. The spinlock is released while waiting, of
course. This function is useful in cases where the existence of the
mutex object is ensured by holding the threads spinlock.
* Changed the two instances in the VFS code where an IO context of
another team needs to be locked to use mutex_lock_threads_locked().
Before it required a semaphore-based mutex implementation.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25283 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 873f6659eb778776900c54ad9cc2a85455f5a6ca 28-Apr-2008 Jérôme Duval <korli@users.berlios.de>

reverted r25227. Thanks for the hint Ingo :)


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25228 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 615ffc60ccb5f0c68e5020a80ea559a560d65101 28-Apr-2008 Jérôme Duval <korli@users.berlios.de>

build fix (with no tracing conf)


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25227 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 6bf15ffcdcfc62c39948e0e9449064e65b7f13bd 27-Apr-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

* Changed macros that enable tracing for individual components from
defined/undefined to numeric values (0 for undefined). This allows for
trace levels.
* Set SYSCALL_TRACING_IGNORE_KTRACE_OUTPUT default to 1, since this is
what one usually wants.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25213 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 3bef7d02527eb6eea0fe408499978cc9fc52117b 29-Mar-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

Added functions malloc_referenced[_{acquire,release}]() that implement
reference counted memory allocations. Can be used for sharing immutable
structures.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24666 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 5513309cb44035ce79bf025bd94cd104c19e9597 15-Mar-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

Use a union instead of the macro.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24407 a95241bf-73f2-0310-859d-f6bbb57e9c96


# f4af1fba51532f42723facc3fb305d39f0ba5955 15-Mar-2008 Michael Lotz <mmlr@mlotz.ch>

Use the empty_index field as the allocation_id for large allocations and just
use the index of the first page of the allocation as an id. This removes the
need for separate id generation. This also fixes the possible problem of
multiple large allocations getting the same allocation_id (due to the limited
range of possible ids), which in the worst case (i.e. for adjacent allocations)
could cause pages to be freed that were still in use.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24405 a95241bf-73f2-0310-859d-f6bbb57e9c96


# c1de3c34b3370a5a00d8e74b4646343814016431 09-Mar-2008 Ingo Weinhold <ingo_weinhold@gmx.de>

Added deferred_free() function, that can be used to free allocations
in code that has interrupts disabled. The chunks of memories are queued
and free()d periodically by a kernel daemon.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24332 a95241bf-73f2-0310-859d-f6bbb57e9c96


# b024c395d7eaa61d7e784c380e924c59ddd043f6 18-Feb-2008 Michael Lotz <mmlr@mlotz.ch>

Fix bad error in calculating the leak check info address when updating the
size of a reallocated block. If you had kernel heap leak checking on, this
could have caused the first four bytes of the next block to be overwritten
with the size of the reallocation of the previous block.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24011 a95241bf-73f2-0310-859d-f6bbb57e9c96


# cf46897b4c9a7a275d3d7c6e9c283a3efbe9395b 17-Feb-2008 Michael Lotz <mmlr@mlotz.ch>

Use a dedicated heap to allocate everything that is needed during heap growth.
This eliminates the edge case where the grow thread would not be able to create
a new area because no memory could be allocated for the allocation of the area.
As this case cannot happen anymore, it is also not possible to deadlock in
memalign. Therefore the timeout (which would only have prevented the deadlock
but wouldn't have solved the edge case anyway) has been removed too.
Add options to dump the dedicated grow heap and to only print the current heap
count to the "heap" debugger command.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@23994 a95241bf-73f2-0310-859d-f6bbb57e9c96


# aabef40563f577ecffdc6dff8b459b860eb11866 14-Feb-2008 Michael Lotz <mmlr@mlotz.ch>

* Add a "stats" argument to the kernel heap leak checker to only print the
total count of allocations and bytes.
* Also add a few more bin sizes (for 8, 24 and 48 bytes) turns out especially
allocations of 20-24 bytes are pretty common. And as it only wastes a few
bytes per page this doesn't hurt at all.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@23961 a95241bf-73f2-0310-859d-f6bbb57e9c96


# cf2687e48f58b40c79b5b7b5fb95b611e5260ab2 12-Feb-2008 Michael Lotz <mmlr@mlotz.ch>

Only clear 0xdeadbeef if there is 0xdeadbeef present. The size field of the
heap leak check info would otherwise be overwritten for allocations that still
fit the 16 byte bin (i.e. allocations of 0-4 bytes).

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@23956 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 4af6cd5f383319e59b741ca82284084bc9f91e6a 12-Feb-2008 Michael Lotz <mmlr@mlotz.ch>

Adding two debug features to the new heap implementation:

* Tracing of allocations, reallocations and frees
* Leak checking infrastructure to dump allocations

The leak checking code records the team and thread id when an allocation is
made as well as stores the originally requested size. It also adds the
"allocations" debugger command that can dump all current allocations (usually
a huge list) or filter by either a team or thread id. This way it's easily
possible to find leftover allocations of no more active teams/threads.
Combined with the tracing support one might be able to track down the time and
reason of an allocation and possibly find the corresponding leak if it is one.
Note that kernel heap leak checking has to be enabled manually by setting the
KERNEL_HEAP_LEAK_CHECK define to 1.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@23953 a95241bf-73f2-0310-859d-f6bbb57e9c96


# 5c4d1c5e21dcf7e66414c44691ff0aa92be93842 10-Feb-2008 Michael Lotz <mmlr@mlotz.ch>

Complete rework of the heap implementation. Freelists are now part of the pages
and pages are now kept in lists as well. This allows to return free pages once
a bin does not need them anymore. Partially filled pages are kept in a sorted
linked list so that allocation will always happen on the fullest page - this
favours having full pages and makes it more likely lightly used pages will get
completely empty so they can be returned. Generally this now goes more in the
direction of a slab allocator.
The allocation logic has been extracted, so a heap is now simply attachable to
a region of memory. This allows for multiple heaps and for dynamic growing. In
case the allocator runs out of free pages, an asynchronous growing thread is
notified to create a new area and attach a new heap to it.
By default the kernel heap is now set to 16MB and grows by 8MB each time all
heaps run full.
This should solve quite a few issues, like certain bins just claiming all pages
so that even if there is free space nothing can be allocated. Also it obviously
does aways with filling the heap page by page until it overgrows.
I think this is now a well performing and scalable allocator we can live with
for quite some time. It is well tested under emulation and real hardware and
performs as expected. If problems come up there is an extensive sanity checker
that can be enabled by PARANOID_VALIDATION that covers most aspects of the
allocator. For normal operation this is not necessary though and is therefore
disabled by default.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@23939 a95241bf-73f2-0310-859d-f6bbb57e9c96