source: rtems-libbsd/rtemsbsd/rtems/rtems-bsd-chunk.c @ f244de9

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since f244de9 was f244de9, checked in by Sebastian Huber <sebastian.huber@…>, on 11/06/13 at 07:56:38

Rename rtems-bsd-config.h

Rename rtems-bsd-config.h in rtems-bsd-kernel-space.h.

  • Property mode set to 100644
File size: 3.8 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup rtems_bsd_rtems
5 *
6 * @brief TODO.
7 */
8
9/*
10 * Copyright (c) 2013 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-chunk.h>
42
43#include <sys/malloc.h>
44
45#include <rtems/score/apimutex.h>
46
47#define chunk_of_node(n) ((rtems_bsd_chunk_info *) n)
48
49static int
50chunk_compare(const rtems_rbtree_node *a, const rtems_rbtree_node *b)
51{
52        const rtems_bsd_chunk_info *left = chunk_of_node(a);
53        const rtems_bsd_chunk_info *right = chunk_of_node(b);
54
55        if (left->begin < right->begin) {
56                return -1;
57        } else if (left->begin < right->end) {
58                return 0;
59        } else {
60                return 1;
61        }
62}
63
64void
65rtems_bsd_chunk_init(rtems_bsd_chunk_control *self, uintptr_t info_size,
66    rtems_bsd_chunk_info_ctor info_ctor, rtems_bsd_chunk_info_dtor info_dtor)
67{
68        self->info_size = info_size;
69        self->info_ctor = info_ctor;
70        self->info_dtor = info_dtor;
71        rtems_rbtree_initialize_empty(&self->chunks, chunk_compare, true);
72}
73
74void *
75rtems_bsd_chunk_alloc(rtems_bsd_chunk_control *self, uintptr_t chunk_size)
76{
77        char *p = malloc(chunk_size + self->info_size, M_TEMP, M_WAITOK);
78
79        if (p != NULL) {
80                rtems_bsd_chunk_info *info = (rtems_bsd_chunk_info *) p;
81
82                p += self->info_size;
83
84                info->begin = (uintptr_t) p;
85                info->end = (uintptr_t) p + chunk_size;
86
87                (*self->info_ctor)(self, info);
88
89                _RTEMS_Lock_allocator();
90                rtems_rbtree_insert_unprotected(&self->chunks, &info->node);
91                _RTEMS_Unlock_allocator();
92        }
93
94        return p;
95}
96
97void
98rtems_bsd_chunk_free(rtems_bsd_chunk_control *self,
99    void *some_addr_in_chunk)
100{
101        rtems_bsd_chunk_info *info = rtems_bsd_chunk_get_info(self,
102            some_addr_in_chunk);
103
104        _RTEMS_Lock_allocator();
105        rtems_rbtree_extract_unprotected(&self->chunks, &info->node);
106        _RTEMS_Unlock_allocator();
107
108        (*self->info_dtor)(self, info);
109
110        free(info, M_TEMP);
111}
112
113rtems_bsd_chunk_info *
114rtems_bsd_chunk_get_info(rtems_bsd_chunk_control *self,
115    void *some_addr_in_chunk)
116{
117        rtems_bsd_chunk_info find_me = {
118                .begin = (uintptr_t) some_addr_in_chunk
119        };
120
121        return chunk_of_node(rtems_rbtree_find_unprotected(&self->chunks,
122            &find_me.node));
123}
124
125void *
126rtems_bsd_chunk_get_begin(rtems_bsd_chunk_control *self,
127    void *some_addr_in_chunk)
128{
129        rtems_bsd_chunk_info *info = rtems_bsd_chunk_get_info(self,
130            some_addr_in_chunk);
131
132        return (void *) info->begin;
133}
134
135void
136rtems_bsd_chunk_info_dtor_default(rtems_bsd_chunk_control *self,
137    rtems_bsd_chunk_info *info)
138{
139        (void) self;
140        (void) info;
141}
Note: See TracBrowser for help on using the repository browser.