summaryrefslogtreecommitdiff
path: root/src/util/Cache.c
diff options
context:
space:
mode:
authorgoodale <goodale@17b73243-c579-4c4c-a9d2-2d5706c11dac>1999-11-30 23:50:31 +0000
committergoodale <goodale@17b73243-c579-4c4c-a9d2-2d5706c11dac>1999-11-30 23:50:31 +0000
commita10e0e2d450d0ed23fdf9d6c0e820995e3756140 (patch)
tree8964c80ce1b12f4ef0a0f376ecde68161bb5f818 /src/util/Cache.c
parent2520ea110b9d159c4644261d27db77b683abaf36 (diff)
New cache alignment stuff.
Tom git-svn-id: http://svn.cactuscode.org/flesh/trunk@1178 17b73243-c579-4c4c-a9d2-2d5706c11dac
Diffstat (limited to 'src/util/Cache.c')
-rw-r--r--src/util/Cache.c226
1 files changed, 226 insertions, 0 deletions
diff --git a/src/util/Cache.c b/src/util/Cache.c
new file mode 100644
index 00000000..42a99a4d
--- /dev/null
+++ b/src/util/Cache.c
@@ -0,0 +1,226 @@
+ /*@@
+ @file Cache.c
+ @date Tue Nov 30 08:07:12 1999
+ @author Tom Goodale
+ @desc
+ Routines dealing with cache alignment.
+ @enddesc
+ @@*/
+
+static char *rcsid = "$Header$";
+
+#include <stdlib.h>
+
+#include "cctk_Cache.h"
+#include "cctki_Cache.h"
+
+/********************************************************************
+ ********************* Local Data *****************************
+ ********************************************************************/
+
+/* Structure to hold cache information. */
+static struct s_cache_data
+{
+ unsigned long cacheline_bytes;
+ unsigned long cache_size;
+} cache_data;
+
+/* The number of times the size of the cache has been set. */
+static int cache_set = 0;
+
+/********************************************************************
+ ********************* External Routines **********************
+ ********************************************************************/
+
+ /*@@
+ @routine CCTK_CacheMalloc
+ @date Tue Nov 30 08:13:03 1999
+ @author Tom Goodale
+ @desc
+ Allocates memory aligned on the 'index'ed cache line.
+ @enddesc
+ @calls
+ @calledby
+ @history
+
+ @endhistory
+
+ @var index
+ @vdesc cache line index
+ @vtype int
+ @vio in
+ @vcomment
+ The cache line to align on
+ @endvar
+ @var size
+ @vdesc the amount of memory to allocate
+ @vtype unsigned long
+ @vio in
+ @vcomment
+ This is the size (in bytes) of memory to allocate.
+ @endvar
+ @var realstart
+ @vdesc The real starting address of the memory
+ @vtype void **
+ @vio out
+ @vcomment
+ On return holds the actual starting address of the memory.
+ @endvar
+
+ @returntype void *
+ @returndesc
+ The cache-aligned memory address.
+ @endreturndesc
+
+@@*/
+void *CCTK_CacheMalloc(unsigned index,
+ unsigned long size,
+ void **realstart)
+{
+ char *retval;
+ char *data;
+ unsigned long cacheline_bytes;
+ unsigned long cache_size;
+ unsigned long offset;
+ unsigned long initial_index;
+ unsigned long pad;
+
+ if(CCTKi_CacheDataGet(&cacheline_bytes, &cache_size))
+ {
+ cacheline_bytes = 0;
+ cache_size = 0;
+ }
+
+ data = malloc(size+cache_size);
+
+ if(data)
+ {
+ if(cache_size)
+ {
+ /* Find how far this memory is into a cache line */
+ offset = ((unsigned long)data%cache_size)%cacheline_bytes;
+ /* Find which cache line in the cache it is in */
+ initial_index = ((unsigned long)data%cache_size)/cacheline_bytes;
+
+ pad = ((index-initial_index)*cacheline_bytes + cacheline_bytes - offset)%cache_size;
+ }
+ else
+ {
+ pad = 0;
+ }
+
+ retval = data + pad;
+ }
+ else
+ {
+ retval = NULL;
+ }
+
+ *realstart = (void *)data;
+
+ return (void *)retval;
+}
+
+/********************************************************************
+ ********************* CCTK Local Routines **********************
+ ********************************************************************/
+
+ /*@@
+ @routine CCTKi_CacheDataSet
+ @date Tue Nov 30 08:12:05 1999
+ @author Tom Goodale
+ @desc
+ Stores the sizes of the cache.
+ @enddesc
+ @calls
+ @calledby
+ @history
+
+ @endhistory
+
+ @var cacheline_bytes
+ @vdesc The size of a cacheline
+ @vtype unsigned long
+ @vio in
+ @vcomment
+ Number of bytes in a cache line.
+ @endvar
+ @var cache_size
+ @vdesc Size of the cache
+ @vtype unsigned long
+ @vio in
+ @vcomment
+ The size (in bytes) of the cache.
+ @endvar
+
+ @returntype int
+ @returndesc
+ The number of times this function has been called before.
+ @endreturndesc
+
+@@*/
+int CCTKi_CacheDataSet(unsigned long cacheline_bytes,
+ unsigned long cache_size)
+{
+ cache_data.cacheline_bytes = cacheline_bytes;
+ cache_data.cache_size = cache_size;
+
+ cache_set++;
+
+ return cache_set-1;
+}
+
+ /*@@
+ @routine CCTKi_CacheDataGet
+ @date Tue Nov 30 08:12:41 1999
+ @author Tom Goodale
+ @desc
+ Gets the sizes of the cache.
+ @enddesc
+ @calls
+ @calledby
+ @history
+
+ @endhistory
+
+ @var cacheline_bytes
+ @vdesc The size of a cacheline
+ @vtype unsigned long *
+ @vio out
+ @vcomment
+ Number of bytes in a cache line.
+ @endvar
+ @var cache_size
+ @vdesc Size of the cache
+ @vtype unsigned long *
+ @vio out
+ @vcomment
+ The size (in bytes) of the cache.
+ @endvar
+
+ @returntype int
+ @returndesc
+ 0 - success
+ -1 - cache sizes have not been set yet.
+ @endreturndesc
+
+@@*/
+int CCTKi_CacheDataGet(unsigned long *cacheline_bytes,
+ unsigned long *cache_size)
+{
+ int retcode;
+
+ if(cache_set)
+ {
+ *cacheline_bytes = cache_data.cacheline_bytes;
+ *cache_size = cache_data.cache_size;
+ retcode = 0;
+ }
+ else
+ {
+ retcode = -1;
+ }
+
+ return retcode;
+}
+