Skip to content

Commit

Permalink
test page alloc is working, but while page alloc isn't working
Browse files Browse the repository at this point in the history
  • Loading branch information
fijiol committed Apr 11, 2010
1 parent d33ccbe commit 61aa37b
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 41 deletions.
2 changes: 1 addition & 1 deletion extended_test/page_alloc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#CC = gcc -I $(INCLUDES) -imacros $(MACROS) # -D EXTENDED_TEST
#CC = gcc -include ../../src/include/lib/page_alloc.h -imacros $(MACROS) # -D EXTENDED_TEST
#CC = gcc -include ../../src/include/lib/page_alloc.h -D EXTENDED_TEST
CC = gcc -I ./ -D EXTENDED_TEST
CC = gcc -g -I ./ -D EXTENDED_TEST

.PHONY: test
test: page_alloc
Expand Down
124 changes: 100 additions & 24 deletions extended_test/page_alloc/test_page_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <stdio.h>
#include <lib/page_alloc.h>

#define NOVERBOSE_DEBUG_OUT

/**
* set of page structure
*/
Expand All @@ -19,7 +21,7 @@ typedef struct test_list {
struct test_list *next;
}test_list_t;

/**
/** //Have any troubles
* alloc set of page
*/
test_list_t* page_set_alloc( int count ) {
Expand All @@ -29,15 +31,17 @@ test_list_t* page_set_alloc( int count ) {
for (i=0;i<count;++i) {
printf("%d ",i);
tmp = malloc(sizeof(test_list_t));
tmp->page = page_alloc(); /* CHECK THIS FUNCTION !!!!! */
tmp->page = NULL;
#ifdef VERBOSE_DEBUG_OUT
printf("\t\t\tMALLOC (page set): %08x\n",tmp);
#endif
//tmp->page = page_alloc(); /* CHECK THIS FUNCTION !!!!! */
tmp->next = fr;
fr = tmp;
}
return fr;
}

/**
/** //Have any troubles
* free set of page
*/
void free_page_set(test_list_t *list) {
Expand All @@ -46,6 +50,9 @@ void free_page_set(test_list_t *list) {
printf("* ");
list = cur->next;
//page_free(cur->page); /* CHECK THIS FUNCTION !!!!! */
#ifdef VERBOSE_DEBUG_OUT
printf("\t\t\tFREE (page set): %08x\n",cur);
#endif
free(cur);
cur = list;
}
Expand All @@ -54,12 +61,12 @@ void free_page_set(test_list_t *list) {
/**
* count the number of free pages
*/
size_t free_page_count() { /* CHECK THIS FUNCTION !!!!! */
size_t free_page_count() {
/* go from prev to next */
size_t fr = cmark_p->psize;
pmark_t* cur = cmark_p->pnext;
while (cur != cmark_p) {
printf(". ");
size_t fr = get_cmark_p()->psize;
pmark_t* cur = get_cmark_p()->pnext;
while (cur != get_cmark_p()) {
// printf(". ");
fr = fr + cur->psize;
cur = cur->pnext;
}
Expand All @@ -69,16 +76,17 @@ size_t free_page_count() { /* CHECK THIS FUNCTION !!!!! */
/**
* count the number of allowed pages
*/
size_t allow_page_count() { /* CHECK THIS FUNCTION !!!!! */
size_t allow_page_count() {
/* go from next to prev */
size_t fr = cmark_p->psize;
pmark_t* cur = cmark_p->pprev;
while (cur != cmark_p) {
printf(". ");
size_t fr = get_cmark_p()->psize;
pmark_t* cur = get_cmark_p()->pprev;
while (cur != get_cmark_p()) {
// printf(". ");
fr = fr + cur->psize;
cur = cur->pprev;
}
return PAGE_SIZE*PAGE_QUANTITY-fr;
//return PAGE_QUANTITY*PAGE_SIZE-fr;
return PAGE_QUANTITY-fr;
}

/**
Expand All @@ -97,6 +105,9 @@ stack_t *stack;
void push(int count) {
printf("\tpush %d pages: ",count);
stack_t* tmp = malloc(sizeof(stack_t));
#ifdef VERBOSE_DEBUG_OUT
printf("\t\t\MALLOC (stack elem): %08x\n",tmp);
#endif
tmp->list = page_set_alloc(count);
tmp->next = stack;
stack = tmp;
Expand All @@ -107,40 +118,105 @@ void push(int count) {
* pop from stack
*/
void pop() {
printf("\tpop pages: \n");
printf("\tpop pages: ");
stack_t* tmp = stack->next;
free_page_set(stack->list);
#ifdef VERBOSE_DEBUG_OUT
printf("\t\t\FREE (stack elem): %08x\n",stack);
#endif
free(stack);
stack = tmp;
printf("\n");
}

/**
* Debug output memory
*/
void do_allpage() {
pmark_t* pcur = get_cmark_p();
printf("Print map of memory: \n");
do {
printf("\tStruct: %08x\n\t\tsize: %d\n\t\tnext: %08x\n\t\tprev: %08x\n\n",
pcur, pcur->psize, pcur->pnext, pcur->pprev);
pcur = pcur->pnext;
} while (pcur != get_cmark_p());
}

int main() {
printf("TEST PAGE_ALLOC NOW\n");
const int test_count = 10;
const int max_page_for_alloc = 20;
printf("TEST PAGE_ALLOC NOW. COUNT OF PAGES: %ld\n",(long) PAGE_QUANTITY);
const int test_count = 1000;
const int max_page_for_alloc = 2000;
int i;
push(random() % max_page_for_alloc + 1);

page_alloc_init(); // WTF?! must be auto-init in function "page_alloc"
//ok while test it, I don't run page_alloc =)

#ifdef VERBOSE_DEBUG_OUT
page_alloc_init();
do_allpage();

printf("Allowed %ld Free %ld \n",allow_page_count(), free_page_count());
printf("PAGE ALLOC HAS INIT\n\n");
#endif

#if 0

void * page = page_alloc();

push(1);
do_allpage();

push(2);
do_allpage();

push(4);
do_allpage();

page_free(page);
do_allpage();

pop();
do_allpage();

printf("Allowed %ld Free %ld \n",allow_page_count(), free_page_count());
#endif

//#if 0
push(random() % max_page_for_alloc + 1);
for (i=0;i<test_count;++i) {
#ifdef VERBOSE_DEBUG_OUT
printf("Test #%d\n",i);
printf("Test #%d: Allowed %ld Free %ld \n",i,allow_page_count(),free_page_count());
do_allpage();
#endif
printf("Test #%d: Allowed %ld Free %ld \n",i,allow_page_count(),
free_page_count());

if (stack!=NULL) {
if (random()%2) {
printf("push)\n");
#ifdef VERBOSE_DEBUG_OUT
printf("PUSH:\n");
#endif
push(random() % max_page_for_alloc + 1);
} else {
printf("pop)\n");
#ifdef VERBOSE_DEBUG_OUT
printf("POP:\n");
#endif
pop();
}
} else {
printf("PUSH because have NULL:\n");
push(random() % max_page_for_alloc +1 );
}
}

while (stack!=NULL) {
#ifdef VERBOSE_DEBUG_OUT
printf("Test #END\n",i);
printf("Test #END (free last): Allowed %ld Free %ld \n",allow_page_count(),free_page_count());
#endif
printf("Test #END (free last): Allowed %ld Free %ld \n",allow_page_count(),
free_page_count());
pop();
}
//#endif
}

6 changes: 4 additions & 2 deletions src/include/lib/page_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ void page_free(pmark_t *paddr);
#ifdef EXTENDED_TEST
# define PAGE_QUANTITY 0x100
# define PAGE_SIZE 0x20
static uint8_t page_pool[PAGE_SIZE][PAGE_QUANTITY];
static pmark_t *cmark_p = (pmark_t *) page_pool;

pmark_t* get_cmark_p();

int page_alloc_init(void);
#endif

#endif /* __PAGE_ALLOC_H_ */
Expand Down
53 changes: 39 additions & 14 deletions src/lib/page_alloc/page_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* TODO:
* debug it
* page_alloc don't work
*
* rewrite it all!!!!!
*/

#include <lib/page_alloc.h>
Expand All @@ -27,16 +29,19 @@ int page_alloc_hasinit = 0;
#ifdef EXTENDED_TEST

//very TEMP!!!
#include <stdio.h>
#if 0
// #include <stdio.h>
static uint8_t page_pool[PAGE_SIZE][PAGE_QUANTITY];
static pmark_t *cmark_p = (pmark_t *) page_pool;
#endif
#else
#define START_MEMORY_ADDR 0x40000000
static pmark_t *cmark_p = (pmark_t *)START_MEMORY_ADDR;
#endif

#ifdef EXTENDED_TEST
pmark_t* get_cmark_p() {
return cmark_p;
}
#endif
/* defragmentation pages */
static void page_defrag(void ) {
}
Expand All @@ -51,52 +56,72 @@ static pmark_t *copy_mark( pmark_t *from , pmark_t *to ) {

/* Initialize page allocator */
int page_alloc_init(void) {
#if 0
pmark_t *cmark_pn = cmark_p + PAGE_SIZE;
#endif

cmark_p->psize = PAGE_QUANTITY;
cmark_p->pnext = cmark_p;
cmark_p->pprev = cmark_p;

#if 0
cmark_pn->psize = PAGE_QUANTITY-1;
cmark_pn->pnext = cmark_p;
cmark_pn->pprev = cmark_p;
#endif
return 0;
}

/* allocate page */
pmark_t *page_alloc(void) {
pmark_t *page_alloc(void) { /* Don't work!!!! */
size_t psize = 1;
pmark_t *pcur;
int has_only_one_block = 0;

pmark_t *pcur,*tmp;

if (!page_alloc_hasinit) {
page_alloc_init();
page_alloc_hasinit = 1;
}
/* find first proper block */
pcur = cmark_p->pnext;
while ( pcur != cmark_p /*&& pcur->psize < psize */) {
pcur = pcur->pnext;
pcur = cmark_p;

#if 0
if (pcur->pnext == pcur) {
has_only_one_block = 1;
}
#endif

/* check finded block */
if ( pcur->psize >= psize ) {
/* change list and return value */
if (pcur->psize > psize ) {
pcur->psize = pcur->psize - psize;
pcur->psize -= psize;
tmp = cmark_p->pnext;
cmark_p->pprev->pnext = pcur + PAGE_SIZE * psize;
tmp->pprev = pcur + PAGE_SIZE * psize;
cmark_p = copy_mark( pcur , pcur + PAGE_SIZE * psize );
return pcur;
} else {
/* psize == pcur->psize */
pcur->pprev->pnext = pcur->pnext;
pcur->pnext->pprev = pcur->pprev;
cmark_p = pcur->pnext;
return pcur;
}

}

return NULL;
}

/* free page that was allocated */
void page_free(pmark_t *paddr) {
void page_free(pmark_t *paddr) { /* Don't work!!!! */
/* find */
paddr->psize = 1;
paddr->pprev = cmark_p->pprev;
cmark_p->pprev = paddr;
paddr->pnext = cmark_p;
paddr->pnext = cmark_p->pnext;
paddr->pprev = cmark_p;
cmark_p->pnext->pprev = paddr;
cmark_p->pnext = paddr;
}


0 comments on commit 61aa37b

Please sign in to comment.