diff options
author | Alex AUVOLAT <alexis211@gmail.com> | 2012-05-04 20:47:46 +0200 |
---|---|---|
committer | Alex AUVOLAT <alexis211@gmail.com> | 2012-05-04 20:47:46 +0200 |
commit | a9bb8d1be024730fc4687aab78a8e5cb54b4f05e (patch) | |
tree | e10940a39e9864430b584cd84700b8d49aebd62a /src/kernel/mem/seg.cpp | |
parent | 35857cb36ae65779a7cab773040c0fc91387a989 (diff) | |
download | TCE-a9bb8d1be024730fc4687aab78a8e5cb54b4f05e.tar.gz TCE-a9bb8d1be024730fc4687aab78a8e5cb54b4f05e.zip |
More OOP;
Diffstat (limited to 'src/kernel/mem/seg.cpp')
-rw-r--r-- | src/kernel/mem/seg.cpp | 98 |
1 files changed, 47 insertions, 51 deletions
diff --git a/src/kernel/mem/seg.cpp b/src/kernel/mem/seg.cpp index c29e94e..06209a1 100644 --- a/src/kernel/mem/seg.cpp +++ b/src/kernel/mem/seg.cpp @@ -1,14 +1,22 @@ #include "seg.h" #include "mem.h" #include <core/sys.h> +#include <lib/cpp.h> + +segment::segment() { + mappings = 0; +} +segment::~segment() { +} /* Call this function when mapping a segment to a page directory. Calls the appropriate map method and updates the segment's and pagedir's information. */ -segment_map *seg_map(segment* seg, page_directory *pagedir, size_t offset) { - segment_map *sm = seg->map(seg, pagedir, offset); +segment_map *segment::map(page_directory *pagedir, size_t offset) { + segment_map *sm = i_map(pagedir, offset); if (sm == 0) return 0; - seg->mappings++; - sm->seg = seg; + mappings++; + + sm->seg = this; sm->pagedir = pagedir; sm->next = pagedir->mappedSegs; pagedir->mappedSegs = sm; @@ -18,57 +26,48 @@ segment_map *seg_map(segment* seg, page_directory *pagedir, size_t offset) { /* Call this function when unmapping a segment from a page directory. The segment will automatically be deleted if it is not mapped. Calls the appropriate unmap method and updates the segment's and pagedir's information. */ -void seg_unmap(segment_map *map) { - map->seg->unmap(map); +void segment::unmap(segment_map *map) { + ASSERT(this == map->seg); + i_unmap(map); if (map->pagedir->mappedSegs == map) { - map->pagedir->mappedSegs = map->pagedir->mappedSegs->next; + map->pagedir->mappedSegs = map->next; } else { segment_map *m = map->pagedir->mappedSegs; while (m->next != 0 && m->next != map) m = m->next; if (m->next == map) m->next = map->next; } - map->seg->mappings--; - if (map->seg->mappings == 0) { - map->seg->del(map->seg); - kfree(map->seg->seg_data); - kfree(map->seg); + delete map; + mappings--; + if (mappings == 0) { + delete this; } - kfree (map); } // ************************************ SIMPLESEG stuff ************* -static segment_map* simpleseg_map(segment* seg, page_directory* pagedir, size_t offset); -static void simpleseg_unmap(segment_map*); -static void simpleseg_delete(segment *seg); -static int simpleseg_handleFault(segment_map* map, size_t addr, int write); /* Call this when creating a simpleseg. Creates the simpleseg structure and the segment structure and fills them up. */ -segment* simpleseg_make(size_t start, size_t len, int writable) { - simpleseg *ss = new simpleseg(); - segment *se = new segment(); - se->seg_data = ss; - se->mappings = 0; - se->map = simpleseg_map; - se->unmap = simpleseg_unmap; - se->del = simpleseg_delete; - se->handle_fault = simpleseg_handleFault; - ss->writable = writable; ss->start = start; ss->len = len; - return se; +simpleseg::simpleseg(size_t s, size_t l, int w) : segment() { + writable = w; + start = s; + len = l; +} + +simpleseg::~simpleseg() { } /* For internal use only. Called when a simpleseg is mapped to a pagedirectory. */ -segment_map* simpleseg_map(segment* seg, page_directory* pagedir, size_t offset) { +segment_map* simpleseg::i_map(page_directory* pagedir, size_t offset) { segment_map *sm = new segment_map(); - sm->start = ((simpleseg*)(seg->seg_data))->start; - sm->len = ((simpleseg*)(seg->seg_data))->len; + sm->start = start; + sm->len = len; return sm; } /* For internal use only. Called when a simpleseg is unmapped. Frees all the allocated pages. */ -void simpleseg_unmap(segment_map* sm) { +void simpleseg::i_unmap(segment_map* sm) { size_t i; for (i = sm->start; i < sm->start + sm->len; i += 0x1000) { page_unmapFree(pagedir_getPage(sm->pagedir, i, 0)); @@ -76,38 +75,35 @@ void simpleseg_unmap(segment_map* sm) { } /* For internal use only. Handles a page fault. Can allocate and map a frame if necessary. */ -int simpleseg_handleFault(segment_map* sm, size_t addr, int write) { - simpleseg *ss = (simpleseg*)sm->seg->seg_data; - if (write && !ss->writable) return 1; +int simpleseg::handle_fault(segment_map* sm, size_t addr, int write) { + ASSERT(sm->seg == this); + + if (write && !writable) return 1; addr &= 0xFFFFF000; page *p = pagedir_getPage(sm->pagedir, addr, 1); if (p->frame != 0) return 1; - page_map(p, frame_alloc(), 1, ss->writable); + page_map(p, frame_alloc(), 1, writable); return 0; } -/* For internal use only. Called when the simpleseg is deleted. Does nothing. */ -void simpleseg_delete(segment* seg) { -} - /* Call this to resize a simpleseg. Ajusts the size and frees pages if the new size is smaller.*/ -int simpleseg_resize(segment_map *map, size_t len) { +int simpleseg::resize(segment_map *map, size_t nlen) { + ASSERT(this == map->seg); + size_t i; if (map == 0) return -1; - if (map->seg->del != simpleseg_delete) return -2; //check segment is a simpleseg - simpleseg *s = (simpleseg*)map->seg->seg_data; - if (len & 0xFFF) len = (len & 0xFFFFF000) + 0x1000; - if (len < map->len) { - for (i = map->start + len; i < map->start + map->len; i += 0x1000) { + if (nlen & 0xFFF) nlen = (nlen & 0xFFFFF000) + 0x1000; + if (nlen < map->len) { + for (i = map->start + nlen; i < map->start + map->len; i += 0x1000) { page_unmapFree(pagedir_getPage(map->pagedir, i, 0)); } - map->len = len; - s->len = len; - } else if (len > map->len) { - map->len = len; - s->len = len; + map->len = nlen; + len = nlen; + } else if (nlen > map->len) { + map->len = nlen; + len = nlen; } return 0; } |