ZONE(9): Add and use red-black tree chunk alloc

This commit is contained in:
Sebastian Huber
2013-10-25 15:09:17 +02:00
parent 4adeb59b18
commit 3e2938873d
8 changed files with 292 additions and 139 deletions

View File

@@ -0,0 +1,97 @@
/**
* @file
*
* @ingroup rtems_bsd_machine
*
* @brief TODO.
*/
/*
* Copyright (c) 2013 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Dornierstr. 4
* 82178 Puchheim
* Germany
* <rtems@embedded-brains.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _RTEMS_BSD_MACHINE_RTEMS_BSD_CHUNK_H_
#define _RTEMS_BSD_MACHINE_RTEMS_BSD_CHUNK_H_
/*
* A chunk is a fixed size memory area with some meta information attached to
* it. This API is used by ZONE(9).
*/
#include <sys/cdefs.h>
#include <rtems/rbtree.h>
__BEGIN_DECLS
typedef struct rtems_bsd_chunk_info rtems_bsd_chunk_info;
typedef struct rtems_bsd_chunk_control rtems_bsd_chunk_control;
typedef void (*rtems_bsd_chunk_info_ctor)(rtems_bsd_chunk_control *self,
rtems_bsd_chunk_info *info);
typedef void (*rtems_bsd_chunk_info_dtor)(rtems_bsd_chunk_control *self,
rtems_bsd_chunk_info *info);
struct rtems_bsd_chunk_info {
rtems_rbtree_node node;
uintptr_t begin;
uintptr_t end;
};
struct rtems_bsd_chunk_control {
rtems_rbtree_control chunks;
uintptr_t info_size;
rtems_bsd_chunk_info_ctor info_ctor;
rtems_bsd_chunk_info_dtor info_dtor;
};
void rtems_bsd_chunk_init(rtems_bsd_chunk_control *self, uintptr_t info_size,
rtems_bsd_chunk_info_ctor info_ctor, rtems_bsd_chunk_info_dtor info_dtor);
void *rtems_bsd_chunk_alloc(rtems_bsd_chunk_control *self,
uintptr_t chunk_size);
void rtems_bsd_chunk_free(rtems_bsd_chunk_control *self,
void *some_addr_in_chunk);
rtems_bsd_chunk_info *rtems_bsd_chunk_get_info(rtems_bsd_chunk_control *self,
void *some_addr_in_chunk);
void *rtems_bsd_chunk_get_begin(rtems_bsd_chunk_control *self,
void *some_addr_in_chunk);
void rtems_bsd_chunk_info_dtor_default(rtems_bsd_chunk_control *self,
rtems_bsd_chunk_info *info);
__END_DECLS
#endif /* _RTEMS_BSD_MACHINE_RTEMS_BSD_CHUNK_H_ */

View File

