shithub: riscv

Download patch

ref: addb36ee488757125c43c02076006dba8c9e69bc
parent: a375c9ac38e72dc5b0a6710c8da89245d154b435
author: cinap_lenrek <[email protected]>
date: Fri May 10 08:10:02 EDT 2019

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

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