source: rtems-libbsd/rtemsbsd/rtems/rtems-kernel-chunk.c

6-freebsd-12
Last change on this file was 0237319, checked in by Sebastian Huber <sebastian.huber@…>, on 05/23/17 at 11:18:31

Update due to Newlib 2017-06-07 changes

The following files are now provided by Newlib:

  • arpa/inet.h
  • net/if.h
  • netinet/in.h
  • netinet/tcp.h
  • sys/socket.h
  • sys/uio.h
  • sys/un.h

The <sys/param.h> and <sys/cpuset.h> are now compatible enough to be
used directly.

Update #2833.

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