source: rtems-libbsd/rtemsbsd/rtems/rtems-bsd-page.c @ 5383ed4

4.115-freebsd-12freebsd-9.3
Last change on this file since 5383ed4 was 5383ed4, checked in by Sebastian Huber <sebastian.huber@…>, on Feb 13, 2015 at 9:31:27 AM

Add and use rtems_bsd_get_allocator_domain_size()

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup rtems_bsd_rtems
5 *
6 * @brief TODO.
7 */
8
9/*
10 * Copyright (c) 2015 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Dornierstr. 4
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
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 <machine/rtems-bsd-kernel-space.h>
41#include <machine/rtems-bsd-page.h>
42
43#include <rtems/bsd/sys/param.h>
44#include <rtems/bsd/sys/types.h>
45#include <rtems/bsd/sys/lock.h>
46#include <sys/mutex.h>
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <vm/uma.h>
50
51#include <stdlib.h>
52
53#include <rtems/bsd/bsd.h>
54#include <rtems/malloc.h>
55#include <rtems/rbheap.h>
56
57void **rtems_bsd_page_object_table;
58
59uintptr_t rtems_bsd_page_area_begin;
60
61static rtems_rbheap_control page_heap;
62
63struct mtx page_heap_mtx;
64
65void *
66rtems_bsd_page_alloc(uintptr_t size_in_bytes, int wait)
67{
68        void *addr;
69
70        mtx_lock(&page_heap_mtx);
71
72        addr = rtems_rbheap_allocate(&page_heap, size_in_bytes);
73        if (addr == NULL && wait) {
74                int i;
75
76                for (i = 0; i < 8; i++) {
77                        mtx_unlock(&page_heap_mtx);
78                        uma_reclaim();
79                        mtx_lock(&page_heap_mtx);
80
81                        addr = rtems_rbheap_allocate(&page_heap, size_in_bytes);
82                        if (addr != NULL)
83                                break;
84
85                        msleep(&page_heap, &page_heap_mtx, 0,
86                            "page alloc", (hz / 4) * (i + 1));
87                }
88
89                if (i == 8) {
90                        panic("rtems_bsd_page_alloc: page starvation");
91                }
92        }
93
94        mtx_unlock(&page_heap_mtx);
95
96        return (addr);
97}
98
99void
100rtems_bsd_page_free(void *addr)
101{
102        mtx_lock(&page_heap_mtx);
103        rtems_rbheap_free(&page_heap, addr);
104        wakeup(&page_heap);
105        mtx_unlock(&page_heap_mtx);
106}
107
108static void
109rtems_bsd_page_init(void *arg)
110{
111        rtems_status_code sc;
112        void *area;
113        void **obj_table;
114        rtems_rbheap_chunk *chunks;
115        size_t i;
116        size_t n;
117        uintptr_t heap_size;
118
119        mtx_init(&page_heap_mtx, "page heap", NULL, MTX_DEF);
120
121        heap_size = rtems_bsd_get_allocator_domain_size(
122            RTEMS_BSD_ALLOCATOR_DOMAIN_PAGE);
123
124        area = rtems_heap_allocate_aligned_with_boundary(heap_size, PAGE_SIZE,
125            0);
126        BSD_ASSERT(area != NULL);
127
128        sc = rtems_rbheap_initialize(&page_heap, area, heap_size, PAGE_SIZE,
129            rtems_rbheap_extend_descriptors_with_malloc, NULL);
130        BSD_ASSERT(sc == RTEMS_SUCCESSFUL);
131
132        rtems_rbheap_set_extend_descriptors(&page_heap,
133            rtems_rbheap_extend_descriptors_never);
134
135        n = heap_size / PAGE_SIZE;
136
137        chunks = malloc(n * sizeof(*chunks), M_RTEMS_HEAP, M_NOWAIT);
138        BSD_ASSERT(chunks != NULL);
139
140        for (i = 0; i < n; ++i) {
141                rtems_rbheap_add_to_spare_descriptor_chain(&page_heap,
142                    &chunks[i]);
143        }
144
145        obj_table = calloc(n, sizeof(*obj_table));
146
147        rtems_bsd_page_area_begin = (uintptr_t)area;
148        rtems_bsd_page_object_table = obj_table;
149}
150
151SYSINIT(rtems_bsd_page, SI_SUB_VM, SI_ORDER_FIRST, rtems_bsd_page_init, NULL);
Note: See TracBrowser for help on using the repository browser.