Explorar o código

Update to "malloc" so it will work on CPUs not supporting non-aligned words.
malloc still only works for little-endian CPUs... Patch by Jean-Yves Avenard

Eric Andersen %!s(int64=25) %!d(string=hai) anos
pai
achega
2109636071
Modificáronse 1 ficheiros con 35 adicións e 7 borrados
  1. 35 7
      libc/stdlib/malloc/malloc.c

+ 35 - 7
libc/stdlib/malloc/malloc.c

@@ -64,7 +64,7 @@
 #include <sys/mman.h>
 #include <sys/mman.h>
 #include <string.h>
 #include <string.h>
 #include "malloc.h"
 #include "malloc.h"
-
+#include <stdio.h>
 
 
 #define M_DOTRIMMING 1
 #define M_DOTRIMMING 1
 #define M_MULTITHREADED 0
 #define M_MULTITHREADED 0
@@ -168,8 +168,8 @@ void *__hunk_alloc(int size)
 	unsigned long *cpl;
 	unsigned long *cpl;
 	int i, c;
 	int i, c;
 
 
-	if (size >= HUNK_THRESHOLD)
-		size = ALIGN(size);
+	//	if (size >= HUNK_THRESHOLD)
+	size = ALIGN(size);
 
 
 	/* Look for already allocated hunkblocks */
 	/* Look for already allocated hunkblocks */
 	if ((p = __free_h[size]) == NULL) {
 	if ((p = __free_h[size]) == NULL) {
@@ -183,7 +183,10 @@ void *__hunk_alloc(int size)
 							 MAP_PRIVATE | MAP_ANONYMOUS
 							 MAP_PRIVATE | MAP_ANONYMOUS
 #endif
 #endif
 							 , 0, 0)) == (Hunk_t *) MAP_FAILED)
 							 , 0, 0)) == (Hunk_t *) MAP_FAILED)
+		  // {
+		  //  printf("hunk_alloc failed: %d, %d\n", size, errno);
 			return NULL;
 			return NULL;
+		  // }
 		memset(p, 0, HUNK_MSIZE);
 		memset(p, 0, HUNK_MSIZE);
 		p->id = HUNK_ID;
 		p->id = HUNK_ID;
 		p->total = __total_h[size];
 		p->total = __total_h[size];
@@ -195,31 +198,52 @@ void *__hunk_alloc(int size)
 	}
 	}
 
 
 	/* Locate free point in usagemap */
 	/* Locate free point in usagemap */
+	
+	/* First find a word where not all the bits are set */
 	for (cpl = (unsigned long *) usagemap(p); *cpl == 0xFFFFFFFF; cpl++);
 	for (cpl = (unsigned long *) usagemap(p); *cpl == 0xFFFFFFFF; cpl++);
+
+	/* Remember the byte position of that word */
 	i = ((unsigned char *) cpl) - usagemap(p);
 	i = ((unsigned char *) cpl) - usagemap(p);
+
+	/* Now find find a free bit in the word using binary search */
 	if (*(unsigned short *) cpl != 0xFFFF) {
 	if (*(unsigned short *) cpl != 0xFFFF) {
+
 		if (*(unsigned char *) cpl == 0xFF) {
 		if (*(unsigned char *) cpl == 0xFF) {
-			c = *(int *) (((unsigned char *) cpl) + 1);
+			c = *(((unsigned char *) cpl) + 1);
 			i++;
 			i++;
-		} else
-			c = *(int *) (unsigned char *) cpl;
+		}
+		else
+		  {
+		    c = *(unsigned char *) cpl;
+		  }
 	} else {
 	} else {
 		i += 2;
 		i += 2;
 		c = *(((unsigned char *) cpl) + 2);
 		c = *(((unsigned char *) cpl) + 2);
 		if (c == 0xFF) {
 		if (c == 0xFF) {
-			c = *(int *) (((unsigned char *) cpl) + 3);
+			c = *(((unsigned char *) cpl) + 3);
 			i++;
 			i++;
 		}
 		}
 	}
 	}
+	
+	/*
+	 * Multiply i by 8 for the bit position
+	 * Further down, we divide by 8 again to find the byte position
+	 */
 	EMUL8(i);
 	EMUL8(i);
+	
+	/* If bottom nibble is set, shift down the top nibble */
 	if ((c & 0xF) == 0xF) {
 	if ((c & 0xF) == 0xF) {
 		c >>= 4;
 		c >>= 4;
 		i += 4;
 		i += 4;
 	}
 	}
+	
+	/* If bottom 2 bits are set, shift down the top two */
 	if ((c & 0x3) == 0x3) {
 	if ((c & 0x3) == 0x3) {
 		c >>= 2;
 		c >>= 2;
 		i += 2;
 		i += 2;
 	}
 	}
+	
+	/* Check which one of the two bits is set */
 	if (c & 1)
 	if (c & 1)
 		i++;
 		i++;
 
 
@@ -230,6 +254,8 @@ void *__hunk_alloc(int size)
 		__free_h[p->size] = p->next;
 		__free_h[p->size] = p->next;
 		p->next = NULL;
 		p->next = NULL;
 	}
 	}
+	
+	// fprintf(stderr, "hunk_alloc: i=%d, p->size=%d, p=%p\n", i, p->size, p);
 	return hunk_ptr(p) + i * p->size;
 	return hunk_ptr(p) + i * p->size;
 }
 }
 #endif							/* L_malloc */
 #endif							/* L_malloc */
@@ -636,6 +662,7 @@ void __malloc_init(void)
 	}
 	}
 	mutex_init(&malloc_lock);
 	mutex_init(&malloc_lock);
 	__malloc_initialized = 1;
 	__malloc_initialized = 1;
+	// fprintf(stderr, "malloc_init: hunk_t=%d\n", sizeof(Hunk_t));
 }
 }
 #endif							/* L__malloc_init */
 #endif							/* L__malloc_init */
 
 
@@ -662,6 +689,7 @@ void *malloc(size_t size)
 	if (__malloc_initialized)
 	if (__malloc_initialized)
 		mutex_unlock(&malloc_lock);
 		mutex_unlock(&malloc_lock);
 
 
+	// fprintf(stderr, "malloc returning: s=%d, p=%p\n", size, p);
 	return p;
 	return p;
 }
 }
 #endif							/* L_malloc */
 #endif							/* L_malloc */