1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup rtems_bsd_rtems |
---|
5 | * |
---|
6 | * @brief FreeBSD uma source used pages which where there was an |
---|
7 | * assumption of page alignment. Doing this alignment would waste |
---|
8 | * more memory than we were willing to do. Therefore, a set of |
---|
9 | * rtems-bsd-page routines track the allocation of pages and the |
---|
10 | * small sections of source in the uma source were modified to use |
---|
11 | * these methods on an rtems system. |
---|
12 | */ |
---|
13 | |
---|
14 | /* |
---|
15 | * COPYRIGHT (c) 2012. On-Line Applications Research Corporation (OAR). |
---|
16 | * All rights reserved. |
---|
17 | * |
---|
18 | * Redistribution and use in source and binary forms, with or without |
---|
19 | * modification, are permitted provided that the following conditions |
---|
20 | * are met: |
---|
21 | * 1. Redistributions of source code must retain the above copyright |
---|
22 | * notice, this list of conditions and the following disclaimer. |
---|
23 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
24 | * notice, this list of conditions and the following disclaimer in the |
---|
25 | * documentation and/or other materials provided with the distribution. |
---|
26 | * |
---|
27 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
30 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
33 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
34 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
35 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
36 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
37 | * SUCH DAMAGE. |
---|
38 | */ |
---|
39 | |
---|
40 | #include <freebsd/machine/rtems-bsd-config.h> |
---|
41 | |
---|
42 | #include <freebsd/sys/types.h> |
---|
43 | #include <freebsd/sys/systm.h> |
---|
44 | #include <freebsd/sys/malloc.h> |
---|
45 | #include <rtems/chain.h> |
---|
46 | |
---|
47 | |
---|
48 | #define RTEMS_PAGE_COUNT 100 |
---|
49 | |
---|
50 | typedef struct { |
---|
51 | rtems_chain_node node; |
---|
52 | void *page; |
---|
53 | void *end; |
---|
54 | |
---|
55 | } rtems_page_t; |
---|
56 | |
---|
57 | // chain of pages that have been allocated |
---|
58 | static rtems_chain_control rtems_page_list = RTEMS_CHAIN_INITIALIZER_EMPTY( rtems_page_list ); |
---|
59 | |
---|
60 | void * |
---|
61 | rtems_page_alloc(int bytes) |
---|
62 | { |
---|
63 | static void *p; |
---|
64 | static rtems_page_t *page; |
---|
65 | |
---|
66 | bytes = round_page(bytes); |
---|
67 | p = (void *) malloc(bytes, M_TEMP, M_NOWAIT | M_ZERO); |
---|
68 | |
---|
69 | page = (rtems_page_t *) malloc(sizeof(rtems_page_t), M_TEMP, M_NOWAIT | M_ZERO); |
---|
70 | page->page = p; |
---|
71 | page->end = p + bytes; |
---|
72 | |
---|
73 | rtems_chain_append( &rtems_page_list, page ); |
---|
74 | |
---|
75 | return p; |
---|
76 | } |
---|
77 | |
---|
78 | rtems_page_t *rtems_page_t_find( void *address ) |
---|
79 | { |
---|
80 | rtems_chain_node *the_node; |
---|
81 | rtems_page_t *the_page = NULL; |
---|
82 | |
---|
83 | for (the_node = rtems_chain_first( &rtems_page_list ); |
---|
84 | !rtems_chain_is_tail(&rtems_page_list, the_node); |
---|
85 | the_node = rtems_chain_next(the_node)) { |
---|
86 | the_page = the_node; |
---|
87 | |
---|
88 | if ((address >= the_page->page) && |
---|
89 | (address <= the_page->end)) |
---|
90 | return the_page; |
---|
91 | } |
---|
92 | return NULL; |
---|
93 | } |
---|
94 | |
---|
95 | void *rtems_page_find( void *address ) |
---|
96 | { |
---|
97 | rtems_page_t *ptr; |
---|
98 | |
---|
99 | ptr = rtems_page_t_find( address ); |
---|
100 | |
---|
101 | if (ptr) |
---|
102 | return ptr->page; |
---|
103 | |
---|
104 | return ptr; |
---|
105 | } |
---|
106 | |
---|
107 | void rtems_page_free( void *address ) |
---|
108 | { |
---|
109 | rtems_page_t *ptr; |
---|
110 | |
---|
111 | ptr = rtems_page_t_find( address ); |
---|
112 | KASSERT(ptr != NULL, ("Unable to locate page for freed element")); |
---|
113 | |
---|
114 | free( ptr->page, M_TEMP ); |
---|
115 | free( ptr, M_TEMP ); |
---|
116 | } |
---|