Reduced boot time
[akaros.git] / kern / include / colored_page_alloc.h
1 /*
2  * Copyright (c) 2009 The Regents of the University  of California.  
3  * See the COPYRIGHT files at the top of this source tree for full 
4  * license information.
5  */
6 /**
7  * @author Kevin Klues <klueska@cs.berkeley.edu>
8  */
9  
10 #ifndef ROS_KERN_COLORED_PAGE_ALLOC_H
11 #define ROS_KERN_COLORED_PAGE_ALLOC_H
12
13 #include <colored_caches.h>
14 #include <arch/colored_page_alloc.h>
15 #include <stdio.h>
16         
17 #define DECLARE_CACHE_COLORED_PAGE_LINK(_cache)                               \
18         page_list_entry_t _cache##_cache_colored_page_link;
19         
20 #define DECLARE_CACHE_COLORED_PAGE_FREE_LIST(_cache)                          \
21         uint8_t _cache##_num_colors = 0;                                          \
22         page_list_t *COUNT(_cache##_num_colors) _cache##_cache_colored_page_list = NULL;
23         
24 #define DECLARE_EXTERN_CACHE_COLORED_PAGE_FREE_LIST(_cache)                   \
25     extern uint8_t _cache##_num_colors;                                       \
26         extern page_list_t *COUNT(_cache##_num_colors) _cache##_cache_colored_page_list;
27         
28 #define DECLARE_CACHE_COLORED_PAGE_ALLOC_FUNCTION(_cache)                     \
29 error_t _cache##_page_alloc(page_t** page, size_t color)                      \
30 {                                                                             \
31         /*      TODO: Put a lock around this */                                       \
32         if(available_caches._cache && !LIST_EMPTY(&(_cache##_cache_colored_page_list)[(color)])) {           \
33                 *(page) = LIST_FIRST(&(_cache##_cache_colored_page_list)[(color)]);   \
34                 LIST_REMOVE(*page, global_link);                                      \
35                 REMOVE_CACHE_COLORING_PAGE_FROM_FREE_LISTS(page);                     \
36                 page_clear(*page);                                                    \
37                 return ESUCCESS;                                                      \
38         }                                                                         \
39         return -ENOMEM;                                                           \
40 }
41
42 #define INIT_CACHE_COLORED_PAGE_FREE_LIST(_cache)                             \
43 {                                                                             \
44         if(available_caches._cache == TRUE) {                                     \
45             _cache##_num_colors = get_cache_num_page_colors(&(_cache));           \
46             assert(((_cache##_num_colors) & ((_cache##_num_colors)-1)) == 0);     \
47             size_t list_size = _cache##_num_colors*sizeof(page_list_t);           \
48             _cache##_cache_colored_page_list                                      \
49                = (page_list_t*) boot_alloc(list_size, PGSIZE);                    \
50                 for(int i=0; i<_cache##_num_colors; i++) {                            \
51                         LIST_INIT(&(_cache##_cache_colored_page_list[i]));                \
52                 }                                                                     \
53         }                                                                         \
54 }
55
56 #define REMOVE_CACHE_COLORING_PAGE_FROM_FREE_LIST(_page, _cache)              \
57         if(available_caches._cache == TRUE)                                       \
58                 LIST_REMOVE(*(_page), _cache##_cache_colored_page_link);
59
60
61 #define INSERT_CACHE_COLORING_PAGE_ONTO_FREE_LIST(_page, _cache)              \
62         if(available_caches._cache == TRUE) {                                     \
63                 LIST_INSERT_HEAD(                                                     \
64                    &(_cache##_cache_colored_page_list                                 \
65                          [get_page_color(page2ppn((_page)), &(_cache))]),             \
66                    (_page),                                                           \
67                    _cache##_cache_colored_page_link                                   \
68                 );                                                                    \
69         }
70
71 #endif // ROS_KERN_COLORED_PAGE_ALLOC_H