Skip to content

Commit

Permalink
page_alloc fixed. #48
Browse files Browse the repository at this point in the history
  • Loading branch information
fijiol committed Apr 13, 2010
1 parent 3454aac commit a61850a
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 101 deletions.
18 changes: 9 additions & 9 deletions extended_test/page_alloc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
CC = gcc -g -I ./ -D EXTENDED_TEST

.PHONY: test
test: page_alloc
cp ../../src/include/lib/page_alloc.h ./lib/
$(CC) ./test_page_alloc.c page_alloc -o test_page_alloc
test: clean page_alloc ./test_page_alloc.c ./config.h ../../src/include/lib/page_alloc.h
cp -f ../../src/include/lib/page_alloc.h ./lib/
$(CC) ./test_page_alloc.c page_alloc.o -o test_page_alloc

page_alloc:
cp ../../src/include/lib/page_alloc.h ./lib/
$(CC) ../../src/lib/page_alloc/page_alloc.c -c -o page_alloc
page_alloc: ../../src/lib/page_alloc/page_alloc.c
cp -f ../../src/include/lib/page_alloc.h ./lib/
$(CC) ../../src/lib/page_alloc/page_alloc.c -c -o page_alloc.o

clean:
rm ./lib/page_alloc.h
rm ./page_alloc
rm ./test_page_alloc
rm -f ./lib/page_alloc.h
rm -f ./page_alloc.o
rm -f ./test_page_alloc

9 changes: 9 additions & 0 deletions extended_test/page_alloc/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

//#define DONOTUSE_PAGE_ALLOC /* for testing test of page alloc =) */
//#define INTERACTIVE_TEST /* */
//#define VERBOSE_DEBUG_OUT /* Some advanced output (list of page marks) */
//#define VERBOSE_DEBUG_OUT_MALLOC
//#define STACK_POP_FROM_HEAD /* or tail */ // I'm not sure that is working
#define TEST_COUNT 100
#define MAX_PAGE_FOR_ALLOC 0x10000

185 changes: 124 additions & 61 deletions extended_test/page_alloc/test_page_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <stdio.h>
#include <lib/page_alloc.h>

#define NOVERBOSE_DEBUG_OUT
#include "config.h"

/**
* set of page structure
Expand All @@ -21,36 +21,52 @@ 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 ) {
test_list_t* fr = NULL;
test_list_t* tmp = NULL;
int i;
for (i=0;i<count;++i) {
#ifdef VERBOSE_DEBUG_OUT
printf("%d ",i);
#endif
tmp = malloc(sizeof(test_list_t));
#ifdef VERBOSE_DEBUG_OUT
#ifdef VERBOSE_DEBUG_OUT_MALLOC
printf("\t\t\tMALLOC (page set): %08x\n",tmp);
#endif
//tmp->page = page_alloc(); /* CHECK THIS FUNCTION !!!!! */
#ifndef DONOTUSE_PAGE_ALLOC
if (!(tmp->page = page_alloc())) {
#ifdef VERBOSE_DEBUG_OUT
printf("\t\tPAGE_ALLOC: No memory enough\n");
#else
printf("%d ", i);
#endif
free(tmp);
return fr;
}
tmp->next = fr;
fr = tmp;
#endif
}
return fr;
}

