[mem] Naïve, wasteful, utterly terrible, page-aligned allocations with valloc(). Works with free, too.

This commit is contained in:
Kevin Lange 2011-02-27 21:58:34 -06:00
parent 211ad21467
commit 407558256d
2 changed files with 33 additions and 2 deletions

View File

@ -7,6 +7,8 @@
* Overall competition winner for speed. * Overall competition winner for speed.
* Well ranked in memory usage. * Well ranked in memory usage.
* *
* XXX: Modified to work withe the ToAru kernel.
*
* Copyright (c) 2010 Kevin Lange. All rights reserved. * Copyright (c) 2010 Kevin Lange. All rights reserved.
* *
* Developed by: Kevin Lange <lange7@acm.uiuc.edu> * Developed by: Kevin Lange <lange7@acm.uiuc.edu>
@ -137,6 +139,7 @@
static void * __attribute__ ((malloc)) klmalloc(size_t size); static void * __attribute__ ((malloc)) klmalloc(size_t size);
static void * __attribute__ ((malloc)) klrealloc(void * ptr, size_t size); static void * __attribute__ ((malloc)) klrealloc(void * ptr, size_t size);
static void * __attribute__ ((malloc)) klcalloc(size_t nmemb, size_t size); static void * __attribute__ ((malloc)) klcalloc(size_t nmemb, size_t size);
static void * __attribute__ ((malloc)) klvalloc(size_t size);
static void klfree(void * ptr); static void klfree(void * ptr);
void * __attribute__ ((malloc)) malloc(size_t size) { void * __attribute__ ((malloc)) malloc(size_t size) {
@ -151,6 +154,10 @@ void * __attribute__ ((malloc)) calloc(size_t nmemb, size_t size) {
return klcalloc(nmemb, size); return klcalloc(nmemb, size);
} }
void * __attribute__ ((malloc)) valloc(size_t size) {
return klvalloc(size);
}
void free(void * ptr) { void free(void * ptr) {
klfree(ptr); klfree(ptr);
} }
@ -682,6 +689,17 @@ static void klfree(void *ptr) {
return; return;
} }
/*
* Woah, woah, hold on, was this a page-aligned block?
*/
if ((uintptr_t)ptr % PAGE_SIZE == 0) {
/*
* Well howdy-do, it was.
*/
kprintf("Attempted to free herpaderpaderpa.\n");
ptr = (void *)((uintptr_t)ptr - 1);
}
/* /*
* Get our pointer to the head of this block by * Get our pointer to the head of this block by
* page aligning it. * page aligning it.
@ -799,7 +817,18 @@ static void klfree(void *ptr) {
*/ */
klmalloc_stack_push(header, ptr); klmalloc_stack_push(header, ptr);
} }
}
/* }}} */
/* valloc() {{{ */
static void * __attribute__ ((malloc)) klvalloc(size_t size) {
/*
* Allocate a page-aligned block.
* XXX: THIS IS HORRIBLY, HORRIBLY WASTEFUL!! ONLY USE THIS
* IF YOU KNOW WHAT YOU ARE DOING!
*/
size_t true_size = size + PAGE_SIZE - sizeof(klmalloc_big_bin_header); /* Here we go... */
void * result = klmalloc(true_size);
return (void *)((uintptr_t)result + (PAGE_SIZE - sizeof(klmalloc_big_bin_header)));
} }
/* }}} */ /* }}} */
/* realloc() {{{ */ /* realloc() {{{ */

View File

@ -1,6 +1,7 @@
/* /*
* Kernel Memory Manager * Kernel Memory Manager
* (and pager) * vim:tabstop=4
* vim:noexpandtab
*/ */
#include <system.h> #include <system.h>
@ -24,6 +25,7 @@ kmalloc_real(
int align, int align,
uintptr_t * phys uintptr_t * phys
) { ) {
if (align && (placement_pointer & 0xFFFFF000)) { if (align && (placement_pointer & 0xFFFFF000)) {
placement_pointer &= 0xFFFFF000; placement_pointer &= 0xFFFFF000;
} }