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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 821ae67 was 821ae67, checked in by Sebastian Huber <sebastian.huber@…>, on 01/30/14 at 14:59:15

ZONE(9): Make sure the chunks are properly aligned

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