forked from Imagelibrary/rtems
rtems: Move rtems_cache_aligned_malloc()
Make sure also the size is cache aligned since otherwise we may have some overlap with the next allocation block. A cache invalidate on this area would be fatal.
This commit is contained in:
@@ -2,7 +2,6 @@ include_libcpudir = $(includedir)/libcpu
|
||||
|
||||
EXTRA_DIST =
|
||||
EXTRA_DIST += shared/include/cache.h
|
||||
EXTRA_DIST += shared/src/cache_aligned_malloc.c
|
||||
EXTRA_DIST += shared/src/cache_manager.c
|
||||
|
||||
SUBDIRS = @libcpu_cpu_subdir@
|
||||
|
||||
@@ -54,7 +54,7 @@ include_libcpu_HEADERS += include/gpioRegs.h
|
||||
|
||||
include_libcpu_HEADERS += ../shared/include/cache.h
|
||||
noinst_PROGRAMS += cache.rel
|
||||
cache_rel_SOURCES = cache/cache.c ../shared/src/cache_aligned_malloc.c \
|
||||
cache_rel_SOURCES = cache/cache.c \
|
||||
../shared/src/cache_manager.c cache/cache_.h
|
||||
cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/cache
|
||||
cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS)
|
||||
|
||||
@@ -11,7 +11,7 @@ include_libcpu_HEADERS += ../shared/include/cache.h
|
||||
include_libcpu_HEADERS += byteorder.h
|
||||
|
||||
noinst_PROGRAMS += cache.rel
|
||||
cache_rel_SOURCES = cache.c cache_.h ../shared/src/cache_aligned_malloc.c \
|
||||
cache_rel_SOURCES = cache.c cache_.h \
|
||||
../shared/src/cache_manager.c ../shared/include/cache.h
|
||||
cache_rel_CPPFLAGS = $(AM_CPPFLAGS)
|
||||
cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS)
|
||||
|
||||
@@ -13,7 +13,6 @@ include_libcpudir = $(includedir)/libcpu
|
||||
include_libcpu_HEADERS = ../shared/include/cache.h
|
||||
noinst_PROGRAMS += shared/cache.rel
|
||||
shared_cache_rel_SOURCES = ../shared/src/no_cache.c shared/cache/cache_.h \
|
||||
../shared/src/cache_aligned_malloc.c \
|
||||
../shared/src/cache_manager.c
|
||||
shared_cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/shared/cache
|
||||
shared_cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS)
|
||||
|
||||
@@ -13,7 +13,6 @@ include_libcpu_HEADERS = ../shared/include/cache.h
|
||||
noinst_PROGRAMS += shared/cache.rel
|
||||
shared_cache_rel_SOURCES = shared/cache/cache.c
|
||||
shared_cache_rel_SOURCES += shared/cache/cache_.h
|
||||
shared_cache_rel_SOURCES += ../shared/src/cache_aligned_malloc.c
|
||||
shared_cache_rel_SOURCES += ../shared/src/cache_manager.c
|
||||
shared_cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/shared/cache
|
||||
shared_cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS)
|
||||
|
||||
@@ -12,7 +12,7 @@ include_libcpudir = $(includedir)/libcpu
|
||||
include_libcpu_HEADERS = ../shared/include/cache.h
|
||||
|
||||
noinst_PROGRAMS += shared/cache.rel
|
||||
shared_cache_rel_SOURCES = shared/cache/cache.c ../shared/src/cache_aligned_malloc.c \
|
||||
shared_cache_rel_SOURCES = shared/cache/cache.c \
|
||||
../shared/src/cache_manager.c shared/cache/cache_.h
|
||||
shared_cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/shared/cache
|
||||
shared_cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS)
|
||||
|
||||
@@ -13,7 +13,6 @@ include_libcpudir = $(includedir)/libcpu
|
||||
include_libcpu_HEADERS = ../shared/include/cache.h
|
||||
noinst_PROGRAMS += shared/cache.rel
|
||||
shared_cache_rel_SOURCES = ../shared/src/no_cache.c shared/cache/cache_.h \
|
||||
../shared/src/cache_aligned_malloc.c \
|
||||
../shared/src/cache_manager.c
|
||||
shared_cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/shared/cache
|
||||
shared_cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS)
|
||||
|
||||
@@ -72,7 +72,7 @@ include_libcpu_HEADERS += shared/include/cpuIdent.h
|
||||
# shared/cache
|
||||
noinst_PROGRAMS += shared/cache.rel
|
||||
shared_cache_rel_SOURCES = shared/src/cache_.h \
|
||||
../shared/src/cache_aligned_malloc.c ../shared/src/cache_manager.c \
|
||||
../shared/src/cache_manager.c \
|
||||
../shared/include/cache.h
|
||||
shared_cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/shared/src
|
||||
shared_cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS)
|
||||
|
||||
@@ -8,8 +8,7 @@ include_libcpudir = $(includedir)/libcpu
|
||||
include_libcpu_HEADERS = ../shared/include/cache.h
|
||||
|
||||
noinst_PROGRAMS += cache.rel
|
||||
cache_rel_SOURCES = ../shared/src/cache_aligned_malloc.c
|
||||
cache_rel_SOURCES += ../shared/src/cache_manager.c
|
||||
cache_rel_SOURCES = ../shared/src/cache_manager.c
|
||||
cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/cache
|
||||
cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS)
|
||||
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* RTEMS Cache Aligned Malloc
|
||||
*
|
||||
*
|
||||
* COPYRIGHT (c) 1989-1999.
|
||||
* On-Line Applications Research Corporation (OAR).
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rtems.org/license/LICENSE.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <rtems.h>
|
||||
#include <cache_.h>
|
||||
#include <rtems/rtems/cache.h>
|
||||
|
||||
/*
|
||||
* rtems_cache_aligned_malloc
|
||||
*
|
||||
* DESCRIPTION:
|
||||
*
|
||||
* This function is used to allocate storage that spans an
|
||||
* integral number of cache blocks.
|
||||
*/
|
||||
|
||||
void *rtems_cache_aligned_malloc (
|
||||
size_t nbytes
|
||||
)
|
||||
{
|
||||
/*
|
||||
* Arrange to have the user storage start on the first cache
|
||||
* block beyond the header.
|
||||
*/
|
||||
#if defined(CPU_DATA_CACHE_ALIGNMENT)
|
||||
return (void *) ((((unsigned long)
|
||||
malloc( nbytes + CPU_DATA_CACHE_ALIGNMENT - 1 ))
|
||||
+ CPU_DATA_CACHE_ALIGNMENT - 1 ) &(~(CPU_DATA_CACHE_ALIGNMENT - 1)) );
|
||||
#else
|
||||
return malloc( nbytes );
|
||||
#endif
|
||||
}
|
||||
@@ -11,7 +11,7 @@ include_libcpu_HEADERS += include/libcpu/access.h
|
||||
|
||||
noinst_PROGRAMS += cache.rel
|
||||
cache_rel_SOURCES = cache/cache.c cache/cache_.h \
|
||||
../shared/src/cache_aligned_malloc.c ../shared/src/cache_manager.c
|
||||
../shared/src/cache_manager.c
|
||||
cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/cache
|
||||
cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS)
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ shared_sparc64_syscall_rel_LDFLAGS = $(RTEMS_RELLDFLAGS)
|
||||
|
||||
noinst_PROGRAMS += shared/cache.rel
|
||||
shared_cache_rel_SOURCES = shared/cache/cache.c shared/cache/cache_.h \
|
||||
../shared/src/cache_aligned_malloc.c ../shared/src/cache_manager.c
|
||||
../shared/src/cache_manager.c
|
||||
shared_cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/shared/cache
|
||||
shared_cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS)
|
||||
endif
|
||||
|
||||
@@ -108,6 +108,7 @@ MALLOC_C_FILES = src/malloc_initialize.c src/calloc.c src/malloc.c \
|
||||
src/rtems_heap_null_extend.c \
|
||||
src/rtems_heap_extend.c \
|
||||
src/rtems_heap_greedy.c
|
||||
MALLOC_C_FILES += src/cachealignedalloc.c
|
||||
|
||||
PASSWORD_GROUP_C_FILES = src/pwdgrp.c
|
||||
PASSWORD_GROUP_C_FILES += src/getgrent.c
|
||||
|
||||
28
cpukit/libcsupport/src/cachealignedalloc.c
Normal file
28
cpukit/libcsupport/src/cachealignedalloc.c
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* RTEMS Cache Aligned Malloc
|
||||
*
|
||||
*
|
||||
* COPYRIGHT (c) 1989-1999.
|
||||
* On-Line Applications Research Corporation (OAR).
|
||||
*
|
||||
* The license and distribution terms for this file may be
|
||||
* found in the file LICENSE in this distribution or at
|
||||
* http://www.rtems.org/license/LICENSE.
|
||||
*/
|
||||
|
||||
#include <rtems.h>
|
||||
#include <rtems/malloc.h>
|
||||
|
||||
void *rtems_cache_aligned_malloc( size_t nbytes )
|
||||
{
|
||||
size_t line_size = rtems_cache_get_data_line_size();
|
||||
|
||||
if ( line_size > 0 ) {
|
||||
/* Assume that the cache line size is a power of two */
|
||||
size_t m = line_size - 1;
|
||||
|
||||
nbytes = (nbytes + m) & ~m;
|
||||
}
|
||||
|
||||
return rtems_heap_allocate_aligned_with_boundary( nbytes, line_size, 0 );
|
||||
}
|
||||
@@ -17,6 +17,7 @@
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <rtems.h>
|
||||
@@ -379,12 +380,42 @@ static void test_timing(void)
|
||||
rtems_interrupt_lock_destroy(&lock);
|
||||
}
|
||||
|
||||
static void test_cache_aligned_alloc(void)
|
||||
{
|
||||
void *p0;
|
||||
void *p1;
|
||||
size_t cls;
|
||||
|
||||
printf("test rtems_cache_aligned_malloc()\n");
|
||||
|
||||
p0 = rtems_cache_aligned_malloc(1);
|
||||
p1 = rtems_cache_aligned_malloc(1);
|
||||
|
||||
rtems_test_assert(p0 != NULL);
|
||||
rtems_test_assert(p1 != NULL);
|
||||
|
||||
cls = rtems_cache_get_data_line_size();
|
||||
if (cls > 0) {
|
||||
size_t m = cls - 1;
|
||||
uintptr_t a0 = (uintptr_t) p0;
|
||||
uintptr_t a1 = (uintptr_t) p1;
|
||||
|
||||
rtems_test_assert(a1 - a0 > cls);
|
||||
rtems_test_assert((a0 & m) == 0);
|
||||
rtems_test_assert((a1 & m) == 0);
|
||||
}
|
||||
|
||||
free(p0);
|
||||
free(p1);
|
||||
}
|
||||
|
||||
static void Init(rtems_task_argument arg)
|
||||
{
|
||||
TEST_BEGIN();
|
||||
|
||||
test_data_flush_and_invalidate();
|
||||
test_timing();
|
||||
test_cache_aligned_alloc();
|
||||
|
||||
TEST_END();
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ directives:
|
||||
- rtems_cache_invalidate_entire_instruction()
|
||||
- rtems_cache_invalidate_multiple_data_lines()
|
||||
- rtems_cache_invalidate_multiple_instruction_lines()
|
||||
- rtems_cache_aligned_malloc()
|
||||
|
||||
concepts:
|
||||
|
||||
|
||||
@@ -42,4 +42,5 @@ invalidate multiple instruction
|
||||
duration with normal cache 680 ns
|
||||
duration with warm cache 640 ns
|
||||
duration with invalidated cache 2600 ns
|
||||
test rtems_cache_aligned_malloc()
|
||||
*** END OF TEST SPCACHE 1 ***
|
||||
|
||||
Reference in New Issue
Block a user