@@ -0,0 +1,141 @@
/**
* @file
*
* @ingroup rtems_bsd_rtems
*
* @brief TODO.
*/
/*
* Copyright (c) 2013 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Dornierstr. 4
* 82178 Puchheim
* Germany
* <rtems@embedded-brains.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <machine/rtems-bsd-config.h>
#include <machine/rtems-bsd-chunk.h>
#include <sys/malloc.h>
#include <rtems/score/apimutex.h>
#define chunk_of_node(n) ((rtems_bsd_chunk_info *) n)
static int
chunk_compare(const rtems_rbtree_node *a, const rtems_rbtree_node *b)
{
const rtems_bsd_chunk_info *left = chunk_of_node(a);
const rtems_bsd_chunk_info *right = chunk_of_node(b);
if (left->begin < right->begin) {
return -1;
} else if (left->begin < right->end) {
return 0;
} else {
return 1;
}
}
void
rtems_bsd_chunk_init(rtems_bsd_chunk_control *self, uintptr_t info_size,
rtems_bsd_chunk_info_ctor info_ctor, rtems_bsd_chunk_info_dtor info_dtor)
{
self->info_size = info_size;
self->info_ctor = info_ctor;
self->info_dtor = info_dtor;
rtems_rbtree_initialize_empty(&self->chunks, chunk_compare, true);
}
void *
rtems_bsd_chunk_alloc(rtems_bsd_chunk_control *self, uintptr_t chunk_size)
{
char *p = malloc(chunk_size + self->info_size, M_TEMP, M_WAITOK);
if (p != NULL) {
rtems_bsd_chunk_info *info = (rtems_bsd_chunk_info *) p;
p += self->info_size;
info->begin = (uintptr_t) p;
info->end = (uintptr_t) p + chunk_size;
(*self->info_ctor)(self, info);
_RTEMS_Lock_allocator();
rtems_rbtree_insert_unprotected(&self->chunks, &info->node);
_RTEMS_Unlock_allocator();
}
return p;
}
void
rtems_bsd_chunk_free(rtems_bsd_chunk_control *self,
void *some_addr_in_chunk)
{
rtems_bsd_chunk_info *info = rtems_bsd_chunk_get_info(self,
some_addr_in_chunk);
_RTEMS_Lock_allocator();
rtems_rbtree_extract_unprotected(&self->chunks, &info->node);
_RTEMS_Unlock_allocator();
(*self->info_dtor)(self, info);
free(info, M_TEMP);
}
rtems_bsd_chunk_info *
rtems_bsd_chunk_get_info(rtems_bsd_chunk_control *self,
void *some_addr_in_chunk)
{
rtems_bsd_chunk_info find_me = {
.begin = (uintptr_t) some_addr_in_chunk
};
return chunk_of_node(rtems_rbtree_find_unprotected(&self->chunks,
&find_me.node));
}
void *
rtems_bsd_chunk_get_begin(rtems_bsd_chunk_control *self,
void *some_addr_in_chunk)
{
rtems_bsd_chunk_info *info = rtems_bsd_chunk_get_info(self,
some_addr_in_chunk);
return (void *) info->begin;
}
void
rtems_bsd_chunk_info_dtor_default(rtems_bsd_chunk_control *self,
rtems_bsd_chunk_info *info)
{
(void) self;
(void) info;
}

View File

@@ -1,116 +0,0 @@
/**
* @file
*
* @ingroup rtems_bsd_rtems
*
* @brief FreeBSD uma source used pages which where there was an
* assumption of page alignment. Doing this alignment would waste
* more memory than we were willing to do. Therefore, a set of
* rtems-bsd-page routines track the allocation of pages and the
* small sections of source in the uma source were modified to use
* these methods on an rtems system.
*/
/*
* COPYRIGHT (c) 2012. On-Line Applications Research Corporation (OAR).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <machine/rtems-bsd-config.h>
#include <rtems/bsd/sys/types.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <rtems/chain.h>
#define RTEMS_PAGE_COUNT 100
typedef struct {
rtems_chain_node node;
void *page;
void *end;
} rtems_page_t;
// chain of pages that have been allocated
static rtems_chain_control rtems_page_list = RTEMS_CHAIN_INITIALIZER_EMPTY( rtems_page_list );
void *
rtems_page_alloc(int bytes)
{
static void *p;
static rtems_page_t *page;
bytes = round_page(bytes);
p = (void *) malloc(bytes, M_TEMP, M_NOWAIT | M_ZERO);
page = (rtems_page_t *) malloc(sizeof(rtems_page_t), M_TEMP, M_NOWAIT | M_ZERO);
page->page = p;
page->end = p + bytes;
rtems_chain_append( &rtems_page_list, page );
return p;
}
rtems_page_t *rtems_page_t_find( void *address )
{
rtems_chain_node *the_node;
rtems_page_t *the_page = NULL;
for (the_node = rtems_chain_first( &rtems_page_list );
!rtems_chain_is_tail(&rtems_page_list, the_node);
the_node = rtems_chain_next(the_node)) {
the_page = the_node;
if ((address >= the_page->page) &&
(address <= the_page->end))
return the_page;
}
return NULL;
}
void *rtems_page_find( void *address )
{
rtems_page_t *ptr;
ptr = rtems_page_t_find( address );
if (ptr)
return ptr->page;
return ptr;
}
void rtems_page_free( void *address )
{
rtems_page_t *ptr;
ptr = rtems_page_t_find( address );
KASSERT(ptr != NULL, ("Unable to locate page for freed element"));
free( ptr->page, M_TEMP );
free( ptr, M_TEMP );
}