area alloc: mapping optionnal
This commit is contained in:
parent
2c6ffe34a1
commit
46e1bb0642
@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
#define PAGING_MEM_USER 1
|
#define PAGING_MEM_USER (1U << 0)
|
||||||
#define PAGING_MEM_READ (1U << 1)
|
#define PAGING_MEM_READ (1U << 1)
|
||||||
#define PAGING_MEM_WRITE (1U << 2)
|
#define PAGING_MEM_WRITE (1U << 2)
|
||||||
|
|
||||||
|
@ -127,7 +127,7 @@ static int allocSlab(struct slabDesc **desc, size_t size, size_t sizeSlab, int s
|
|||||||
|
|
||||||
nbPage = DIV_ROUND_UP(sizeSlab, PAGE_SIZE);
|
nbPage = DIV_ROUND_UP(sizeSlab, PAGE_SIZE);
|
||||||
if (allocInitialized) {
|
if (allocInitialized) {
|
||||||
alloc = areaAlloc(nbPage);
|
alloc = areaAlloc(nbPage, AREA_PHY_MAP);
|
||||||
if (alloc == (paddr_t)NULL)
|
if (alloc == (paddr_t)NULL)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
} else {
|
} else {
|
||||||
@ -182,7 +182,7 @@ static int allocSlabEntry(struct slabEntry **desc, size_t size, size_t sizeSlab,
|
|||||||
}
|
}
|
||||||
|
|
||||||
nbPage = DIV_ROUND_UP(sizeSlab, PAGE_SIZE);
|
nbPage = DIV_ROUND_UP(sizeSlab, PAGE_SIZE);
|
||||||
vaddr_t alloc = areaAlloc(nbPage);
|
vaddr_t alloc = areaAlloc(nbPage, AREA_PHY_MAP);
|
||||||
if (alloc == (paddr_t)NULL)
|
if (alloc == (paddr_t)NULL)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
@ -262,7 +262,7 @@ void *malloc(size_t size)
|
|||||||
disable_IRQs(flags);
|
disable_IRQs(flags);
|
||||||
|
|
||||||
if (size >= PAGE_SIZE){
|
if (size >= PAGE_SIZE){
|
||||||
vaddr_t area = areaAlloc(DIV_ROUND_UP(size, PAGE_SIZE));
|
vaddr_t area = areaAlloc(DIV_ROUND_UP(size, PAGE_SIZE), AREA_PHY_MAP);
|
||||||
|
|
||||||
return (void *)area;
|
return (void *)area;
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ static void insertSorted(struct memArea **list, struct memArea *item)
|
|||||||
list_add_tail(*list, item);
|
list_add_tail(*list, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
vaddr_t areaAlloc(unsigned int nbPages)
|
vaddr_t areaAlloc(unsigned int nbPages, uint32_t flags)
|
||||||
{
|
{
|
||||||
struct memArea *area, *allocated;
|
struct memArea *area, *allocated;
|
||||||
|
|
||||||
@ -75,33 +75,35 @@ vaddr_t areaAlloc(unsigned int nbPages)
|
|||||||
list_delete(freeArea, area);
|
list_delete(freeArea, area);
|
||||||
insertSorted(&usedArea, area);
|
insertSorted(&usedArea, area);
|
||||||
allocated = area;
|
allocated = area;
|
||||||
|
} else {
|
||||||
|
struct memArea *newArea = (struct memArea *)malloc(sizeof(struct memArea));
|
||||||
|
if (!newArea) {
|
||||||
|
pr_devel("Failed to allocated area of %d pages\n", nbPages);
|
||||||
|
return (vaddr_t)NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Avoid insertSorted(freeArea, newArea) call by modifying area
|
||||||
|
|
||||||
|
newArea->nbPages = nbPages;
|
||||||
|
newArea->startAddr = area->startAddr;
|
||||||
|
|
||||||
|
area->nbPages -= nbPages;
|
||||||
|
area->startAddr += nbPages * PAGE_SIZE;
|
||||||
|
|
||||||
|
insertSorted(&usedArea, newArea);
|
||||||
|
|
||||||
|
allocated = newArea;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct memArea *newArea = (struct memArea *)malloc(sizeof(struct memArea));
|
if (flags & AREA_PHY_MAP) {
|
||||||
if (!newArea){
|
for (uint i = 0; i < nbPages; i++) {
|
||||||
pr_devel("Failed to allocated area of %d pages\n", nbPages);
|
paddr_t page = allocPhyPage(1);
|
||||||
return (vaddr_t)NULL;
|
if (page) {
|
||||||
}
|
pageMap(allocated->startAddr + i * PAGE_SIZE, page, PAGING_MEM_WRITE);
|
||||||
|
} else {
|
||||||
// Avoid insertSorted(freeArea, newArea) call by modifying area
|
// TODO
|
||||||
|
assert(1);
|
||||||
newArea->nbPages = nbPages;
|
}
|
||||||
newArea->startAddr = area->startAddr;
|
|
||||||
|
|
||||||
area->nbPages -= nbPages;
|
|
||||||
area->startAddr += nbPages * PAGE_SIZE;
|
|
||||||
|
|
||||||
insertSorted(&usedArea, newArea);
|
|
||||||
|
|
||||||
allocated = newArea;
|
|
||||||
|
|
||||||
for (uint i = 0; i < nbPages; i++) {
|
|
||||||
paddr_t page = allocPhyPage(1);
|
|
||||||
if (page) {
|
|
||||||
pageMap(newArea->startAddr + i * PAGE_SIZE, page, PAGING_MEM_WRITE);
|
|
||||||
} else {
|
|
||||||
// TODO
|
|
||||||
assert(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,6 +177,9 @@ int areaFree(vaddr_t addr)
|
|||||||
pr_info("Cannot find memArea associated to %p\n", addr);
|
pr_info("Cannot find memArea associated to %p\n", addr);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
for(uint i = 0; i < area->nbPages; i++){
|
||||||
|
pageUnmap( area->startAddr + i * PAGE_SIZE);
|
||||||
|
}
|
||||||
list_delete(usedArea, area);
|
list_delete(usedArea, area);
|
||||||
insertSorted(&freeArea, area);
|
insertSorted(&freeArea, area);
|
||||||
|
|
||||||
|
@ -2,6 +2,11 @@
|
|||||||
#include "paging.h"
|
#include "paging.h"
|
||||||
#include "stdarg.h"
|
#include "stdarg.h"
|
||||||
|
|
||||||
|
/* Pure Virtual Memory Allocation */
|
||||||
|
|
||||||
|
// areaAlloc map vmem to phy pages
|
||||||
|
#define AREA_PHY_MAP (1<<0)
|
||||||
|
|
||||||
struct memArea {
|
struct memArea {
|
||||||
vaddr_t startAddr;
|
vaddr_t startAddr;
|
||||||
uint nbPages;
|
uint nbPages;
|
||||||
@ -11,6 +16,6 @@ struct memArea {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void areaInit(vaddr_t firstMemUsed, vaddr_t lastUsed);
|
void areaInit(vaddr_t firstMemUsed, vaddr_t lastUsed);
|
||||||
vaddr_t areaAlloc(unsigned int nbPages);
|
vaddr_t areaAlloc(unsigned int nbPages, uint32_t flags);
|
||||||
int areaFree(vaddr_t addr);
|
int areaFree(vaddr_t addr);
|
||||||
int areaAdd(vaddr_t addr, uint nbPages, int isFree);
|
int areaAdd(vaddr_t addr, uint nbPages, int isFree);
|
||||||
|
Loading…
Reference in New Issue
Block a user