[rtems commit] rtems: Move rtems_cache_aligned_malloc()

Sebastian Huber sebh at rtems.org
Tue Nov 25 15:19:51 UTC 2014


Module:    rtems
Branch:    master
Commit:    7e5c9b895e52c76376386e54a0008c3a9e4a1698
Changeset: http://git.rtems.org/rtems/commit/?id=7e5c9b895e52c76376386e54a0008c3a9e4a1698

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Tue Nov 25 14:58:13 2014 +0100

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.

---

 c/src/lib/libcpu/Makefile.am                       |  1 -
 c/src/lib/libcpu/bfin/Makefile.am                  |  2 +-
 c/src/lib/libcpu/i386/Makefile.am                  |  2 +-
 c/src/lib/libcpu/lm32/Makefile.am                  |  1 -
 c/src/lib/libcpu/m68k/Makefile.am                  |  1 -
 c/src/lib/libcpu/mips/Makefile.am                  |  2 +-
 c/src/lib/libcpu/nios2/Makefile.am                 |  1 -
 c/src/lib/libcpu/powerpc/Makefile.am               |  2 +-
 c/src/lib/libcpu/sh/Makefile.am                    |  3 +-
 c/src/lib/libcpu/shared/src/cache_aligned_malloc.c | 43 ----------------------
 c/src/lib/libcpu/sparc/Makefile.am                 |  2 +-
 c/src/lib/libcpu/sparc64/Makefile.am               |  2 +-
 cpukit/libcsupport/Makefile.am                     |  1 +
 cpukit/libcsupport/src/cachealignedalloc.c         | 28 ++++++++++++++
 testsuites/sptests/spcache01/init.c                | 31 ++++++++++++++++
 testsuites/sptests/spcache01/spcache01.doc         |  1 +
 testsuites/sptests/spcache01/spcache01.scn         |  1 +
 17 files changed, 69 insertions(+), 55 deletions(-)

diff --git a/c/src/lib/libcpu/Makefile.am b/c/src/lib/libcpu/Makefile.am
index 7d3a43e..630f525 100644
--- a/c/src/lib/libcpu/Makefile.am
+++ b/c/src/lib/libcpu/Makefile.am
@@ -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@
diff --git a/c/src/lib/libcpu/bfin/Makefile.am b/c/src/lib/libcpu/bfin/Makefile.am
index 06e2315..cc0b70b 100644
--- a/c/src/lib/libcpu/bfin/Makefile.am
+++ b/c/src/lib/libcpu/bfin/Makefile.am
@@ -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)
diff --git a/c/src/lib/libcpu/i386/Makefile.am b/c/src/lib/libcpu/i386/Makefile.am
index 0330ba3..0869435 100644
--- a/c/src/lib/libcpu/i386/Makefile.am
+++ b/c/src/lib/libcpu/i386/Makefile.am
@@ -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)
diff --git a/c/src/lib/libcpu/lm32/Makefile.am b/c/src/lib/libcpu/lm32/Makefile.am
index f51d0eb..e571885 100644
--- a/c/src/lib/libcpu/lm32/Makefile.am
+++ b/c/src/lib/libcpu/lm32/Makefile.am
@@ -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)
diff --git a/c/src/lib/libcpu/m68k/Makefile.am b/c/src/lib/libcpu/m68k/Makefile.am
index 19f500f..45d7634 100644
--- a/c/src/lib/libcpu/m68k/Makefile.am
+++ b/c/src/lib/libcpu/m68k/Makefile.am
@@ -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)
diff --git a/c/src/lib/libcpu/mips/Makefile.am b/c/src/lib/libcpu/mips/Makefile.am
index be4d2c0..b261afe 100644
--- a/c/src/lib/libcpu/mips/Makefile.am
+++ b/c/src/lib/libcpu/mips/Makefile.am
@@ -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)
diff --git a/c/src/lib/libcpu/nios2/Makefile.am b/c/src/lib/libcpu/nios2/Makefile.am
index 233b5a1..e249998 100644
--- a/c/src/lib/libcpu/nios2/Makefile.am
+++ b/c/src/lib/libcpu/nios2/Makefile.am
@@ -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)
diff --git a/c/src/lib/libcpu/powerpc/Makefile.am b/c/src/lib/libcpu/powerpc/Makefile.am
index 5b83288..f7d7e64 100644
--- a/c/src/lib/libcpu/powerpc/Makefile.am
+++ b/c/src/lib/libcpu/powerpc/Makefile.am
@@ -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)
diff --git a/c/src/lib/libcpu/sh/Makefile.am b/c/src/lib/libcpu/sh/Makefile.am
index 532597f..5c0a6ed 100644
--- a/c/src/lib/libcpu/sh/Makefile.am
+++ b/c/src/lib/libcpu/sh/Makefile.am
@@ -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)
 
diff --git a/c/src/lib/libcpu/shared/src/cache_aligned_malloc.c b/c/src/lib/libcpu/shared/src/cache_aligned_malloc.c
deleted file mode 100644
index 17fc468..0000000
--- a/c/src/lib/libcpu/shared/src/cache_aligned_malloc.c
+++ /dev/null
@@ -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
-}
diff --git a/c/src/lib/libcpu/sparc/Makefile.am b/c/src/lib/libcpu/sparc/Makefile.am
index 9f8bacb..ceed3bc 100644
--- a/c/src/lib/libcpu/sparc/Makefile.am
+++ b/c/src/lib/libcpu/sparc/Makefile.am
@@ -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)
 
diff --git a/c/src/lib/libcpu/sparc64/Makefile.am b/c/src/lib/libcpu/sparc64/Makefile.am
index a95672e..b0a1f84 100644
--- a/c/src/lib/libcpu/sparc64/Makefile.am
+++ b/c/src/lib/libcpu/sparc64/Makefile.am
@@ -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
diff --git a/cpukit/libcsupport/Makefile.am b/cpukit/libcsupport/Makefile.am
index bd3f90a..95c85c4 100644
--- a/cpukit/libcsupport/Makefile.am
+++ b/cpukit/libcsupport/Makefile.am
@@ -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
diff --git a/cpukit/libcsupport/src/cachealignedalloc.c b/cpukit/libcsupport/src/cachealignedalloc.c
new file mode 100644
index 0000000..a704859
--- /dev/null
+++ b/cpukit/libcsupport/src/cachealignedalloc.c
@@ -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 );
+}
diff --git a/testsuites/sptests/spcache01/init.c b/testsuites/sptests/spcache01/init.c
index e303436..95777e1 100644
--- a/testsuites/sptests/spcache01/init.c
+++ b/testsuites/sptests/spcache01/init.c
@@ -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();
 
diff --git a/testsuites/sptests/spcache01/spcache01.doc b/testsuites/sptests/spcache01/spcache01.doc
index bbc8f25..04e32a1 100644
--- a/testsuites/sptests/spcache01/spcache01.doc
+++ b/testsuites/sptests/spcache01/spcache01.doc
@@ -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:
 
diff --git a/testsuites/sptests/spcache01/spcache01.scn b/testsuites/sptests/spcache01/spcache01.scn
index 8013945..4e7d53a 100644
--- a/testsuites/sptests/spcache01/spcache01.scn
+++ b/testsuites/sptests/spcache01/spcache01.scn
@@ -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 ***



More information about the vc mailing list