Created
February 3, 2012 02:57
-
-
Save benwei/1727350 to your computer and use it in GitHub Desktop.
membuddy.c & makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#http://en.wikipedia.org/wiki/Buddy_memory_allocation | |
#This is a dummy code for Buddy memory allocation implementation. | |
CFLAGS=-DHAVE_DEV_SYS | |
LDFLAGS= | |
CFILES=membuddy.c | |
OBJS=$(CFILES:.c=.o) | |
BINNAME=membuddy | |
all: $(BINNAME) | |
.c.o: | |
$(CC) -c $< $(CFLAGS) | |
$(BINNAME): $(OBJS) | |
$(CC) -o $@ $^ $(LDFLAGS) | |
run: $(BINNAME) | |
./$^ | |
clean: | |
rm -f $(OBJS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef HAVE_DEV_SYS | |
#include <stdio.h> | |
#include <stdlib.h> | |
#define BOS_PRINTF printf | |
#else | |
#define BOS_PRINTF printf | |
typedef unsigned int size_t; | |
#endif | |
void *__bos_mem_heap_base_adr = 0; | |
size_t __bos_mem_heap_size = 0; | |
struct bos_mem_bluk { | |
void *addr; | |
size_t size; | |
struct bos_mem_bluk *next; | |
}; | |
struct bos_mem_map { | |
void *baseaddr; /* system reserved a big block memeory */ | |
struct bos_mem_bluk *bluk_head; | |
size_t free_size; | |
size_t size; | |
}; | |
struct bos_mem_map __bos_mem_map; | |
static struct bos_mem_map *pm = &__bos_mem_map; | |
void | |
bos_free(void *ptr); | |
void * | |
bos_malloc(size_t size); | |
void __bos_mem_init(void *ptr, size_t size) | |
{ | |
pm->baseaddr = __bos_mem_heap_base_adr; | |
pm->size = __bos_mem_heap_size; | |
pm->free_size = pm->size; | |
pm->bluk_head = NULL; | |
} | |
#define bluk_max_size 1024 | |
static struct bos_mem_bluk __bos_membluks[bluk_max_size]; | |
static struct bos_mem_bluk *pb_head = __bos_membluks; | |
void | |
bos_free(void *ptr) | |
{ | |
BOS_PRINTF("bos_free dummy.\n"); | |
} | |
struct bos_mem_bluk* get_mem_empty_bluk() | |
{ | |
struct bos_mem_bluk *p = pb_head; | |
struct bos_mem_bluk *pend = pb_head + sizeof(__bos_membluks); | |
for(;p < pend && p->addr; p++) ; | |
if (p < pend) return p; | |
return NULL; | |
} | |
void * | |
bos_malloc(size_t size) | |
{ | |
struct bos_mem_bluk *bluk = get_mem_empty_bluk(); | |
if (!bluk) return NULL; | |
/* TBM: with buddy allocation */ | |
bluk->addr = (void *)( pm->baseaddr + size ); | |
bluk->size = size; | |
BOS_PRINTF("bos_malloc %p.\n", bluk->addr); | |
return bluk->addr; | |
} | |
size_t | |
__bos_total_used(void) | |
{ | |
return pm->size - pm->free_size; | |
} | |
size_t | |
__bos_total_free(void) | |
{ | |
return pm->free_size; | |
} | |
#ifdef HAVE_DEV_SYS | |
int main() | |
{ | |
__bos_mem_heap_size = 4096*1024; | |
__bos_mem_heap_base_adr = malloc(__bos_mem_heap_size); | |
__bos_mem_init(__bos_mem_heap_base_adr, __bos_mem_heap_size); | |
void *p16a = bos_malloc(16); | |
void *p16b = bos_malloc(16); | |
bos_free(p16a); | |
bos_free(p16b); | |
size_t total_used_mem = __bos_total_used(); | |
BOS_PRINTF("Free: %08lx Used: %08lx\n", __bos_total_free(), total_used_mem); | |
if (total_used_mem == 0) { | |
BOS_PRINTF("test buddy system: passed\n"); | |
return 0; | |
} else { | |
BOS_PRINTF("test buddy system: failed\n"); | |
} | |
return 1; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment