[lib][heap] more WIP on heap stuffs
This commit is contained in:
@@ -558,14 +558,19 @@ static inline int munmap(void *base, size_t len) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int direct_mmap(size_t s)
|
||||
{
|
||||
panic("direct map");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define MMAP(s) mmap(s)
|
||||
#define DIRECT_MMAP(s) mmap(s)
|
||||
#define DIRECT_MMAP(s) direct_mmap(s)
|
||||
#define MUNMAP(b, s) munmap(b, s)
|
||||
#define DEFAULT_MMAP_THRESHOLD MAX_SIZE_T /* disable direct mapping of chunks */
|
||||
#define DEFAULT_GRANULARITY PAGE_SIZE // (64*1024)
|
||||
#define HAVE_MORECORE 0
|
||||
#define MORECORE dl_sbrk
|
||||
void *dl_sbrk(long incr);
|
||||
#define USE_LOCKS 2
|
||||
#include <debug.h>
|
||||
#define ABORT panic("dlmalloc abort\n")
|
||||
#define MALLOC_FAILURE_ACTION //dprintf(INFO, "dlmalloc failure\n");
|
||||
#endif /* LK */
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2008-2014 Travis Geiselbrecht
|
||||
* Copyright (c) 2008-2015 Travis Geiselbrecht
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files
|
||||
@@ -31,8 +31,9 @@
|
||||
#include <kernel/spinlock.h>
|
||||
#include <lib/console.h>
|
||||
|
||||
#define LOCAL_TRACE 0
|
||||
#define LOCAL_TRACE 1
|
||||
|
||||
/* delayed free list */
|
||||
struct list_node delayed_free_list = LIST_INITIAL_VALUE(delayed_free_list);
|
||||
spin_lock_t delayed_free_lock = SPIN_LOCK_INITIAL_VALUE;
|
||||
|
||||
@@ -122,7 +123,7 @@ static void heap_free_delayed_list(void)
|
||||
|
||||
void *heap_alloc(size_t size, unsigned int alignment)
|
||||
{
|
||||
LTRACEF("size %zd, align %d\n", size, alignment);
|
||||
LTRACEF("size %zd, align %u\n", size, alignment);
|
||||
|
||||
// deal with the pending free list
|
||||
if (unlikely(!list_is_empty(&delayed_free_list))) {
|
||||
@@ -193,7 +194,8 @@ ssize_t heap_grow_memory(void **ptr, size_t size)
|
||||
if (have_asked_for_memory)
|
||||
return ERR_NO_MEMORY;
|
||||
|
||||
*ptr = HEAP_START;
|
||||
// XXX dont return all of the range on the first call
|
||||
*ptr = (void *)HEAP_START;
|
||||
size = HEAP_LEN;
|
||||
have_asked_for_memory = true;
|
||||
#endif
|
||||
@@ -295,11 +297,11 @@ usage:
|
||||
}
|
||||
|
||||
if (strcmp(argv[1].str, "info") == 0) {
|
||||
heap_dump(); // XXX
|
||||
heap_dump();
|
||||
} else if (strcmp(argv[1].str, "alloc") == 0) {
|
||||
if (argc < 3) goto notenoughargs;
|
||||
|
||||
void *ptr = heap_alloc(argv[2].u, (argc >= 3) ? argv[3].u : 0);
|
||||
void *ptr = heap_alloc(argv[2].u, (argc >= 4) ? argv[3].u : 0);
|
||||
printf("heap_alloc returns %p\n", ptr);
|
||||
} else if (strcmp(argv[1].str, "free") == 0) {
|
||||
if (argc < 2) goto notenoughargs;
|
||||
|
||||
@@ -30,7 +30,6 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <kernel/thread.h>
|
||||
#include <kernel/mutex.h>
|
||||
#include <lib/miniheap.h>
|
||||
#include <lib/heap.h>
|
||||
@@ -214,10 +213,8 @@ void *miniheap_alloc(size_t size, unsigned int alignment)
|
||||
size += alignment;
|
||||
}
|
||||
|
||||
#if WITH_KERNEL_VM
|
||||
int retry_count = 0;
|
||||
retry:
|
||||
#endif
|
||||
mutex_acquire(&theheap.lock);
|
||||
|
||||
// walk through the list
|
||||
@@ -289,7 +286,6 @@ retry:
|
||||
|
||||
mutex_release(&theheap.lock);
|
||||
|
||||
#if WITH_KERNEL_VM
|
||||
/* try to grow the heap if we can */
|
||||
if (ptr == NULL && retry_count == 0) {
|
||||
ssize_t err = heap_grow(size);
|
||||
@@ -298,7 +294,6 @@ retry:
|
||||
goto retry;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
LTRACEF("returning ptr %p\n", ptr);
|
||||
|
||||
@@ -372,7 +367,7 @@ static ssize_t heap_grow(size_t size)
|
||||
return ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
LTRACEF("growing heap by 0x%zx bytes, allocated 0x%zx, new ptr %p\n", size, allocated, ptr);
|
||||
LTRACEF("growing heap by 0x%zx bytes, allocated 0x%zx, new ptr %p\n", size, (size_t)allocated, ptr);
|
||||
|
||||
heap_insert_free_chunk(heap_create_free_chunk(ptr, allocated, true));
|
||||
|
||||
@@ -402,9 +397,6 @@ void miniheap_init(void *ptr, size_t len)
|
||||
theheap.base = 0;
|
||||
theheap.len = 0;
|
||||
theheap.remaining = 0; // will get set by heap_insert_free_chunk()
|
||||
theheap.low_watermark = theheap.len;
|
||||
|
||||
// create an initial free chunk
|
||||
//heap_insert_free_chunk(heap_create_free_chunk(theheap.base, theheap.len, false));
|
||||
theheap.low_watermark = 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user