Resolve hisenbugs regarding GCC and -Os
This commit is contained in:
@ -8,15 +8,13 @@
|
||||
#include <log.h>
|
||||
|
||||
extern void main(void);
|
||||
extern uint8_t _bss_start;
|
||||
extern uint8_t _bss_end;
|
||||
extern uint8_t _bss_start[];
|
||||
extern uint8_t _bss_end[];
|
||||
|
||||
void clearbss(void) {
|
||||
uint8_t *ps = &_bss_start;
|
||||
uint8_t *pe = &_bss_end;
|
||||
size_t sz = pe - ps;
|
||||
for (size_t i = 0; i < sz; i++) {
|
||||
ps[i] = 0;
|
||||
uint8_t *p = _bss_start;
|
||||
while (p < _bss_end) {
|
||||
*p++ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,7 @@
|
||||
#define LACKS_STRINGS_H 1
|
||||
#define LACKS_SCHED_H 1
|
||||
#define HAVE_MMAP 0
|
||||
#define MORECORE_CONTIGUOUS 0
|
||||
#define ABORT \
|
||||
do { \
|
||||
uprintf("dlmalloc: Aborting...\n"); \
|
||||
@ -54,50 +55,28 @@ static MLOCK_T malloc_global_mutex = { 0 };
|
||||
|
||||
#define PAGE_SIZE 0x1000
|
||||
|
||||
static uint8_t *heap_start = NULL;
|
||||
static uint8_t *heap_end = NULL;
|
||||
static uint8_t *heap_commit = NULL;
|
||||
|
||||
static size_t _roundpage(size_t sz) {
|
||||
return (sz + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
|
||||
}
|
||||
|
||||
void *_last = 0;
|
||||
|
||||
void *sbrk(ptrdiff_t inc) {
|
||||
if (!heap_end) {
|
||||
size_t allocsz = PAGE_SIZE;
|
||||
uint8_t *maddr = NULL;
|
||||
int32_t ret = mman_map(NULL, allocsz, MMAN_MAP_PF_RW, 0, &maddr);
|
||||
if (ret != E_OK || maddr == NULL) {
|
||||
return (void *)-1;
|
||||
}
|
||||
heap_start = maddr;
|
||||
heap_end = maddr;
|
||||
heap_commit = maddr + allocsz;
|
||||
if (inc < 0) {
|
||||
return 0;
|
||||
}
|
||||
if (!inc) {
|
||||
return _last;
|
||||
}
|
||||
|
||||
if (inc == 0) {
|
||||
return heap_end;
|
||||
uint64_t pages = _roundpage(inc);
|
||||
uint8_t *maddr = NULL;
|
||||
int32_t ret = mman_map(NULL, pages, MMAN_MAP_PF_RW, 0, &maddr);
|
||||
if (ret != E_OK) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t *oldbrk = heap_end;
|
||||
uint8_t *newbrk = heap_end + inc;
|
||||
|
||||
if (newbrk < heap_start) {
|
||||
return (void *)-1;
|
||||
}
|
||||
|
||||
if (inc > 0) {
|
||||
if (newbrk > heap_commit) {
|
||||
size_t need = (size_t)(newbrk - heap_commit);
|
||||
size_t extra = _roundpage(need);
|
||||
uint8_t *maddr = NULL;
|
||||
int32_t ret = mman_map(NULL, extra, MMAN_MAP_PF_RW, 0, &maddr);
|
||||
if (ret != E_OK || maddr == NULL) {
|
||||
return (void *)-1;
|
||||
}
|
||||
heap_commit += extra;
|
||||
}
|
||||
}
|
||||
heap_end = newbrk;
|
||||
return (void *)oldbrk;
|
||||
string_memset(maddr, 0, pages);
|
||||
_last = (void *)(maddr + inc);
|
||||
return maddr;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user