Browse Source

Glenn managed to shrink .text by 5 more bytes.

tags/v0.9.3
Michael Brown 18 years ago
parent
commit
cfae86f6c8
1 changed files with 6 additions and 3 deletions
  1. 6
    3
      src/core/malloc.c

+ 6
- 3
src/core/malloc.c View File

66
  */
66
  */
67
 void * alloc_memblock ( size_t size, size_t align ) {
67
 void * alloc_memblock ( size_t size, size_t align ) {
68
 	struct memory_block *block;
68
 	struct memory_block *block;
69
+	size_t align_mask;
69
 	size_t pre_size;
70
 	size_t pre_size;
70
 	ssize_t post_size;
71
 	ssize_t post_size;
71
 	struct memory_block *pre;
72
 	struct memory_block *pre;
72
 	struct memory_block *post;
73
 	struct memory_block *post;
73
 
74
 
74
-	/* Round up alignment and size to multiples of MIN_MEMBLOCK_SIZE */
75
-	align = ( align + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 );
75
+	/* Round up size to multiple of MIN_MEMBLOCK_SIZE and
76
+	 * calculate alignment mask.
77
+	 */
76
 	size = ( size + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 );
78
 	size = ( size + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 );
79
+	align_mask = ( align - 1 ) | ( MIN_MEMBLOCK_SIZE - 1 );
77
 	DBG ( "Allocating %zx (aligned %zx)\n", size, align );
80
 	DBG ( "Allocating %zx (aligned %zx)\n", size, align );
78
 
81
 
79
 	/* Search through blocks for the first one with enough space */
82
 	/* Search through blocks for the first one with enough space */
80
 	list_for_each_entry ( block, &free_blocks, list ) {
83
 	list_for_each_entry ( block, &free_blocks, list ) {
81
-		pre_size = ( - virt_to_phys ( block ) ) & ( align - 1 );
84
+		pre_size = ( - virt_to_phys ( block ) ) & align_mask;
82
 		post_size = block->size - pre_size - size;
85
 		post_size = block->size - pre_size - size;
83
 		if ( post_size >= 0 ) {
86
 		if ( post_size >= 0 ) {
84
 			/* Split block into pre-block, block, and
87
 			/* Split block into pre-block, block, and

Loading…
Cancel
Save