Merge branch 'master' into signal-dev
[lunaix-os.git] / lunaix-os / kernel / mm / region.c
1 #include <lunaix/mm/region.h>
2 #include <lunaix/mm/kalloc.h>
3 #include <lunaix/process.h>
4
5 void region_add(struct proc_info* proc,unsigned long start, unsigned long end, unsigned int attr) {
6     struct mm_region* region = lxmalloc(sizeof(struct mm_region));
7
8     *region = (struct mm_region) {
9         .attr = attr,
10         .end = end,
11         .start = start
12     };
13
14     if (!proc->mm.regions) {
15         llist_init_head(&region->head);
16         proc->mm.regions = region;
17     }
18     else {
19         llist_append(&proc->mm.regions->head, &region->head);
20     }
21 }
22
23 void region_release_all(struct proc_info* proc) {
24     struct mm_region* head = proc->mm.regions;
25     struct mm_region *pos, *n;
26
27     llist_for_each(pos, n, &head->head, head) {
28         lxfree(pos);
29     }
30
31     proc->mm.regions = NULL;
32 }
33
34 struct mm_region* region_get(struct proc_info* proc, unsigned long vaddr) {
35     struct mm_region* head = proc->mm.regions;
36     
37     if (!head) {
38         return NULL;
39     }
40
41     struct mm_region *pos, *n;
42
43     llist_for_each(pos, n, &head->head, head) {
44         if (vaddr >= pos->start && vaddr < pos->end) {
45             return pos;
46         }
47     }
48     return NULL;
49 }