Skip to content

Commit

Permalink
Write page_alloc (no page_free). #48
Browse files Browse the repository at this point in the history
  • Loading branch information
fijiol committed Apr 4, 2010
1 parent 64a900f commit 02727a7
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
46 changes: 46 additions & 0 deletions extended_test/page_alloc/page_alloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @file
* @brief page allocator header
* @details
*
* @date 04.04.10
* @author Fedor Burdun
*/

#ifndef __PAGE_ALLOC_H_
#define __PAGE_ALLOC_H_

#ifdef DEBUG_x86_ONLY /* Code included for debug specific */
#include <malloc.h>
#else /* Embox specific code */
#endif /* End of specific code */

struct pmark_t;

#ifdef DEBUG_x86_ONLY /* Code included for debug specific */

typedef long psize_t;
typedef struct pmark_t* paddr_t;
const psize_t MAXPAGECOUNT = 0xffff;
const paddr_t SIZEOFPAGE = 0x00ff;
struct paddr_t *cmark_p;
void *mem;
int palloc_hasinit = 0x0000;

#else /* Embox specific code */

#endif /* End of specific code */

/* structure of page marker */
struct pmark_t {
psize_t psize;
paddr_t *pnext;
paddr_t *pprev;
};

/* */
paddr_t page_alloc(psize_t);
void page_free(paddr_t);

#endif /* __PAGE_ALLOC_H_ */

20 changes: 20 additions & 0 deletions extended_test/page_alloc/test_page_alloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @file
* @brief test page allocator
* @details
*
* @date 04.04.10
* @author Fedor Burdun
*/


#define DEBUG_x86_ONLY
#include "page_alloc.h"

#if 0
#include "../../src/lib/page_alloc/page_alloc.c"
#endif

int main() {
}

90 changes: 90 additions & 0 deletions src/lib/page_alloc/page_alloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* @file
* @brief page allocator
* @details
*
* @date 04.04.10
* @author Fedor Burdun
*/

#if 0 /* Example code for use from header file */
typedef long psize_t;
typedef long paddr_t;
const psize_t MAXPAGECOUNT = 0xffff;
const paddr_t SIZEOFPAGE = 0xff;
pmark_t *cmark_p;
void *mem;

#ifdef DEBUG_x86_ONLY /* Code included for debug specific */
#else /* Embox specific code */
#endif /* End of specific code */

/* structure of page marker */
struct pmark_t {
psize_t psize;
pmark_t *pnext;
};

#endif

/* copy mark structure */
paddr_t copy_mark( paddr_t from , paddr_t to ) {
to->psize = from->psize;
to->pnext = from->pnext;
to->pprev = from->pprev;
return to;
}

/* Initialize page allocator */
void page_alloc_init() {
#ifdef DEBUG_x86_ONLY /* Code included for debug specific */
mem = malloc( MAXPAGECOUNT * SIZEOFPAGE );
#else /* Embox specific code */
#endif /* End of specific code */
cmark_p = mem;
cmark_p->psize = MAXPAGECOUNT;
cmark_p->pnext = cmark_p;
palloc_hasinit = 1;
}

/* allocate page */
paddr_t page_alloc(psize_t psize) {
if (!palloc_hasinit) {
page_alloc_init();
}
/* find first proper block */
struct pmark_t *pcur;
pcur = cmark_p->next;
while ( pcur != cmark_p && pcur->psize < psize ) {
pcur = pcur->next;
}
/* check finded block */
if ( pcur->psize >= psize ) {
/* change list and return value */
if (pcur->psize > psize ) {
pcur->psize = pcur->psize - psize;
cmark_p = copy_mark( pcur , pcur + SIZEOFPAGE * psize );
return pcur;
} else {
/* psize == pcur->psize */
pcur->pprev->next = pcur->next;
return pcur;
}
} else {
#if 0
page_defrag();
return page_alloc(psize);
#endif
/* generate memory error */
}
}

/* free page that was allocated */
void page_free(paddr_t paddr) {
/* find */
}

/* defragmentation pages */
void page_defrag() {
}

0 comments on commit 02727a7

Please sign in to comment.