2012-08-01 08:27:02 -04:00
|
|
|
/*******************************************************************************
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2014-05-15 09:03:54 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014.
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of the Sortix C Library.
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
The Sortix C Library is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
The Sortix C Library is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2013-06-24 17:52:59 -04:00
|
|
|
stdlib/heap.cpp
|
2013-07-10 09:26:01 -04:00
|
|
|
Functions that allocate/free memory from a dynamic memory heap.
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-08-01 08:27:02 -04:00
|
|
|
*******************************************************************************/
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-03-21 12:19:26 -04:00
|
|
|
#include <sys/mman.h>
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2014-01-18 10:21:46 -05:00
|
|
|
#if __STDC_HOSTED__
|
2012-12-07 07:11:26 -05:00
|
|
|
#include <error.h>
|
2012-09-07 15:46:43 -04:00
|
|
|
#include <stdio.h>
|
2011-12-16 07:24:49 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2012-09-22 08:57:20 -04:00
|
|
|
#include <assert.h>
|
2012-09-22 10:44:50 -04:00
|
|
|
#include <errno.h>
|
2012-09-22 08:14:58 -04:00
|
|
|
#include <malloc.h>
|
2013-06-20 08:46:10 -04:00
|
|
|
#include <stdint.h>
|
2012-09-23 07:55:58 -04:00
|
|
|
#include <stdlib.h>
|
2012-09-22 14:38:34 -04:00
|
|
|
#include <string.h>
|
2012-09-22 08:14:58 -04:00
|
|
|
|
2011-12-01 04:45:44 -05:00
|
|
|
#define PARANOIA 1
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2014-01-18 10:21:46 -05:00
|
|
|
#if defined(__is_sortix_kernel)
|
2013-06-20 08:46:10 -04:00
|
|
|
#include <sortix/kernel/decl.h>
|
|
|
|
#include <sortix/kernel/addralloc.h>
|
2012-08-01 08:27:02 -04:00
|
|
|
#include <sortix/kernel/kthread.h>
|
2013-06-20 08:46:10 -04:00
|
|
|
#include <sortix/kernel/log.h>
|
2012-03-21 19:52:29 -04:00
|
|
|
#include <sortix/kernel/memorymanagement.h>
|
|
|
|
#include <sortix/kernel/panic.h>
|
2012-09-23 07:55:58 -04:00
|
|
|
#endif
|
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
//
|
|
|
|
// This first section is just magic compiler/platform stuff, you should
|
|
|
|
// skip ahead to the actual algorithm.
|
|
|
|
//
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-23 07:55:58 -04:00
|
|
|
#if defined(__x86_64__)
|
2012-09-26 13:56:28 -04:00
|
|
|
const size_t MAGIC = 0xDEADDEADDEADDEADUL;
|
|
|
|
const size_t ALIGNMENT = 16UL;
|
2011-11-27 10:55:44 -05:00
|
|
|
#else
|
2012-09-26 13:56:28 -04:00
|
|
|
const size_t MAGIC = 0xDEADDEADUL;
|
|
|
|
const size_t ALIGNMENT = 8UL;
|
2011-11-27 10:55:44 -05:00
|
|
|
#endif
|
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
const size_t PAGESIZE = 4UL * 1024UL; // 4 KiB
|
|
|
|
const size_t NUMBINS = 8UL * sizeof(size_t);
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2013-06-20 08:46:10 -04:00
|
|
|
static uintptr_t wilderness;
|
2011-12-16 07:24:49 -05:00
|
|
|
|
2014-01-18 10:21:46 -05:00
|
|
|
#if defined(__is_sortix_kernel)
|
2013-06-20 08:46:10 -04:00
|
|
|
static uintptr_t GetHeapStart()
|
2012-09-26 13:56:28 -04:00
|
|
|
{
|
2013-10-26 10:56:32 -04:00
|
|
|
return Sortix::GetHeapLower();
|
2012-09-26 13:56:28 -04:00
|
|
|
}
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2013-06-20 08:46:10 -04:00
|
|
|
static void FreeMemory(uintptr_t where, size_t bytes)
|
2012-09-26 13:56:28 -04:00
|
|
|
{
|
|
|
|
assert(Sortix::Page::IsAligned(where + bytes));
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
while ( bytes )
|
|
|
|
{
|
|
|
|
addr_t page = Sortix::Memory::Unmap(where);
|
|
|
|
Sortix::Page::Put(page);
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
bytes -= PAGESIZE;
|
|
|
|
where += PAGESIZE;
|
|
|
|
}
|
|
|
|
}
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2013-06-20 08:46:10 -04:00
|
|
|
static bool AllocateMemory(uintptr_t where, size_t bytes)
|
2012-09-26 13:56:28 -04:00
|
|
|
{
|
|
|
|
assert(Sortix::Page::IsAligned(where + bytes));
|
2011-12-16 07:24:49 -05:00
|
|
|
|
2013-06-20 08:46:10 -04:00
|
|
|
uintptr_t pos = where;
|
2011-12-16 07:24:49 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
while ( bytes )
|
|
|
|
{
|
|
|
|
addr_t page = Sortix::Page::Get();
|
|
|
|
if ( !page )
|
2011-11-27 10:55:44 -05:00
|
|
|
{
|
2012-09-26 13:56:28 -04:00
|
|
|
FreeMemory(where, pos-where);
|
|
|
|
return false;
|
2011-11-27 10:55:44 -05:00
|
|
|
}
|
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
if ( !Sortix::Memory::Map(page, pos, PROT_KREAD | PROT_KWRITE) )
|
2011-12-16 07:24:49 -05:00
|
|
|
{
|
2012-09-26 13:56:28 -04:00
|
|
|
Sortix::Page::Put(page);
|
|
|
|
FreeMemory(where, pos-where);
|
|
|
|
return false;
|
2011-12-16 07:24:49 -05:00
|
|
|
}
|
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
bytes -= PAGESIZE;
|
|
|
|
pos += PAGESIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ExtendHeap(size_t bytesneeded)
|
|
|
|
{
|
2013-02-09 12:32:45 -05:00
|
|
|
size_t got_bytes = Sortix::ExpandHeap(bytesneeded);
|
|
|
|
if ( !got_bytes )
|
|
|
|
return false;
|
|
|
|
assert(bytesneeded <= got_bytes);
|
|
|
|
|
2013-10-26 10:56:32 -04:00
|
|
|
if ( !AllocateMemory(wilderness, got_bytes) )
|
2013-02-09 12:32:45 -05:00
|
|
|
{
|
|
|
|
Sortix::ShrinkHeap(got_bytes);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-09-26 13:56:28 -04:00
|
|
|
}
|
|
|
|
#else
|
2013-06-20 08:46:10 -04:00
|
|
|
static uintptr_t GetHeapStart()
|
2012-09-26 13:56:28 -04:00
|
|
|
{
|
2013-06-20 08:46:10 -04:00
|
|
|
uintptr_t base = (uintptr_t) sbrk(0);
|
|
|
|
uintptr_t unaligned = base % ALIGNMENT;
|
2012-09-26 13:56:28 -04:00
|
|
|
if ( unaligned )
|
|
|
|
{
|
|
|
|
sbrk(ALIGNMENT-unaligned);
|
|
|
|
}
|
2013-06-20 08:46:10 -04:00
|
|
|
uintptr_t result = (uintptr_t) sbrk(0);
|
2012-09-26 13:56:28 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ExtendHeap(size_t bytesneeded)
|
|
|
|
{
|
|
|
|
void* newheapend = sbrk(bytesneeded);
|
|
|
|
return newheapend != (void*) -1UL;
|
|
|
|
}
|
2011-11-27 10:55:44 -05:00
|
|
|
#endif
|
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// TODO: BitScanForward and BitScanReverse are x86 instructions, but
|
|
|
|
// directly using them messes with the optimizer. Once possible, use
|
|
|
|
// the inline assembly instead of the C-version of the functions.
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// Returns the index of the most significant set bit.
|
|
|
|
inline size_t BSR(size_t Value)
|
|
|
|
{
|
2011-11-27 10:55:44 -05:00
|
|
|
#if 1
|
2012-09-26 13:56:28 -04:00
|
|
|
assert(Value > 0);
|
|
|
|
for ( size_t I = 8*sizeof(size_t); I > 0; I-- )
|
|
|
|
{
|
|
|
|
if ( Value & ( 1UL << (I-1) ) ) { return I-1; }
|
|
|
|
}
|
|
|
|
return 0;
|
2011-11-27 10:55:44 -05:00
|
|
|
#else
|
2012-09-26 13:56:28 -04:00
|
|
|
size_t Result;
|
|
|
|
asm("bsr %0, %1" : "=r"(Result) : "r"(Value));
|
|
|
|
return Result;
|
2011-11-27 10:55:44 -05:00
|
|
|
#endif
|
2012-09-26 13:56:28 -04:00
|
|
|
}
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// Returns the index of the least significant set bit.
|
|
|
|
inline size_t BSF(size_t Value)
|
|
|
|
{
|
2011-11-27 10:55:44 -05:00
|
|
|
#if 1
|
2012-09-26 13:56:28 -04:00
|
|
|
assert(Value > 0);
|
|
|
|
for ( size_t I = 0; I < 8*sizeof(size_t); I++ )
|
|
|
|
{
|
|
|
|
if ( Value & ( 1UL << I ) ) { return I; }
|
|
|
|
}
|
|
|
|
return 0;
|
2011-11-27 10:55:44 -05:00
|
|
|
#else
|
2012-09-26 13:56:28 -04:00
|
|
|
size_t Result;
|
|
|
|
asm("bsf %0, %1" : "=r"(Result) : "r"(Value));
|
|
|
|
return Result;
|
2011-11-27 10:55:44 -05:00
|
|
|
#endif
|
2012-09-26 13:56:28 -04:00
|
|
|
}
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
//
|
|
|
|
// Now for some helper functions and structures.
|
|
|
|
//
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
struct Chunk;
|
|
|
|
struct Trailer;
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2014-01-18 10:21:46 -05:00
|
|
|
#if defined(__is_sortix_kernel)
|
2012-09-26 13:56:28 -04:00
|
|
|
Sortix::kthread_mutex_t heaplock;
|
2012-08-01 08:27:02 -04:00
|
|
|
#endif
|
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// The location where the heap originally grows from.
|
2013-06-20 08:46:10 -04:00
|
|
|
uintptr_t heapstart;
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// If heap grows down: Location of the first mapped page.
|
|
|
|
// If heap grows up: Location of the first not-mapped page.
|
2013-06-20 08:46:10 -04:00
|
|
|
#if 0 /* forward declared abvce */
|
|
|
|
static uintptr_t wilderness;
|
|
|
|
#endif
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// How many bytes remain in the wilderness.
|
|
|
|
size_t wildernesssize;
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// How many bytes are the heap allow to grow to (including wilderness).
|
|
|
|
size_t heapmaxsize;
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// How many bytes are currently used for chunks in the heap, which
|
|
|
|
// excludes the wilderness.
|
|
|
|
size_t heapsize;
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// bins[N] contain a linked list of chunks that are at least 2^(N+1)
|
|
|
|
// bytes, but less than 2^(N+2) bytes. By selecting the proper bin in
|
|
|
|
// constant time, we can allocate chunks in constant time.
|
|
|
|
Chunk* bins[NUMBINS];
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// Bit N is set if bin[N] contains a chunk.
|
|
|
|
size_t bincontainschunks;
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
static bool IsGoodHeapPointer(void* ptr, size_t size)
|
|
|
|
{
|
|
|
|
uintptr_t ptrlower = (uintptr_t) ptr;
|
|
|
|
uintptr_t ptrupper = ptrlower + size;
|
|
|
|
uintptr_t heaplower = heapstart;
|
|
|
|
uintptr_t heapupper = wilderness;
|
|
|
|
return heaplower <= ptrlower && ptrupper <= heapupper;
|
|
|
|
}
|
2012-09-07 15:46:43 -04:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// A preamble to every chunk providing meta-information.
|
|
|
|
struct Chunk
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
size_t size; // Includes size of Chunk and Trailer
|
|
|
|
union
|
|
|
|
{
|
|
|
|
size_t magic;
|
|
|
|
Chunk* nextunused;
|
|
|
|
};
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
public:
|
|
|
|
bool IsUsed() { return magic == MAGIC; }
|
|
|
|
Trailer* GetTrailer();
|
|
|
|
Chunk* LeftNeighbor();
|
|
|
|
Chunk* RightNeighbor();
|
|
|
|
bool IsSane();
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
};
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// A trailer to every chunk providing meta-information.
|
|
|
|
struct Trailer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
union
|
|
|
|
{
|
|
|
|
size_t magic;
|
|
|
|
Chunk* prevunused;
|
|
|
|
};
|
|
|
|
size_t size; // Includes size of Chunk and Trailer
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool IsUsed() { return magic == MAGIC; }
|
|
|
|
Chunk* GetChunk();
|
|
|
|
|
|
|
|
};
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
const size_t OVERHEAD = sizeof(Chunk) + sizeof(Trailer);
|
|
|
|
|
|
|
|
// This is how a real chunk actually looks:
|
|
|
|
//struct RealChunk
|
|
|
|
//{
|
|
|
|
// Chunk header;
|
|
|
|
// byte data[...];
|
|
|
|
// Trailer footer;
|
|
|
|
// };
|
|
|
|
|
|
|
|
Trailer* Chunk::GetTrailer()
|
|
|
|
{
|
2013-06-20 08:46:10 -04:00
|
|
|
return (Trailer*) (((uintptr_t) this) + size - sizeof(Trailer));
|
2012-09-26 13:56:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Chunk* Chunk::LeftNeighbor()
|
|
|
|
{
|
2013-06-20 08:46:10 -04:00
|
|
|
Trailer* trailer = (Trailer*) (((uintptr_t) this) - sizeof(Trailer));
|
2012-09-26 13:56:28 -04:00
|
|
|
return trailer->GetChunk();
|
|
|
|
}
|
|
|
|
|
|
|
|
Chunk* Chunk::RightNeighbor()
|
|
|
|
{
|
2013-06-20 08:46:10 -04:00
|
|
|
return (Chunk*) (((uintptr_t) this) + size);
|
2012-09-26 13:56:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Chunk* Trailer::GetChunk()
|
|
|
|
{
|
2013-06-20 08:46:10 -04:00
|
|
|
return (Chunk*) (((uintptr_t) this) + sizeof(Trailer) - size);
|
2012-09-26 13:56:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Chunk::IsSane()
|
|
|
|
{
|
|
|
|
if ( !IsGoodHeapPointer(this, sizeof(*this)) )
|
|
|
|
return false;
|
|
|
|
if ( !size ) { return false; }
|
|
|
|
size_t binindex = BSR(size);
|
|
|
|
Trailer* trailer = GetTrailer();
|
|
|
|
if ( !IsGoodHeapPointer(trailer, sizeof(*trailer)) )
|
|
|
|
return false;
|
|
|
|
if ( trailer->size != size ) { return false; }
|
|
|
|
if ( IsUsed() )
|
|
|
|
{
|
|
|
|
if ( bins[binindex] == this ) { return false; }
|
|
|
|
if ( magic != MAGIC || trailer->magic != magic ) { return false; }
|
|
|
|
}
|
|
|
|
if ( !IsUsed() )
|
|
|
|
{
|
2013-06-20 08:46:10 -04:00
|
|
|
if ( ((uintptr_t) nextunused) & (ALIGNMENT-1UL) ) { return false; }
|
|
|
|
if ( ((uintptr_t) trailer->prevunused) & (ALIGNMENT-1UL) ) { return false; }
|
2012-09-26 13:56:28 -04:00
|
|
|
if ( nextunused && !IsGoodHeapPointer(nextunused->GetTrailer(),
|
|
|
|
sizeof(Trailer)) )
|
|
|
|
return false;
|
|
|
|
if ( nextunused && nextunused->GetTrailer()->prevunused != this ) { return false; }
|
|
|
|
|
|
|
|
if ( trailer->prevunused )
|
2011-11-27 10:55:44 -05:00
|
|
|
{
|
2012-09-26 13:56:28 -04:00
|
|
|
if ( !IsGoodHeapPointer(trailer->prevunused,
|
|
|
|
sizeof(*trailer->prevunused)) )
|
2012-09-07 15:46:43 -04:00
|
|
|
return false;
|
2012-09-26 13:56:28 -04:00
|
|
|
if ( bins[binindex] == this ) { return false; }
|
|
|
|
if ( trailer->prevunused->nextunused != this ) { return false; }
|
2011-11-27 10:55:44 -05:00
|
|
|
}
|
2012-09-26 13:56:28 -04:00
|
|
|
if ( !trailer->prevunused )
|
2011-11-27 10:55:44 -05:00
|
|
|
{
|
2012-09-26 13:56:28 -04:00
|
|
|
if ( bins[binindex] != this ) { return false; }
|
|
|
|
if ( !(bincontainschunks & (1UL << binindex)) ) { return false; }
|
2011-11-27 10:55:44 -05:00
|
|
|
}
|
2012-09-26 13:56:28 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void InsertChunk(Chunk* chunk)
|
|
|
|
{
|
|
|
|
// Insert the chunk into the right bin.
|
|
|
|
size_t binindex = BSR(chunk->size);
|
|
|
|
chunk->GetTrailer()->prevunused = NULL;
|
|
|
|
chunk->nextunused = bins[binindex];
|
|
|
|
if ( chunk->nextunused )
|
|
|
|
{
|
|
|
|
assert(chunk->nextunused->IsSane());
|
|
|
|
chunk->nextunused->GetTrailer()->prevunused = chunk;
|
|
|
|
}
|
|
|
|
bins[binindex] = chunk;
|
|
|
|
bincontainschunks |= (1UL << binindex);
|
|
|
|
assert(chunk->IsSane());
|
|
|
|
}
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2013-06-26 10:57:19 -04:00
|
|
|
__attribute__((unused))
|
2012-09-26 13:56:28 -04:00
|
|
|
static bool ValidateHeap()
|
|
|
|
{
|
|
|
|
bool foundbin[NUMBINS];
|
|
|
|
for ( size_t i = 0; i < NUMBINS; i++ ) { foundbin[i] = false; }
|
|
|
|
|
|
|
|
Chunk* chunk = (Chunk*) heapstart;
|
2013-06-20 08:46:10 -04:00
|
|
|
while ( (uintptr_t) chunk < wilderness - wildernesssize )
|
2012-09-26 13:56:28 -04:00
|
|
|
{
|
|
|
|
size_t timesfound = 0;
|
|
|
|
for ( size_t i = 0; i < NUMBINS; i++ )
|
2011-11-27 10:55:44 -05:00
|
|
|
{
|
2012-09-26 13:56:28 -04:00
|
|
|
if ( chunk == bins[i] ) { foundbin[i] = true; timesfound++; }
|
2011-11-27 10:55:44 -05:00
|
|
|
}
|
2012-09-26 13:56:28 -04:00
|
|
|
if ( 1 < timesfound ) { return false; }
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
if ( !chunk->IsSane() ) { return false; }
|
|
|
|
chunk = chunk->RightNeighbor();
|
|
|
|
}
|
2011-12-04 14:59:42 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
for ( size_t i = 0; i < NUMBINS; i++ )
|
|
|
|
{
|
|
|
|
if ( !bins[i] )
|
2011-11-27 10:55:44 -05:00
|
|
|
{
|
2012-09-26 13:56:28 -04:00
|
|
|
if ( foundbin[i] ) { return false; }
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( !foundbin[i] ) { return false; }
|
|
|
|
if ( !bins[i]->IsSane() ) { return false; }
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// This is where the actual memory allocation algorithm starts.
|
|
|
|
//
|
|
|
|
|
|
|
|
extern "C" void _init_heap()
|
|
|
|
{
|
|
|
|
heapstart = GetHeapStart();
|
2013-02-09 12:32:45 -05:00
|
|
|
heapmaxsize = SIZE_MAX;
|
2012-09-26 13:56:28 -04:00
|
|
|
heapsize = 0;
|
|
|
|
wilderness = heapstart;
|
|
|
|
wildernesssize = 0;
|
|
|
|
for ( size_t i = 0; i < NUMBINS; i++ ) { bins[i] = NULL; }
|
|
|
|
bincontainschunks = 0;
|
2014-01-18 10:21:46 -05:00
|
|
|
#if defined(__is_sortix_kernel)
|
2012-09-26 13:56:28 -04:00
|
|
|
heaplock = Sortix::KTHREAD_MUTEX_INITIALIZER;
|
2012-08-01 08:27:02 -04:00
|
|
|
#endif
|
2012-09-26 13:56:28 -04:00
|
|
|
}
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// Attempts to expand the wilderness such that it contains at least
|
|
|
|
// bytesneeded bytes. This is done by mapping new pages onto into the
|
|
|
|
// virtual address-space.
|
|
|
|
static bool ExpandWilderness(size_t bytesneeded)
|
|
|
|
{
|
|
|
|
if ( bytesneeded <= wildernesssize ) { return true; }
|
2012-09-22 08:14:58 -04:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
bytesneeded -= wildernesssize;
|
|
|
|
|
|
|
|
// Align the increase on page boundaries.
|
|
|
|
const size_t PAGEMASK = ~(PAGESIZE - 1UL);
|
|
|
|
bytesneeded = ( bytesneeded + PAGESIZE - 1UL ) & PAGEMASK;
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
assert(bytesneeded >= PAGESIZE);
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// TODO: Overflow MAY happen here!
|
|
|
|
if ( heapmaxsize <= heapsize + wildernesssize + bytesneeded )
|
2013-02-09 12:32:45 -05:00
|
|
|
return errno = ENOMEM, true;
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2013-06-20 08:46:10 -04:00
|
|
|
uintptr_t newwilderness = wilderness + bytesneeded;
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// Attempt to map pages so our wilderness grows.
|
2013-02-09 12:32:45 -05:00
|
|
|
if ( !ExtendHeap(bytesneeded) )
|
|
|
|
return false;
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
wildernesssize += bytesneeded;
|
|
|
|
wilderness = newwilderness;
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
return true;
|
|
|
|
}
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
extern "C" void* malloc(size_t size)
|
|
|
|
{
|
2014-01-18 10:21:46 -05:00
|
|
|
#if defined(__is_sortix_kernel)
|
2012-09-26 13:56:28 -04:00
|
|
|
Sortix::ScopedLock scopedlock(&heaplock);
|
|
|
|
#endif
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
#if 2 <= PARANOIA
|
|
|
|
assert(ValidateHeap());
|
|
|
|
#endif
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// The size field keeps both the allocation and meta information.
|
|
|
|
size += OVERHEAD;
|
2011-11-27 10:55:44 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// Round up to nearest alignment.
|
|
|
|
size = (size + ALIGNMENT - 1UL) & (~(ALIGNMENT-1UL));
|
|
|
|
|
|
|
|
// Find the index of the smallest usable bin.
|
|
|
|
size_t minbinindex = BSR(size-1UL)+1UL;
|
|
|
|
|
|
|
|
// Make a bitmask that filter away all bins that are too small.
|
|
|
|
size_t minbinmask = ~((1UL << minbinindex) - 1UL);
|
|
|
|
|
|
|
|
// Figure out which bins are usable for our chunk.
|
|
|
|
size_t availablebins = bincontainschunks & minbinmask;
|
|
|
|
|
|
|
|
if ( availablebins )
|
|
|
|
{
|
|
|
|
// Find the smallest available bin.
|
|
|
|
size_t binindex = BSF(availablebins);
|
|
|
|
|
|
|
|
Chunk* chunk = bins[binindex];
|
|
|
|
assert(chunk->IsSane());
|
|
|
|
bins[binindex] = chunk->nextunused;
|
|
|
|
|
|
|
|
size_t binsize = 1UL << binindex;
|
|
|
|
|
|
|
|
// Mark the bin as empty if we emptied it.
|
|
|
|
if ( !bins[binindex] )
|
2011-11-27 10:55:44 -05:00
|
|
|
{
|
2012-09-26 13:56:28 -04:00
|
|
|
bincontainschunks ^= binsize;
|
2011-11-27 10:55:44 -05:00
|
|
|
}
|
2012-09-26 13:56:28 -04:00
|
|
|
else
|
2011-11-27 10:55:44 -05:00
|
|
|
{
|
2012-09-26 13:56:28 -04:00
|
|
|
Trailer* trailer = bins[binindex]->GetTrailer();
|
|
|
|
trailer->prevunused = NULL;
|
2011-11-27 10:55:44 -05:00
|
|
|
}
|
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
assert(!bins[binindex] || bins[binindex]->IsSane());
|
|
|
|
|
|
|
|
// If we don't use the entire chunk.
|
2014-05-15 09:03:54 -04:00
|
|
|
size_t original_chunk_size = chunk->size;
|
|
|
|
if ( OVERHEAD <= original_chunk_size - size )
|
2011-11-27 10:55:44 -05:00
|
|
|
{
|
2014-05-15 09:03:54 -04:00
|
|
|
size_t left = original_chunk_size - size;
|
2012-09-26 13:56:28 -04:00
|
|
|
chunk->size -= left;
|
|
|
|
chunk->GetTrailer()->size = chunk->size;
|
|
|
|
|
|
|
|
Chunk* leftchunk = chunk->RightNeighbor();
|
|
|
|
leftchunk->size = left;
|
|
|
|
Trailer* lefttrailer = leftchunk->GetTrailer();
|
|
|
|
lefttrailer->size = left;
|
|
|
|
|
|
|
|
InsertChunk(leftchunk);
|
2011-11-27 10:55:44 -05:00
|
|
|
}
|
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
chunk->magic = MAGIC;
|
|
|
|
chunk->GetTrailer()->magic = MAGIC;
|
|
|
|
|
2012-12-07 07:11:26 -05:00
|
|
|
#if 3 <= PARANOIA
|
2012-09-26 13:56:28 -04:00
|
|
|
assert(ValidateHeap());
|
|
|
|
#endif
|
|
|
|
|
2013-06-20 08:46:10 -04:00
|
|
|
uintptr_t result = ((uintptr_t) chunk) + sizeof(Chunk);
|
2012-09-26 13:56:28 -04:00
|
|
|
return (void*) result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no bins are available, try to allocate from the wilderness.
|
|
|
|
|
|
|
|
// Check if the wilderness can meet our requirements.
|
|
|
|
if ( wildernesssize < size && !ExpandWilderness(size) )
|
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Carve a new chunk out of the wilderness and initialize it.
|
|
|
|
Chunk* chunk = (Chunk*) (wilderness - wildernesssize);
|
|
|
|
assert(size <= wildernesssize);
|
|
|
|
wildernesssize -= size;
|
|
|
|
heapsize += size;
|
|
|
|
assert(IsGoodHeapPointer(chunk, sizeof(*chunk)));
|
|
|
|
chunk->size = size;
|
|
|
|
Trailer* trailer = chunk->GetTrailer();
|
|
|
|
assert(IsGoodHeapPointer(trailer, sizeof(*trailer)));
|
|
|
|
trailer->size = size;
|
|
|
|
chunk->magic = MAGIC;
|
|
|
|
trailer->magic = MAGIC;
|
|
|
|
|
2012-12-07 07:11:26 -05:00
|
|
|
#if 3 <= PARANOIA
|
2012-09-26 13:56:28 -04:00
|
|
|
assert(ValidateHeap());
|
|
|
|
#endif
|
|
|
|
|
2013-06-20 08:46:10 -04:00
|
|
|
uintptr_t result = ((uintptr_t) chunk) + sizeof(Chunk);
|
2012-09-26 13:56:28 -04:00
|
|
|
return (void*) result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsLeftmostChunk(Chunk* chunk)
|
|
|
|
{
|
2013-06-20 08:46:10 -04:00
|
|
|
return heapstart <= (uintptr_t) chunk;
|
2012-09-26 13:56:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsRightmostChunk(Chunk* chunk)
|
|
|
|
{
|
2013-06-20 08:46:10 -04:00
|
|
|
return heapstart + heapsize <= (uintptr_t) chunk + chunk->size;
|
2012-09-26 13:56:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Removes a chunk from its bin.
|
|
|
|
static void UnlinkChunk(Chunk* chunk)
|
|
|
|
{
|
|
|
|
assert(chunk->IsSane());
|
|
|
|
Trailer* trailer = chunk->GetTrailer();
|
|
|
|
if ( trailer->prevunused )
|
|
|
|
{
|
|
|
|
assert(trailer->prevunused->IsSane());
|
|
|
|
trailer->prevunused->nextunused = chunk->nextunused;
|
|
|
|
if ( chunk->nextunused )
|
2011-11-27 10:55:44 -05:00
|
|
|
{
|
2012-09-26 13:56:28 -04:00
|
|
|
assert(chunk->nextunused->IsSane());
|
|
|
|
chunk->nextunused->GetTrailer()->prevunused = trailer->prevunused;
|
2011-11-27 10:55:44 -05:00
|
|
|
}
|
2012-09-26 13:56:28 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( chunk->nextunused )
|
2011-11-27 10:55:44 -05:00
|
|
|
{
|
2012-09-26 13:56:28 -04:00
|
|
|
assert(chunk->nextunused->IsSane());
|
|
|
|
chunk->nextunused->GetTrailer()->prevunused = NULL;
|
2011-11-27 10:55:44 -05:00
|
|
|
}
|
2012-09-26 13:56:28 -04:00
|
|
|
size_t binindex = BSR(chunk->size);
|
|
|
|
assert(bins[binindex] == chunk);
|
|
|
|
bins[binindex] = chunk->nextunused;
|
|
|
|
if ( !bins[binindex] ) { bincontainschunks ^= 1UL << binindex; }
|
|
|
|
else { assert(bins[binindex]->IsSane()); }
|
|
|
|
}
|
|
|
|
}
|
2011-12-23 22:05:38 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
// Transforms a chunk and its neighbors into a single chunk if possible.
|
|
|
|
static void UnifyNeighbors(Chunk** chunk)
|
|
|
|
{
|
|
|
|
if ( !IsLeftmostChunk(*chunk) )
|
|
|
|
{
|
|
|
|
Chunk* neighbor = (*chunk)->LeftNeighbor();
|
|
|
|
if ( !neighbor->IsUsed() )
|
2011-12-23 22:05:38 -05:00
|
|
|
{
|
2012-09-26 13:56:28 -04:00
|
|
|
size_t size = neighbor->size;
|
|
|
|
size_t chunksize = (*chunk)->size;
|
|
|
|
UnlinkChunk(neighbor);
|
|
|
|
*chunk = neighbor;
|
|
|
|
(*chunk)->size = size + chunksize;
|
|
|
|
(*chunk)->GetTrailer()->size = (*chunk)->size;
|
2011-12-23 22:05:38 -05:00
|
|
|
}
|
2012-09-26 13:56:28 -04:00
|
|
|
}
|
2012-02-12 18:31:05 -05:00
|
|
|
|
2012-09-26 13:56:28 -04:00
|
|
|
if ( !IsRightmostChunk(*chunk) )
|
|
|
|
{
|
|
|
|
Chunk* neighbor = (*chunk)->RightNeighbor();
|
|
|
|
if ( !neighbor->IsUsed() )
|
2012-02-12 18:31:05 -05:00
|
|
|
{
|
2012-09-26 13:56:28 -04:00
|
|
|
UnlinkChunk(neighbor);
|
|
|
|
(*chunk)->size += neighbor->size;
|
|
|
|
(*chunk)->GetTrailer()->size = (*chunk)->size;
|
2012-02-12 18:31:05 -05:00
|
|
|
}
|
2011-11-27 10:55:44 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-26 13:56:28 -04:00
|
|
|
|
|
|
|
extern "C" void free(void* addr)
|
|
|
|
{
|
2014-01-18 10:21:46 -05:00
|
|
|
#if defined(__is_sortix_kernel)
|
2012-09-26 13:56:28 -04:00
|
|
|
Sortix::ScopedLock scopedlock(&heaplock);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 2 <= PARANOIA
|
|
|
|
assert(ValidateHeap());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ( !addr) { return; }
|
2013-06-20 08:46:10 -04:00
|
|
|
Chunk* chunk = (Chunk*) ((uintptr_t) addr - sizeof(Chunk));
|
2014-01-18 10:21:46 -05:00
|
|
|
#if __STDC_HOSTED__
|
2012-12-07 07:11:26 -05:00
|
|
|
if ( !IsGoodHeapPointer(addr, 1) ||
|
|
|
|
!IsGoodHeapPointer(chunk, sizeof(*chunk)) )
|
|
|
|
{
|
|
|
|
error(0, 0, "attempted to free(3) non-heap pointer: 0x%zx", addr);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
if ( !chunk->IsUsed() )
|
|
|
|
{
|
|
|
|
error(0, 0, "attempted to free(3) area that doesn't appear to be "
|
|
|
|
"allocated: 0x%zx + 0x%zx", addr);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
#endif
|
2012-09-26 13:56:28 -04:00
|
|
|
assert(chunk->IsUsed());
|
|
|
|
assert(chunk->IsSane());
|
|
|
|
|
|
|
|
UnifyNeighbors(&chunk);
|
|
|
|
|
|
|
|
bool nexttowilderness = IsRightmostChunk(chunk);
|
|
|
|
|
|
|
|
// If possible, let the wilderness regain the memory.
|
|
|
|
if ( nexttowilderness )
|
|
|
|
{
|
|
|
|
heapsize -= chunk->size;
|
|
|
|
wildernesssize += chunk->size;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
InsertChunk(chunk);
|
|
|
|
|
2012-12-07 07:11:26 -05:00
|
|
|
#if 3 <= PARANOIA
|
2012-09-26 13:56:28 -04:00
|
|
|
assert(ValidateHeap());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Implement this function properly.
|
|
|
|
extern "C" void* realloc(void* ptr, size_t size)
|
|
|
|
{
|
|
|
|
if ( !ptr ) { return malloc(size); }
|
2013-06-20 08:46:10 -04:00
|
|
|
Chunk* chunk = (Chunk*) ((uintptr_t) ptr - sizeof(Chunk));
|
2012-09-26 13:56:28 -04:00
|
|
|
assert(chunk->IsUsed());
|
|
|
|
assert(chunk->IsSane());
|
|
|
|
size_t allocsize = chunk->size - OVERHEAD;
|
|
|
|
if ( size < allocsize ) { return ptr; }
|
|
|
|
void* newptr = malloc(size);
|
|
|
|
if ( !newptr ) { return NULL; }
|
|
|
|
memcpy(newptr, ptr, allocsize);
|
|
|
|
free(ptr);
|
|
|
|
return newptr;
|
|
|
|
}
|