summaryrefslogtreecommitdiff
path: root/ncache.c
blob: 80f4a43dace5b0cea188518b26fb0e504e5689b1 (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
/*---------------------------------------------------------------------------*/
/*ncache.h*/
/*---------------------------------------------------------------------------*/
/*The implementation of the cache of nodes*/
/*---------------------------------------------------------------------------*/
/*Based on the code of unionfs translator.*/
/*---------------------------------------------------------------------------*/
/*Copyright (C) 2001, 2002, 2005, 2008, 2009 Free Software Foundation,
  Inc.  Written by Sergiu Ivanov <unlimitedscolobb@gmail.com>.

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License as
  published by the Free Software Foundation; either version 2 of the
  License, or * (at your option) any later version.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.*/
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
#define _GNU_SOURCE 1
/*---------------------------------------------------------------------------*/
#include "ncache.h"
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/*--------Global Variables---------------------------------------------------*/
/*The global cache chain*/
ncache_t ncache;
/*---------------------------------------------------------------------------*/
/*Cache size (may be overwritten by the user)*/
/*int ncache_size = NCACHE_SIZE;*/
/*Our cache is not really a cache now (actually, it has never been
  one). Its main task now will be to maintain additional references to
  shadow nodes, which are still in use by some clients and which could
  possibly go away if nobody maintained references to them
  explicitly.*/
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/*--------Functions----------------------------------------------------------*/
/*Initializes the node cache*/
void ncache_init (void)
{
  /*Reset the LRU and MRU ends of the list */
  ncache.mru = ncache.lru = NULL;

  /*Set the maximal size according to the parameter */
  /*ncache.size_max = size_max; */

  /*The cache is empty so far; remark that */
  ncache.size_current = 0;

  /*Init the lock */
  mutex_init (&ncache.lock);
}				/*ncache_init */

/*---------------------------------------------------------------------------*/
/*Looks up the lnode and stores the result in `node`; creates a new
  entry in the cache if the lookup fails*/
error_t ncache_node_lookup (lnode_t * lnode,	/*search for this */
			    node_t ** node	/*put the result here */
			    )
{
  error_t err = 0;

  /*Obtain the pointer to the node corresponding to `lnode` */
  node_t *n = lnode->node;

  /*If a node has already been created for the given lnode */
  if (n != NULL)
    {
      /*count a new reference to 'n' */
      netfs_nref (n);
    }
  else
    {
      /*create a new node for the given lnode and store the result in `n` */
      err = node_create (lnode, &n);
    }

  /*If no errors have occurred during the previous operations */
  if (!err)
    {
      /*lock the mutex in the looked up node */
      mutex_lock (&n->lock);

      /*store the lookup result in `node` */
      *node = n;
    }

  /*Return the result of operations */
  return err;
}				/*ncache_node_lookup */

/*---------------------------------------------------------------------------*/
/*Removes the given node from the cache. Does not release the
  reference held by the cache (for some further finalization actions
  on the node)*/
/*Nodes will NOT be removed from the cache automatically*/
void ncache_node_remove (node_t * node)
{
  /*Obtain the pointer to the netnode (this contains the information
     specific of us) */
  struct netnode *nn = node->nn;

  /*If there exists a successor of this node in the cache chain */
  if (nn->ncache_next)
    /*remove the reference in the successor */
    nn->ncache_next->nn->ncache_prev = nn->ncache_prev;
  /*If there exists a predecessor of this node in the cache chain */
  if (nn->ncache_prev)
    /*remove the reference in the predecessor */
    nn->ncache_prev->nn->ncache_next = nn->ncache_next;

  /*If the node was located at the MRU end of the list */
  if (ncache.mru == node)
    /*shift the MRU end to the next node */
    ncache.mru = nn->ncache_next;
  /*If the node was located at the LRU end of the list */
  if (ncache.lru == node)
    /*shift the LRU end to the previous node */
    ncache.lru = nn->ncache_prev;

  /*Invalidate the references inside the node */
  nn->ncache_next = nn->ncache_prev = NULL;

  /*Count the removal of a node */
  --ncache.size_current;
}				/*ncache_node_remove */

/*---------------------------------------------------------------------------*/
/*Resets the node cache*/
/*No references to nodes are released*/
void
ncache_reset (void)
{
  /*The node being currently removed from the cache */
  node_t *node;

  /*Acquire a lock on the cache */
  mutex_lock (&ncache.lock);

  /*Release the whole cache chain */
  for (node = ncache.mru; node != NULL;
       ncache_node_remove (node), node = ncache.mru);

  /*Release the lock */
  mutex_unlock (&ncache.lock);
}				/*ncache_reset */

/*---------------------------------------------------------------------------*/
/*Adds the given node to the cache*/
void ncache_node_add (node_t * node)
{
  /*Acquire a lock on the cache */
  mutex_lock (&ncache.lock);

  /*If there already are some nodes in the cache already */
  if (ncache.size_current > 0)
    {
      /*If the node to be added is not at the MRU end already */
      if (ncache.mru != node)
	{
	  /*If the node is correctly integrated in the cache */
	  if ((node->nn->ncache_next != NULL)
	      && (node->nn->ncache_prev != NULL))
	    /*remove the old entry */
	    ncache_node_remove (node);
	  else
	    /*add a new reference to the node */
	    netfs_nref (node);

	  /*put the node at the MRU end of the cache chain */
	  node->nn->ncache_next = ncache.mru;
	  node->nn->ncache_prev = NULL;

	  /*setup the pointer in the old MRU end, if it exists */
	  if (ncache.mru != NULL)
	    ncache.mru->nn->ncache_prev = node;

	  /*setup the LRU end of the cache chain, if it did not exist
	    previously */
	  if (ncache.lru == NULL)
	    ncache.lru = node;

	  /*shift the MRU end to the new node */
	  ncache.mru = node;

	  /*count the addition */
	  ++ncache.size_current;
	}
    }

  /*While the size of the cache is exceeding the maximal size */
/*	node_t * old_lru;
	for
		(
		old_lru = ncache.lru;
		ncache.size_current > ncache.size_max;
		old_lru = ncache.lru
		)
		{*/
  /*remove the current LRU end of the list from the cache */
  /*ncache_node_remove(old_lru); */

  /*release the reference to the node owned by this thread */
  /*netfs_nrele(old_lru);
     } */

  /*Release the lock on the cache */
  mutex_unlock (&ncache.lock);
}				/*ncache_node_add */

/*---------------------------------------------------------------------------*/