chore: make things more general
[lunaix-os.git] / lunaix-os / kernel / mm / region.c
1 #include <lunaix/mm/kalloc.h>
2 #include <lunaix/mm/region.h>
3
4 void
5 region_add(struct mm_region** regions,
6            unsigned long start,
7            unsigned long end,
8            unsigned int attr)
9 {
10     struct mm_region* region = lxmalloc(sizeof(struct mm_region));
11
12     *region = (struct mm_region){ .attr = attr, .end = end, .start = start };
13
14     if (!*regions) {
15         llist_init_head(&region->head);
16         *regions = region;
17     } else {
18         llist_append(&(*regions)->head, &region->head);
19     }
20 }
21
22 void
23 region_release_all(struct mm_region** regions)
24 {
25     struct mm_region *pos, *n;
26
27     llist_for_each(pos, n, &(*regions)->head, head)
28     {
29         lxfree(pos);
30     }
31
32     *regions = NULL;
33 }
34
35 void
36 region_copy(struct mm_region** src, struct mm_region** dest)
37 {
38     if (!*src) {
39         return;
40     }
41
42     struct mm_region *pos, *n;
43
44     llist_init_head(*dest);
45     llist_for_each(pos, n, &(*src)->head, head)
46     {
47         region_add(dest, pos->start, pos->end, pos->attr);
48     }
49 }
50
51 struct mm_region*
52 region_get(struct mm_region** regions, unsigned long vaddr)
53 {
54     if (!*regions) {
55         return NULL;
56     }
57
58     struct mm_region *pos, *n;
59
60     llist_for_each(pos, n, &(*regions)->head, head)
61     {
62         if (vaddr >= pos->start && vaddr < pos->end) {
63             return pos;
64         }
65     }
66     return NULL;
67 }