summaryrefslogtreecommitdiff
path: root/src/util/Cache.c
blob: 95309c02608a019f0e608e697ce2465bd766080f (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
 /*@@
   @file      Cache.c
   @date      Tue Nov 30 08:07:12 1999
   @author    Tom Goodale
   @desc 
   Routines dealing with cache alignment.
   @enddesc 
   @version $Header$
 @@*/

#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 
   @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 *Util_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(Utili_CacheDataGet(&cacheline_bytes, &cache_size))
  {
    cacheline_bytes = 0;
    cache_size = 0;
  }

  data = (char *)malloc((size_t)(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    Utili_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 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 
   @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 Utili_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; 
}