/** //Have any troubles
/**
* free set of page
*/
void free_page_set(test_list_t *list) {
test_list_t* cur = list;
while (cur != NULL) {
#ifdef VERBOSE_DEBUG_OUT
printf("* ");
#endif
list = cur->next;
//page_free(cur->page); /* CHECK THIS FUNCTION !!!!! */
#ifdef VERBOSE_DEBUG_OUT
#ifndef DONOTUSE_PAGE_ALLOC
page_free(cur->page);
#endif
#ifdef VERBOSE_DEBUG_OUT_MALLOC
printf("\t\t\tFREE (page set): %08x\n",cur);
#endif
free(cur);
Expand All @@ -62,11 +78,16 @@ void free_page_set(test_list_t *list) {
* count the number of free pages
*/
size_t free_page_count() {
#ifdef DONOTUSE_PAGE_ALLOC
return 0;
#endif
/* go from prev to next */
if (get_cmark_p()==NULL) {
return 0;
}
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 @@ -77,15 +98,19 @@ size_t free_page_count() {
* count the number of allowed pages
*/
size_t allow_page_count() {
#ifdef DONOTUSE_PAGE_ALLOC
return 0;
#endif
/* go from next to prev */
if (get_cmark_p()==NULL) {
return PAGE_QUANTITY;
}
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_QUANTITY*PAGE_SIZE-fr;
return PAGE_QUANTITY-fr;
}

Expand All @@ -105,7 +130,7 @@ stack_t *stack;
void push(int count) {
printf("\tpush %d pages: ",count);
stack_t* tmp = malloc(sizeof(stack_t));
#ifdef VERBOSE_DEBUG_OUT
#ifdef VERBOSE_DEBUG_OUT_MALLOC
printf("\t\t\MALLOC (stack elem): %08x\n",tmp);
#endif
tmp->list = page_set_alloc(count);
Expand All @@ -118,14 +143,49 @@ void push(int count) {
* pop from stack
*/
void pop() {
printf("\tpop pages: ");
stack_t* tmp = stack->next;
if (stack==NULL) {
return;
}
printf("\tpop pages from ");
stack_t* tmp;

#ifdef STACK_POP_FROM_HEAD

printf("head: ");
tmp = stack->next;
free_page_set(stack->list);
#ifdef VERBOSE_DEBUG_OUT
#ifdef VERBOSE_DEBUG_OUT_MALLOC
printf("\t\t\FREE (stack elem): %08x\n",stack);
#endif
free(stack);
stack = tmp;

#else /* if STACK POP FROM TAIL */

printf("tail: ");

if (stack->next == NULL) {
tmp = stack->next;
free_page_set(stack->list);
#ifdef VERBOSE_DEBUG_OUT_MALLOC
printf("\t\t\FREE (stack elem): %08x\n",stack);
#endif
free(stack);
stack = tmp;
} else {
tmp = stack;
while (tmp->next->next != NULL) {
tmp = tmp->next;
}
free_page_set(tmp->next->list);
#ifdef VERBOSE_DEBUG_OUT_MALLOC
printf("\t\t\FREE (stack elem): %08x\n",stack);
#endif
free(tmp->next);
tmp->next = NULL;
}

#endif
printf("\n");
}

Expand All @@ -135,68 +195,62 @@ void pop() {
void do_allpage() {
pmark_t* pcur = get_cmark_p();
printf("Print map of memory: \n");
if (pcur == NULL) { /* don't exist list of empty page */
printf("\tList of free page is empty\n");
return;
}

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 count_of_error = 0;

void memory_check() {
size_t allocp = allow_page_count();
size_t freep = free_page_count();
printf("Allocated: %d ; Free %d \n", allocp , freep );
if (allocp+freep != PAGE_QUANTITY) {
printf("WARNING: Sum of allocated and free page don't equal page quality!!! \n");
++count_of_error;
}
}

int main() {
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;
const int max_page_for_alloc = MAX_PAGE_FOR_ALLOC;
int test_count = TEST_COUNT;
int tpcount,tptype;
int i;

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);
#ifdef INTERACTIVE_TEST
printf("Input: number of tests :: ");
scanf("%d",&test_count);
#endif
for (i=0;i<test_count;++i) {
#ifdef VERBOSE_DEBUG_OUT
printf("Test #%d\n",i);
do_allpage();
#ifndef INTERACTIVE_TEST
tpcount = random() % max_page_for_alloc + 1;
tptype = random() % 2;
#else
printf("Input: type action [ 0 - pop | 1 - push ] , number of page :: ");
scanf("%d %d",&tptype,&tpcount);
#endif
printf("Test #%d: Allowed %ld Free %ld \n",i,allow_page_count(),
free_page_count());

printf("Test #%d: ",i);
memory_check();

if (stack!=NULL) {
if (random()%2) {
if (tptype) {
#ifdef VERBOSE_DEBUG_OUT
printf("PUSH:\n");
#endif
push(random() % max_page_for_alloc + 1);
push(tpcount);
} else {
#ifdef VERBOSE_DEBUG_OUT
printf("POP:\n");
Expand All @@ -205,18 +259,27 @@ int main() {
}
} else {
printf("PUSH because have NULL:\n");
push(random() % max_page_for_alloc +1 );
push(tpcount);
}

#ifdef VERBOSE_DEBUG_OUT
do_allpage();
#endif
}

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

printf("END: ");
memory_check();

printf("\n\nMEMORY BAD SITUATION: %d\n",count_of_error);
//#endif
}

5 changes: 2 additions & 3 deletions src/include/lib/page_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ pmark_t *page_alloc(void);
void page_free(pmark_t *paddr);

#ifdef EXTENDED_TEST
# define PAGE_QUANTITY 0x100
# define PAGE_SIZE 0x20
#define PAGE_QUANTITY 0x100000
#define PAGE_SIZE 0x100

pmark_t* get_cmark_p();

int page_alloc_init(void);
#endif

Expand Down
Loading

0 comments on commit a61850a

Please sign in to comment.