ape: fix malloc to deal with more than 4GB of memory on 64 bit systems

This commit is contained in:
cinap_lenrek 2019-05-10 12:10:02 +02:00
parent a375c9ac38
commit addb36ee48

View file

@ -1,10 +1,9 @@
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <lock.h> #include <lock.h>
typedef unsigned int uint;
enum enum
{ {
MAGIC = 0xbada110c, MAGIC = 0xbada110c,
@ -38,7 +37,7 @@ extern void *sbrk(unsigned long);
void* void*
malloc(size_t size) malloc(size_t size)
{ {
uint next; uintptr_t next;
int pow, n; int pow, n;
Bucket *bp, *nbp; Bucket *bp, *nbp;
@ -70,16 +69,16 @@ good:
if(pow < CUTOFF) { if(pow < CUTOFF) {
n = (CUTOFF-pow)+2; n = (CUTOFF-pow)+2;
bp = sbrk(size*n); bp = sbrk(size*n);
if((int)bp < 0){ if(bp == (void*)-1){
unlock(&arena); unlock(&arena);
return nil; return nil;
} }
next = (uint)bp+size; next = (uintptr_t)bp+size;
nbp = (Bucket*)next; nbp = (Bucket*)next;
arena.btab[pow] = nbp; arena.btab[pow] = nbp;
for(n -= 2; n; n--) { for(n -= 2; n; n--) {
next = (uint)nbp+size; next = (uintptr_t)nbp+size;
nbp->next = (Bucket*)next; nbp->next = (Bucket*)next;
nbp->size = pow; nbp->size = pow;
nbp = nbp->next; nbp = nbp->next;
@ -88,7 +87,7 @@ good:
} }
else { else {
bp = sbrk(size); bp = sbrk(size);
if((int)bp < 0){ if(bp == (void*)-1){
unlock(&arena); unlock(&arena);
return nil; return nil;
} }
@ -110,7 +109,7 @@ free(void *ptr)
return; return;
/* Find the start of the structure */ /* Find the start of the structure */
bp = (Bucket*)((uint)ptr - datoff); bp = (Bucket*)((uintptr_t)ptr - datoff);
if(bp->magic != MAGIC) if(bp->magic != MAGIC)
abort(); abort();
@ -127,14 +126,14 @@ void*
realloc(void *ptr, size_t n) realloc(void *ptr, size_t n)
{ {
void *new; void *new;
uint osize; size_t osize;
Bucket *bp; Bucket *bp;
if(ptr == nil) if(ptr == nil)
return malloc(n); return malloc(n);
/* Find the start of the structure */ /* Find the start of the structure */
bp = (Bucket*)((uint)ptr - datoff); bp = (Bucket*)((uintptr_t)ptr - datoff);
if(bp->magic != MAGIC) if(bp->magic != MAGIC)
abort(); abort();