2002-07-09 16:24:59 +04:00
|
|
|
/*
|
2004-10-18 19:29:17 +04:00
|
|
|
** Copyright 2002-2004, The Haiku Team. All rights reserved.
|
|
|
|
** Distributed under the terms of the Haiku License.
|
|
|
|
**
|
2002-07-09 16:24:59 +04:00
|
|
|
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
|
|
|
|
** Distributed under the terms of the NewOS License.
|
|
|
|
*/
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2002-10-26 20:13:36 +04:00
|
|
|
#include <KernelExport.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <kernel.h>
|
|
|
|
#include <vm.h>
|
|
|
|
#include <lock.h>
|
2002-11-28 05:25:04 +03:00
|
|
|
#include <int.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <memheap.h>
|
2002-10-30 02:07:06 +03:00
|
|
|
#include <malloc.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <debug.h>
|
|
|
|
#include <arch/cpu.h>
|
2003-10-08 03:12:37 +04:00
|
|
|
#include <boot/kernel_args.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2002-09-29 05:58:22 +04:00
|
|
|
#define TRACE_HEAP 0
|
|
|
|
#if TRACE_HEAP
|
|
|
|
# define TRACE(x) dprintf x
|
|
|
|
#else
|
|
|
|
# define TRACE(x) ;
|
|
|
|
#endif
|
|
|
|
|
2002-09-11 16:44:48 +04:00
|
|
|
/* prevent freeing pointers that were not allocated by kmalloc or are already freeed */
|
|
|
|
#define PARANOID_POINTER_CHECK 1
|
|
|
|
/* initialize newly allocated memory with something non zero */
|
|
|
|
#define PARANOID_KMALLOC 1
|
2003-08-18 07:35:57 +04:00
|
|
|
/* check if freed pointers are already freed, and fill freed memory with 0xdeadbeef */
|
2002-09-29 05:58:22 +04:00
|
|
|
#define PARANOID_KFREE 1
|
|
|
|
/* use a back and front wall around each allocation */
|
2004-10-20 14:53:03 +04:00
|
|
|
#define USE_WALL 0
|
2004-10-18 19:29:17 +04:00
|
|
|
#define USE_CHECKING_WALL 0
|
|
|
|
|
|
|
|
#define WALL_SIZE 8
|
|
|
|
/* must be a multiple of 4 */
|
|
|
|
|
|
|
|
#if USE_CHECKING_WALL
|
|
|
|
/* Change to allow (struct list_link) + 2 * uint32 + WALL_SIZE */
|
|
|
|
# define WALL_MIN_ALIGN 32
|
|
|
|
# define WALL_CHECK_FREQUENCY 1 /* every tenth second */
|
|
|
|
#else
|
|
|
|
# define WALL_MIN_ALIGN 16
|
|
|
|
#endif
|
2002-09-29 05:58:22 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// heap stuff
|
|
|
|
// ripped mostly from nujeffos
|
|
|
|
|
|
|
|
struct heap_page {
|
|
|
|
unsigned short bin_index : 5;
|
|
|
|
unsigned short free_count : 9;
|
|
|
|
unsigned short cleaning : 1;
|
|
|
|
unsigned short in_use : 1;
|
|
|
|
} PACKED;
|
|
|
|
|
|
|
|
static struct heap_page *heap_alloc_table;
|
2004-10-18 19:29:17 +04:00
|
|
|
static addr_t heap_base_ptr;
|
|
|
|
static addr_t heap_base;
|
|
|
|
static addr_t heap_size;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
struct heap_bin {
|
|
|
|
unsigned int element_size;
|
|
|
|
unsigned int grow_size;
|
|
|
|
unsigned int alloc_count;
|
|
|
|
void *free_list;
|
|
|
|
unsigned int free_count;
|
|
|
|
char *raw_list;
|
|
|
|
unsigned int raw_count;
|
|
|
|
};
|
2002-10-30 02:07:06 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
static struct heap_bin bins[] = {
|
2004-10-18 19:29:17 +04:00
|
|
|
{16, B_PAGE_SIZE, 0, 0, 0, 0, 0},
|
|
|
|
{32, B_PAGE_SIZE, 0, 0, 0, 0, 0},
|
|
|
|
{64, B_PAGE_SIZE, 0, 0, 0, 0, 0},
|
|
|
|
{128, B_PAGE_SIZE, 0, 0, 0, 0, 0},
|
|
|
|
{256, B_PAGE_SIZE, 0, 0, 0, 0, 0},
|
|
|
|
{512, B_PAGE_SIZE, 0, 0, 0, 0, 0},
|
|
|
|
{1024, B_PAGE_SIZE, 0, 0, 0, 0, 0},
|
|
|
|
{2048, B_PAGE_SIZE, 0, 0, 0, 0, 0},
|
2002-07-09 16:24:59 +04:00
|
|
|
{0x1000, 0x1000, 0, 0, 0, 0, 0},
|
|
|
|
{0x2000, 0x2000, 0, 0, 0, 0, 0},
|
|
|
|
{0x3000, 0x3000, 0, 0, 0, 0, 0},
|
|
|
|
{0x4000, 0x4000, 0, 0, 0, 0, 0},
|
|
|
|
{0x5000, 0x5000, 0, 0, 0, 0, 0},
|
|
|
|
{0x6000, 0x6000, 0, 0, 0, 0, 0},
|
|
|
|
{0x7000, 0x7000, 0, 0, 0, 0, 0},
|
|
|
|
{0x8000, 0x8000, 0, 0, 0, 0, 0},
|
|
|
|
{0x9000, 0x9000, 0, 0, 0, 0, 0},
|
|
|
|
{0xa000, 0xa000, 0, 0, 0, 0, 0},
|
|
|
|
{0xb000, 0xb000, 0, 0, 0, 0, 0},
|
|
|
|
{0xc000, 0xc000, 0, 0, 0, 0, 0},
|
|
|
|
{0xd000, 0xd000, 0, 0, 0, 0, 0},
|
|
|
|
{0xe000, 0xe000, 0, 0, 0, 0, 0},
|
|
|
|
{0xf000, 0xf000, 0, 0, 0, 0, 0},
|
|
|
|
{0x10000, 0x10000, 0, 0, 0, 0, 0} // 64k
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int bin_count = sizeof(bins) / sizeof(struct heap_bin);
|
|
|
|
static mutex heap_lock;
|
|
|
|
|
2002-09-11 16:44:48 +04:00
|
|
|
#if PARANOID_POINTER_CHECK
|
|
|
|
|
|
|
|
#define PTRCHECKLIST_ENTRIES 8192
|
|
|
|
static void *ptrchecklist[PTRCHECKLIST_ENTRIES]; /* automatically initialized to zero */
|
|
|
|
|
|
|
|
static void
|
|
|
|
ptrchecklist_store(void *ptr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
mutex_lock(&heap_lock);
|
|
|
|
for (i = 0; i != PTRCHECKLIST_ENTRIES; i++)
|
|
|
|
if (ptrchecklist[i] == NULL) {
|
|
|
|
ptrchecklist[i] = ptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mutex_unlock(&heap_lock);
|
|
|
|
if (i == PTRCHECKLIST_ENTRIES)
|
|
|
|
panic("Sorry, out of entries, increase PTRCHECKLIST_ENTRIES in heap.c\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
ptrchecklist_remove(void *ptr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
bool found = false;
|
|
|
|
mutex_lock(&heap_lock);
|
|
|
|
for (i = 0; i != PTRCHECKLIST_ENTRIES; i++)
|
|
|
|
if (ptrchecklist[i] == ptr) {
|
|
|
|
ptrchecklist[i] = NULL;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mutex_unlock(&heap_lock);
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* PARANOID_POINTER_CHECK */
|
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
#if USE_WALL
|
|
|
|
|
|
|
|
static void
|
|
|
|
check_wall(void *address)
|
|
|
|
{
|
|
|
|
uint32 *wall = (uint32 *)((uint8 *)address - WALL_SIZE - 8);
|
|
|
|
uint32 size = wall[1];
|
|
|
|
|
|
|
|
if (wall[2] != 0xabadcafe || wall[3] != 0xabadcafe)
|
|
|
|
panic("free: front wall was overwritten (allocation at %p, %lu bytes): %08lx %08lx\n", address, size, wall[1], wall[2]);
|
|
|
|
|
|
|
|
wall = (uint32 *)((uint8 *)address + size);
|
|
|
|
if (wall[0] != 0xabadcafe || wall[1] != 0xabadcafe)
|
|
|
|
panic("free: back wall was overwritten (allocation at %p, %lu bytes): %08lx %08lx\n", address, size, wall[0], wall[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* USE_WALL */
|
|
|
|
|
|
|
|
#if USE_CHECKING_WALL
|
|
|
|
|
|
|
|
sem_id sWallCheckLock = -1;
|
|
|
|
struct list sWalls;
|
|
|
|
|
|
|
|
static void
|
|
|
|
check_wall_daemon(void *arg, int iteration)
|
|
|
|
{
|
|
|
|
struct list_link *link = NULL;
|
|
|
|
uint32 *wall;
|
|
|
|
|
|
|
|
acquire_sem(sWallCheckLock);
|
|
|
|
|
|
|
|
while ((link = list_get_next_item(&sWalls, link)) != NULL) {
|
2004-10-23 17:59:27 +04:00
|
|
|
wall = (uint32 *)((addr_t)link + sizeof(struct list_link) + WALL_SIZE + 8);
|
2004-10-18 19:29:17 +04:00
|
|
|
check_wall(wall);
|
|
|
|
}
|
|
|
|
|
|
|
|
release_sem(sWallCheckLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* USE_CHECKING_WALL */
|
2002-09-11 16:44:48 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
static void dump_bin(int bin_index)
|
|
|
|
{
|
|
|
|
struct heap_bin *bin = &bins[bin_index];
|
|
|
|
unsigned int *temp;
|
|
|
|
|
|
|
|
dprintf("%d:\tesize %d\tgrow_size %d\talloc_count %d\tfree_count %d\traw_count %d\traw_list %p\n",
|
|
|
|
bin_index, bin->element_size, bin->grow_size, bin->alloc_count, bin->free_count, bin->raw_count, bin->raw_list);
|
|
|
|
dprintf("free_list: ");
|
|
|
|
for(temp = bin->free_list; temp != NULL; temp = (unsigned int *)*temp) {
|
|
|
|
dprintf("%p ", temp);
|
|
|
|
}
|
|
|
|
dprintf("NULL\n");
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
dump_bin_list(int argc, char **argv)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
dprintf("%d heap bins at %p:\n", bin_count, bins);
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
for (i = 0; i<bin_count; i++) {
|
2002-07-09 16:24:59 +04:00
|
|
|
dump_bin(i);
|
|
|
|
}
|
2002-07-18 02:07:37 +04:00
|
|
|
return 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
/* called from vm_init. The heap should already be mapped in at this point, we just
|
|
|
|
* do a little housekeeping to set up the data structure.
|
|
|
|
*/
|
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
status_t
|
|
|
|
heap_init(addr_t heapBase)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-10-18 19:29:17 +04:00
|
|
|
const unsigned int page_entries = B_PAGE_SIZE / sizeof(struct heap_page);
|
2002-07-09 16:24:59 +04:00
|
|
|
// set some global pointers
|
2004-10-18 19:29:17 +04:00
|
|
|
heap_alloc_table = (struct heap_page *)heapBase;
|
|
|
|
heap_size = ((uint64)HEAP_SIZE * page_entries / (page_entries + 1)) & ~(B_PAGE_SIZE-1);
|
2002-07-25 18:49:29 +04:00
|
|
|
heap_base = (unsigned int)heap_alloc_table + PAGE_ALIGN(heap_size / page_entries);
|
2002-07-09 16:24:59 +04:00
|
|
|
heap_base_ptr = heap_base;
|
2004-10-18 19:29:17 +04:00
|
|
|
TRACE(("heap_alloc_table = %p, heap_base = 0x%lx, heap_size = 0x%lx\n",
|
|
|
|
heap_alloc_table, heap_base, heap_size));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// zero out the heap alloc table at the base of the heap
|
2004-10-18 19:29:17 +04:00
|
|
|
memset((void *)heap_alloc_table, 0, (heap_size / B_PAGE_SIZE) * sizeof(struct heap_page));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
// pre-init the mutex to at least fall through any semaphore calls
|
|
|
|
heap_lock.sem = -1;
|
2002-07-18 23:21:40 +04:00
|
|
|
heap_lock.holder = -1;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-10-23 17:59:27 +04:00
|
|
|
#if USE_CHECKING_WALL
|
|
|
|
list_init(&sWalls);
|
|
|
|
#endif
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// set up some debug commands
|
2002-07-18 02:07:37 +04:00
|
|
|
add_debugger_command("heap_bindump", &dump_bin_list, "dump stats about bin usage");
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
status_t
|
2004-10-23 17:59:27 +04:00
|
|
|
heap_init_post_sem(kernel_args *ka)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-07-18 23:21:40 +04:00
|
|
|
if (mutex_init(&heap_lock, "heap_mutex") < 0)
|
2002-07-09 16:24:59 +04:00
|
|
|
panic("error creating heap mutex\n");
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
#if USE_CHECKING_WALL
|
|
|
|
sWallCheckLock = create_sem(1, "check wall");
|
2004-10-23 17:59:27 +04:00
|
|
|
#endif
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
heap_init_post_thread(kernel_args *ka)
|
|
|
|
{
|
|
|
|
#if USE_CHECKING_WALL
|
2004-10-18 19:29:17 +04:00
|
|
|
register_kernel_daemon(check_wall_daemon, NULL, WALL_CHECK_FREQUENCY);
|
|
|
|
#endif
|
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
|
|
|
static char *
|
|
|
|
raw_alloc(unsigned int size, int bin_index)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
unsigned int new_heap_ptr;
|
|
|
|
char *retval;
|
|
|
|
struct heap_page *page;
|
2004-10-18 19:29:17 +04:00
|
|
|
addr_t addr;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
new_heap_ptr = heap_base_ptr + PAGE_ALIGN(size);
|
2002-07-18 23:21:40 +04:00
|
|
|
if (new_heap_ptr > heap_base + heap_size)
|
2002-07-09 16:24:59 +04:00
|
|
|
panic("heap overgrew itself!\n");
|
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
for (addr = heap_base_ptr; addr < new_heap_ptr; addr += B_PAGE_SIZE) {
|
|
|
|
page = &heap_alloc_table[(addr - heap_base) / B_PAGE_SIZE];
|
2002-07-09 16:24:59 +04:00
|
|
|
page->in_use = 1;
|
|
|
|
page->cleaning = 0;
|
|
|
|
page->bin_index = bin_index;
|
2004-10-18 19:29:17 +04:00
|
|
|
if (bin_index < bin_count && bins[bin_index].element_size < B_PAGE_SIZE)
|
|
|
|
page->free_count = B_PAGE_SIZE / bins[bin_index].element_size;
|
2002-07-09 16:24:59 +04:00
|
|
|
else
|
|
|
|
page->free_count = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = (char *)heap_base_ptr;
|
|
|
|
heap_base_ptr = new_heap_ptr;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
#if DEBUG
|
|
|
|
static bool
|
|
|
|
is_valid_alignment(size_t number)
|
|
|
|
{
|
|
|
|
// this cryptic line accepts zero and all powers of two
|
|
|
|
return ((~number + 1) | ((number << 1) - 1)) == ~0UL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2002-10-30 02:07:06 +03:00
|
|
|
// #pragma mark -
|
|
|
|
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
void *
|
2004-10-18 19:29:17 +04:00
|
|
|
memalign(size_t alignment, size_t size)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
void *address = NULL;
|
|
|
|
int bin_index;
|
|
|
|
unsigned int i;
|
|
|
|
struct heap_page *page;
|
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
TRACE(("memalign(alignment = %lu, size = %lu\n", alignment, size));
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
if (!is_valid_alignment(alignment))
|
|
|
|
panic("memalign() with an alignment which is not a power of 2\n");
|
|
|
|
#endif
|
|
|
|
|
2002-11-28 05:25:04 +03:00
|
|
|
if (!kernel_startup && !are_interrupts_enabled())
|
|
|
|
panic("malloc: called with interrupts disabled\n");
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
mutex_lock(&heap_lock);
|
|
|
|
|
2002-09-29 05:58:22 +04:00
|
|
|
#if USE_WALL
|
|
|
|
// The wall uses 4 bytes to store the actual length of the requested
|
2004-10-18 19:29:17 +04:00
|
|
|
// block, 4 bytes for the alignment offset, WALL_SIZE, and eventually
|
|
|
|
// a list_link in case USE_CHECKING_WALL is defined
|
|
|
|
if (alignment < WALL_MIN_ALIGN)
|
|
|
|
alignment = WALL_MIN_ALIGN;
|
|
|
|
|
|
|
|
#if USE_CHECKING_WALL
|
|
|
|
size += sizeof(struct list_link);
|
|
|
|
#endif
|
|
|
|
size += 2*WALL_SIZE + 8 + 2*alignment;
|
|
|
|
#else
|
|
|
|
// ToDo: that code "aligns" the buffer because the bins are always
|
|
|
|
// aligned on their bin size
|
|
|
|
if (size < alignment)
|
|
|
|
size = alignment;
|
2002-09-29 05:58:22 +04:00
|
|
|
#endif
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
for (bin_index = 0; bin_index < bin_count; bin_index++)
|
|
|
|
if (size <= bins[bin_index].element_size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (bin_index == bin_count) {
|
|
|
|
// XXX fix the raw alloc later.
|
|
|
|
//address = raw_alloc(size, bin_index);
|
2004-10-18 19:29:17 +04:00
|
|
|
panic("malloc: asked to allocate too much for now (%lu bytes)!\n", size);
|
2002-07-09 16:24:59 +04:00
|
|
|
goto out;
|
|
|
|
} else {
|
|
|
|
if (bins[bin_index].free_list != NULL) {
|
|
|
|
address = bins[bin_index].free_list;
|
|
|
|
bins[bin_index].free_list = (void *)(*(unsigned int *)bins[bin_index].free_list);
|
|
|
|
bins[bin_index].free_count--;
|
|
|
|
} else {
|
|
|
|
if (bins[bin_index].raw_count == 0) {
|
|
|
|
bins[bin_index].raw_list = raw_alloc(bins[bin_index].grow_size, bin_index);
|
|
|
|
bins[bin_index].raw_count = bins[bin_index].grow_size / bins[bin_index].element_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bins[bin_index].raw_count--;
|
|
|
|
address = bins[bin_index].raw_list;
|
|
|
|
bins[bin_index].raw_list += bins[bin_index].element_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bins[bin_index].alloc_count++;
|
2004-10-18 19:29:17 +04:00
|
|
|
page = &heap_alloc_table[((unsigned int)address - heap_base) / B_PAGE_SIZE];
|
2002-07-09 16:24:59 +04:00
|
|
|
page[0].free_count--;
|
2002-09-29 05:58:22 +04:00
|
|
|
|
|
|
|
TRACE(("kmalloc0: page %p: bin_index %d, free_count %d\n", page, page->bin_index, page->free_count));
|
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
for(i = 1; i < bins[bin_index].element_size / B_PAGE_SIZE; i++) {
|
2002-07-09 16:24:59 +04:00
|
|
|
page[i].free_count--;
|
2002-09-29 05:58:22 +04:00
|
|
|
TRACE(("kmalloc1: page 0x%x: bin_index %d, free_count %d\n", page[i], page[i].bin_index, page[i].free_count));
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&heap_lock);
|
|
|
|
|
2002-09-29 05:58:22 +04:00
|
|
|
TRACE(("kmalloc: asked to allocate size %d, returning ptr = %p\n", size, address));
|
2002-09-11 16:44:48 +04:00
|
|
|
|
|
|
|
#if PARANOID_KMALLOC
|
2004-10-18 19:29:17 +04:00
|
|
|
memset(address, 0xcc, size);
|
2002-09-29 06:24:42 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if PARANOID_POINTER_CHECK
|
|
|
|
ptrchecklist_store(address);
|
2002-09-11 16:44:48 +04:00
|
|
|
#endif
|
|
|
|
|
2002-09-29 05:58:22 +04:00
|
|
|
#if USE_WALL
|
|
|
|
{
|
2004-10-18 19:29:17 +04:00
|
|
|
uint32 *wall = (uint32 *)((addr_t)address + alignment - WALL_SIZE - 8);
|
2004-10-23 17:59:27 +04:00
|
|
|
#if USE_CHECKING_WALL
|
|
|
|
struct list_link *link = (struct list_link *)wall - 1;
|
|
|
|
|
|
|
|
acquire_sem(sWallCheckLock);
|
2004-10-18 19:29:17 +04:00
|
|
|
list_add_link_to_tail(&sWalls, link);
|
2004-10-23 17:59:27 +04:00
|
|
|
release_sem(sWallCheckLock);
|
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
size -= sizeof(struct list_link);
|
|
|
|
#endif
|
|
|
|
size -= 8 + 2*WALL_SIZE + 2*alignment;
|
2002-09-29 06:21:22 +04:00
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
wall[0] = alignment;
|
|
|
|
wall[1] = size;
|
2002-09-29 05:58:22 +04:00
|
|
|
wall[2] = 0xabadcafe;
|
2004-10-18 19:29:17 +04:00
|
|
|
wall[3] = 0xabadcafe;
|
2002-09-29 05:58:22 +04:00
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
address = (uint8 *)wall + 16;
|
2002-09-29 05:58:22 +04:00
|
|
|
|
|
|
|
wall = (uint32 *)((uint8 *)address + size);
|
|
|
|
wall[0] = 0xabadcafe;
|
|
|
|
wall[1] = 0xabadcafe;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
void *
|
|
|
|
malloc(size_t size)
|
|
|
|
{
|
|
|
|
return memalign(0, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
void
|
2002-10-30 02:07:06 +03:00
|
|
|
free(void *address)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
|
|
|
struct heap_page *page;
|
|
|
|
struct heap_bin *bin;
|
|
|
|
unsigned int i;
|
|
|
|
|
2002-11-28 05:25:04 +03:00
|
|
|
if (!kernel_startup && !are_interrupts_enabled())
|
|
|
|
panic("free: called with interrupts disabled\n");
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
if (address == NULL)
|
|
|
|
return;
|
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
if ((addr_t)address < heap_base || (addr_t)address >= (heap_base + heap_size))
|
2002-11-28 05:25:04 +03:00
|
|
|
panic("free: asked to free invalid address %p\n", address);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-29 05:58:22 +04:00
|
|
|
#if USE_WALL
|
|
|
|
{
|
2004-10-18 19:29:17 +04:00
|
|
|
uint32 *wall = (uint32 *)((uint8 *)address - WALL_SIZE - 8);
|
|
|
|
uint32 alignOffset = wall[0];
|
2002-09-29 05:58:22 +04:00
|
|
|
|
2004-10-23 17:59:27 +04:00
|
|
|
#if USE_CHECKING_WALL
|
|
|
|
struct list_link *link = (struct list_link *)wall - 1;
|
2002-09-29 05:58:22 +04:00
|
|
|
|
2004-10-23 17:59:27 +04:00
|
|
|
acquire_sem(sWallCheckLock);
|
2004-10-18 19:29:17 +04:00
|
|
|
list_remove_link(link);
|
2004-10-23 17:59:27 +04:00
|
|
|
release_sem(sWallCheckLock);
|
2004-10-18 19:29:17 +04:00
|
|
|
#endif
|
|
|
|
check_wall(address);
|
|
|
|
address = (uint8 *)address - alignOffset;
|
2002-09-29 05:58:22 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-09-11 16:44:48 +04:00
|
|
|
#if PARANOID_POINTER_CHECK
|
|
|
|
if (!ptrchecklist_remove(address))
|
2002-10-30 02:07:06 +03:00
|
|
|
panic("free(): asked to free invalid pointer %p\n", address);
|
2002-09-11 16:44:48 +04:00
|
|
|
#endif
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
mutex_lock(&heap_lock);
|
|
|
|
|
2002-10-30 02:07:06 +03:00
|
|
|
TRACE(("free(): asked to free at ptr = %p\n", address));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
page = &heap_alloc_table[((unsigned)address - heap_base) / B_PAGE_SIZE];
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-10-30 02:07:06 +03:00
|
|
|
TRACE(("free(): page %p: bin_index %d, free_count %d\n", page, page->bin_index, page->free_count));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-09-29 05:58:22 +04:00
|
|
|
if (page[0].bin_index >= bin_count)
|
2002-10-30 02:07:06 +03:00
|
|
|
panic("free(): page %p: invalid bin_index %d\n", page, page->bin_index);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
bin = &bins[page[0].bin_index];
|
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
if (bin->element_size <= B_PAGE_SIZE && (addr_t)address % bin->element_size != 0)
|
2003-04-21 12:25:26 +04:00
|
|
|
panic("kfree: passed invalid pointer %p! Supposed to be in bin for esize 0x%x\n", address, bin->element_size);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2003-08-18 07:35:57 +04:00
|
|
|
#if PARANOID_KFREE
|
|
|
|
// mark the free space as freed
|
|
|
|
{
|
|
|
|
uint32 deadbeef = 0xdeadbeef;
|
|
|
|
uint8 *dead = (uint8 *)address;
|
2003-10-17 18:01:16 +04:00
|
|
|
uint32 i;
|
2003-08-18 07:35:57 +04:00
|
|
|
|
|
|
|
// the first 4 bytes are overwritten with the next free list pointer later
|
|
|
|
for (i = 4; i < bin->element_size; i++)
|
|
|
|
dead[i] = ((uint8 *)&deadbeef)[i % 4];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
for (i = 0; i < bin->element_size / B_PAGE_SIZE; i++) {
|
2002-10-30 02:07:06 +03:00
|
|
|
if (page[i].bin_index != page[0].bin_index)
|
|
|
|
panic("free(): not all pages in allocation match bin_index\n");
|
2002-07-09 16:24:59 +04:00
|
|
|
page[i].free_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if PARANOID_KFREE
|
|
|
|
// walk the free list on this bin to make sure this address doesn't exist already
|
|
|
|
{
|
|
|
|
unsigned int *temp;
|
2002-10-30 02:07:06 +03:00
|
|
|
for (temp = bin->free_list; temp != NULL; temp = (unsigned int *)*temp) {
|
|
|
|
if (temp == (unsigned int *)address)
|
|
|
|
panic("free(): address %p already exists in bin free list\n", address);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
*(unsigned int *)address = (unsigned int)bin->free_list;
|
|
|
|
bin->free_list = address;
|
|
|
|
bin->alloc_count--;
|
|
|
|
bin->free_count++;
|
|
|
|
|
|
|
|
mutex_unlock(&heap_lock);
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:21:40 +04:00
|
|
|
|
2002-10-30 02:07:06 +03:00
|
|
|
/** Naive implementation of realloc() - it's very simple but
|
|
|
|
* it's there and working.
|
|
|
|
* It takes the bin of the current allocation if the new size
|
|
|
|
* fits in and is larger than the size of the next smaller bin.
|
|
|
|
* If not, it allocates a new chunk of memory, and copies and
|
|
|
|
* frees the old buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void *
|
|
|
|
realloc(void *address, size_t newSize)
|
|
|
|
{
|
|
|
|
void *newAddress = NULL;
|
|
|
|
size_t maxSize = 0, minSize;
|
|
|
|
|
2002-11-28 05:25:04 +03:00
|
|
|
if (!kernel_startup && !are_interrupts_enabled())
|
|
|
|
panic("realloc(): called with interrupts disabled\n");
|
|
|
|
|
2004-10-18 19:29:17 +04:00
|
|
|
if (address != NULL && ((addr_t)address < heap_base || (addr_t)address >= (heap_base + heap_size)))
|
2002-10-30 02:07:06 +03:00
|
|
|
panic("realloc(): asked to realloc invalid address %p\n", address);
|
|
|
|
|
|
|
|
if (newSize == 0) {
|
|
|
|
free(address);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find out the size of the old allocation first
|
|
|
|
|
|
|
|
if (address != NULL) {
|
|
|
|
struct heap_page *page;
|
|
|
|
|
|
|
|
mutex_lock(&heap_lock);
|
2004-10-18 19:29:17 +04:00
|
|
|
page = &heap_alloc_table[((unsigned)address - heap_base) / B_PAGE_SIZE];
|
2002-10-30 02:07:06 +03:00
|
|
|
|
|
|
|
TRACE(("realloc(): page %p: bin_index %d, free_count %d\n", page, page->bin_index, page->free_count));
|
|
|
|
|
|
|
|
if (page[0].bin_index >= bin_count)
|
|
|
|
panic("realloc(): page %p: invalid bin_index %d\n", page, page->bin_index);
|
|
|
|
|
|
|
|
maxSize = bins[page[0].bin_index].element_size;
|
|
|
|
minSize = page[0].bin_index > 0 ? bins[page[0].bin_index - 1].element_size : 0;
|
|
|
|
|
|
|
|
mutex_unlock(&heap_lock);
|
|
|
|
|
|
|
|
// does the new allocation simply fit in the bin?
|
|
|
|
if (newSize > minSize && newSize < maxSize)
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if not, allocate a new chunk of memory
|
|
|
|
newAddress = malloc(newSize);
|
|
|
|
if (newAddress == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// copy the old data and free the old allocation
|
|
|
|
if (address) {
|
|
|
|
// we do have the maxSize of the bin at this point
|
|
|
|
memcpy(newAddress, address, min(maxSize, newSize));
|
|
|
|
free(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *
|
|
|
|
calloc(size_t numElements, size_t size)
|
|
|
|
{
|
|
|
|
void *address = malloc(numElements * size);
|
|
|
|
if (address != NULL)
|
|
|
|
memset(address, 0, numElements * size);
|
|
|
|
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|