summaryrefslogtreecommitdiff
path: root/src/util/Cache.c
blob: 5ba302759c156075e0a165c7eb0d44aaf0fdce82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
 /*@@
   @file    Cache.c
   @date    Tue Nov 30 08:07:12 1999
   @author  Tom Goodale
   @desc
            Routines dealing with cache alignment.
   @enddesc
   @version $Id$
 @@*/

#include <stdlib.h>

#include "cctk_Cache.h"
#include "cctki_Cache.h"
#include "cctk_Flesh.h"

static const char *rcsid = "$Header$";

CCTK_FILEVERSION(util_Cache_c);

/********************************************************************
 *********************     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    Util_CacheMalloc
   @date       Tue Nov 30 08:13:03 1999
   @author     Tom Goodale
   @desc
   Allocates memory aligned on the 'index'ed cache line.
   @enddesc

   @var     idx
   @vdesc   cache line index
   @vtype   unsigned 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 *Util_CacheMalloc(unsigned int idx,
                       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(Utili_CacheDataGet(&cacheline_bytes, &cache_size))
  {
    cacheline_bytes = cache_size = 0;
  }

  retval = NULL;

  data = malloc((size_t)(size+cache_size));
  if(data)
  {
    pad = 0;
    if(cache_size && cacheline_bytes)
    {
      /* 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 = ((idx-initial_index)*cacheline_bytes + cacheline_bytes - offset)%cache_size;
    }

    retval = data + pad;
  }

  *realstart = (void *)data;

  return (void *)retval;
}

/********************************************************************
 *********************   CCTK Local Routines   **********************
 ********************************************************************/

 /*@@
   @routine    Utili_CacheDataSet
   @date       Tue Nov 30 08:12:05 1999
   @author     Tom Goodale
   @desc
   Stores the sizes of the cache.
   @enddesc

   @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 Utili_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    Utili_CacheDataGet
   @date       Tue Nov 30 08:12:41 1999
   @author     Tom Goodale
   @desc
   Gets the sizes of the cache.
   @enddesc

   @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 Utili_CacheDataGet(unsigned long *cacheline_bytes,
                       unsigned long *cache_size)
{
  if(cache_set)
  {
    *cacheline_bytes = cache_data.cacheline_bytes;
    *cache_size      = cache_data.cache_size;
  }

  return (cache_set ? 0 : -1);
}