File: acache.c

package info (click to toggle)
acedb 4.9.39%2Bdfsg.02-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 33,796 kB
  • sloc: ansic: 256,989; perl: 2,803; cpp: 2,534; csh: 1,712; python: 862; sh: 658; makefile: 298; awk: 249; lex: 225; yacc: 221
file content (479 lines) | stat: -rw-r--r-- 14,499 bytes parent folder | download | duplicates (6)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*  File: acache.c
 *  Author: Jean Thierry-Mieg (mieg@mrc-lmb.cam.ac.uk)
 *  Copyright (C) J Thierry-Mieg and R Durbin, 1991
 * -------------------------------------------------------------------
 * Acedb 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.
 * or see the on-line version at http://www.gnu.org/copyleft/gpl.txt
 * -------------------------------------------------------------------
 * This file is part of the ACEDB genome database package, written by
 * 	Richard Durbin (MRC LMB, UK) rd@mrc-lmb.cam.ac.uk, and
 *	Jean Thierry-Mieg (CRBM du CNRS, France) mieg@kaa.cnrs-mop.fr
 *
 * Description:
    For acedb5, we are develloping a new disk handling and cache system
    using adisk.c acache.c bs2block.c
    removing disknew.c blocksubs.c and the lower level b*.c routines

    This cache handles A objects
    The idea is to keep them around for a while before sending them to
    disk so that multiple calls to arrayGet/Store, as happens during 
    parse operations and elsewhere cost nothing

    The B objects have their own B-develloped cache, that B-cache, handled
    in objcache.c stores its own objects as arrays but calls directly
    adisk.c and does not travel through the present cache

    Very large Arrays, larger than 1/2 the whole cache size are not cached.
    Also all accessed Arrays  are considered write enabled and no control is 
    exercised here. 

    We have to copy the array before serving it, because the client
    may be updating it, so it would be invalid for a second client, and may
    never store it back if he did not update it, so it would be lost.
    This is a bit costly, but certainly less than accessing the disk.

    We also have to copy the modified array when storing it, because the client
    may keep using it. This could be fixed by distinguishing arrayStore and
    arrayStoreDestroy in a few places in the code.

    Swapping is handled inside the cache, just when read/writing to disk

    Client updating is handled just when disk storing, assuming that acediff
    does not handle A object in any significant way, 
    I do not know if this is correct
 * Exported functions:
    BOOL aCacheGet
    void aCacheKill
    void aCacheStore
    void aCacheStoreDestroy 
    int aCacheSaveAll         // called from saveAll in adisk,c
 * HISTORY:
 * Last edited: Feb  8 14:29 1999 (edgrif)
 * * Feb  8 14:28 1999 (edgrif): Add #ifdef ACEDB5.
 * Created: Wed Feb 25 1997
 *-------------------------------------------------------------------
 */

/* $Id: acache.c,v 1.10 1999/11/24 14:25:50 mieg Exp $ */

      /***************************************************************/
      /**  File acache.c :                                          **/
      /**  Handles the array cache     of the ACeDB program.        **/
      /***************************************************************/
      /***************************************************************/
      /*                                                             */
      /*  ? routines are public :                                    */
      /*         R.Durbin & J.Thierry-Mieg.                          */
      /*                    last modified  7/3/1991 by JTM.          */
      /*                                                             */
      /***************************************************************/
/*
#define MALLOC_CHECK
#define ARRAY_CHECK
*/


#ifndef ACEDB5
void aCacheDummy (void) 
{ return ; }
#else

#include "acedb.h"

#include "lex_bl_.h"  /* for q->aCache */
#include "lex.h"
#include "pick.h"
#include "adisk.h"
#include "acache.h"
#include "client.h"

/* BOOL   READING_MODELS = FALSE ; */

extern BOOL swapData ;
extern void swapArray(Array a, char *format) ;
extern int MAXKNOWNACACHE ;      /* cumulated array size, set in adisk,c  */
static int totalCache = 0, nKnownACache = 0, nAllocatedACache = 0 ;
typedef struct acacheStruct *ACACHE ;
struct acacheStruct { void *magic ; KEY key, parent ; Array a, b ; 
                 ACACHE up , next ; char format[32] ;
                 BOOL  hasBeenModified ; } ;
static ACACHE  knownACacheTop = NULL ,
	       knownACacheEnd  = NULL ;
static int acacheMagic = 0 ;
#define  ACACHEMAGIC  &acacheMagic 
#define SIZE_OF_ACACHE sizeof(struct acacheStruct)
extern KillFunc killFunc[] ;

static int naCache1read = 0, naCache1written = 0 , naCache2served = 0, naCache2saved = 0 ;

static void aCachePop(ACACHE v);
static void aCacheKOadd(ACACHE v);
static void aCacheKOremove(ACACHE v);

static ACACHE aCacheAlloc (void) ;     /* self managed calloc */
static void aCacheFree (ACACHE v);

/*****************************************************************/
/*****************************************************************/
                 /* General utility routines */
/*****************************************************************/
/*****************************************************************/
/********************    self managed calloc  ********************/         
static Stack freeStack = 0 ;

/***********************/

static ACACHE aCacheAlloc (void)   
{
  static int blocSize = 256 ;
  ACACHE p ;
  int i = -12 ;

  if (!freeStack)
    freeStack = stackCreate (4*blocSize) ;
  if (stackEmpty (freeStack))
    { p = (ACACHE) messalloc (blocSize * SIZE_OF_ACACHE) ;
      for (i = blocSize ; i-- ; ++p)
          push (freeStack,p,ACACHE) ;
/* printf ("Adding %d to ACACHE free list\n",blocSize) ; */
      nAllocatedACache += blocSize ;
      blocSize *= 2 ;
    }
  p = pop (freeStack,ACACHE) ;
  p->magic = ACACHEMAGIC ;
  return p ;
}

/***********************/

static void aCacheFree (ACACHE v)
{
  if (v->a)
    totalCache -= v->a->max * v->a->size ;
  arrayDestroy (v->a) ;
  memset (v, 0, SIZE_OF_ACACHE) ; 
  v->magic = 0 ;
  push (freeStack,v,ACACHE) ;
}

/*****************************************************************/
/*****************************************************************/

/************************************************************************/
                          /* Gives the aCache status*/
                          /*    Public;  Calls nothing*/
int aCacheStatus (int *used, int*known, int *modified, 
		 int *nc2rp, int *nc2wp, int *nccrp, int *nccwp)
{
  register ACACHE v;
  register int i, l, m;

  i = l = m = 0; v = knownACacheTop;
  while(v) 
    { i++ ;
    if (v && v->magic != ACACHEMAGIC)
      messcrash ("wrong achache in aCacheStatus") ;

      if(v->hasBeenModified) m++ ;
      v = v->next; 
    }
  *used = nAllocatedACache ;
  *known = i ;
  *modified = m ;
  *nc2rp = naCache2served ; *nc2wp = naCache2saved ;
  *nccrp = naCache1read ; *nccwp = naCache1written ;
  
  return TRUE;
}
/**************************************************************/


void blockStatus(int *nc1rp, int* nc1wp, int *ncc1rp, int* ncc1wp)
{
  *nc1rp =  naCache2served ; *nc1wp = totalCache >> 10 ; /* naCache2saved ;  */
  *ncc1rp = naCache1read ; *ncc1wp = naCache1written ;
}


/**************************************************************/
/**************************************************************/
    /* Linked Lists manipulations */

/************************************************************************/
 /*********************************************************************/
                /* to add an aCacheEntry to the Known ACacheEntry List */
                /*  Static; called by aCacheCreate and aCacheSave        */

static void aCacheKOadd(ACACHE v)
{
  ACACHE w ;
  
  nKnownACache++;
  w = knownACacheTop;
  knownACacheTop=v;
  
  if (!knownACacheEnd)
    knownACacheEnd = v;
  
  if (w) 
    w->up = v ;
  
  v->up =  NULL;
  v->next = w ; 
}

/*********************************************************************/
                /* to remove an aCacheEntry to the Known ACacheEntry List */
                /*  Static; called by aCacheUpdate and aCacheNew        */
                /*          Calls nothing.      */
static void aCacheKOremove(ACACHE v)
{
  ACACHE u, w;
  
  nKnownACache--; 
  u=v->up;
  w=v->next;
  
  if (knownACacheTop == v) 
    knownACacheTop = w ;
  
  if(knownACacheEnd == v)
    knownACacheEnd = u ;
  
  if(u)
    u->next = w;
  if (w) 
    w->up = u;
}

/**************************************************************/
                      /*pops a aCache to the top of the knownACache line*/
                       /*  Static; called by aCacheCreate; Calls nothing*/
static void aCachePop(ACACHE v)
{
  aCacheKOremove(v) ;
  aCacheKOadd(v) ;
}

/**************************************************************/

static void  aCacheDiskStore(ACACHE v, KEY key, KEY parent, 
			     Array a, char *format, BOOL destroy)
{
 
  Array b = 0 ;
  if (v)
    {
      v->hasBeenModified = FALSE ;
      a = v->a ;
      key = v->key ;
      format = v->format ;
      parent = v->parent ; 
      if (destroy)
	{ 
	  totalCache -= v->a->max * v->a->size ;
	  v->a = 0 ;
	}
    }

  if (swapData && format && *format)
    {
      b = destroy ? a : arrayCopy (a) ;
      swapArray(b, format);
      aDiskArrayStore (key, parent, 0, b) ;  /* no swap, use second array */	 
      arrayDestroy (b) ; 
      if (destroy) a = 0 ;
    }
  else
    {
      aDiskArrayStore (key, parent, 0, a) ;  /* no swap, use second array */
      if (destroy)  arrayDestroy (a) ; 
    }  
}

static void aCacheKOMakeRoom (int size)
{
  ACACHE v = knownACacheEnd, w ;
  LEXP q ;
  while (v && totalCache + size > MAXKNOWNACACHE)
    { w = v->up ;
      if(v->hasBeenModified) 
	aCacheDiskStore (v,0,0,0,0,TRUE) ;
      q = KEY2LEX(v->key);
      if (q && q->cache == v) q->cache = 0 ;
      aCacheKOremove(v);
      aCacheFree(v);
      v = w ;
    }
}

/**************************************************************/

static BOOL  aCacheKOPut (KEY key, KEY parent, Array a, char *format, BOOL modif)
{
  LEXP q = KEY2LEX(key);
  ACACHE v = q ? (ACACHE)q->cache : 0 ;  

  if (v && v->magic != ACACHEMAGIC)
    messcrash ("wrong achache in aCacheKOPut(%s)", name(key)) ;

  if (q) q->cache = 0 ;
  if (v) { aCacheKOremove(v); aCacheFree(v) ;}

  q = KEY2LEX(key); 
  if (format && strlen(format) > 32)
    return FALSE ;  /* do not store monsters */
  if (a->dim * a->size * 2 > MAXKNOWNACACHE)
    return FALSE ;       /* do not store giants */
  if (!q)
    return FALSE ;       /* do not store anonymous */

  aCacheKOMakeRoom(a->dim * a->size) ;
  v = aCacheAlloc() ; 

  v->key = key ;
  v->parent = parent ;
  v->hasBeenModified = modif ;
  strncpy(v->format, format, 32) ;
  v->a = arrayCopy (a) ;
  totalCache += v->a->max * v->a->size ;
  q = KEY2LEX(key) ;   /* reevealuate, may have moved */
  if (!q) messcrash("acacheKOput cannot register in q") ;
  q->cache = v ;
  aCacheKOadd(v) ;
  return TRUE ;
}

/**************************************************************/

static void aCacheDoStore (KEY key, KEY parent, Array a, char *format, BOOL destroy)
{
  if (externalSaver)   /* dump old version */
    externalSaver (key, 1) ;
  
  if (!aCacheKOPut (key, parent, a, format, TRUE))
    aCacheDiskStore (0, key, parent, a, format, destroy) ;

  if (externalSaver)   /* dump new  version and acediff it */
    externalSaver (key, 2) ;
  
  lexUnsetStatus (key, EMPTYSTATUS) ;
  lexSetStatus (key, TOUCHSTATUS) ;
}

/**************************************************************/
/********************  public functions ***********************/
/**************************************************************/

/* swapping NOT treated */
void aCacheKill (KEY key)
{
  LEXP q = KEY2LEX(key);
  ACACHE v = q ? (ACACHE)q->cache : 0 ;
  
  if (v && v->magic != ACACHEMAGIC)
    messcrash ("wrong achache in aCacheKOKill(%s)", name(key)) ;

  if (killFunc[class(key)])
    killFunc[class(key)] (key) ;

  if (q) q->cache = 0 ;
  if (v)
    { 
      lex2clear(v->key) ;
      aCacheKOremove(v) ; 
      aCacheFree(v);
      lexkill(key) ;     
    }
  aDiskKill (key) ;  
  lexSetStatus (key, EMPTYSTATUS) ;
  lexSetStatus (key, TOUCHSTATUS) ;  
  if (externalSaver)   /* dump old version */
    externalSaver (key, 41) ;
}

/**************************************************************/

BOOL aCacheGet (KEY key, KEY *pp, Array *ap, char *format)
{
  LEXP q = KEY2LEX(key);
  ACACHE v = q ? (ACACHE)q->cache : 0 ;

  if (v && v->magic != ACACHEMAGIC)
    messcrash ("wrong achache in aCacheKOGet(%s)", name(key)) ;
  if (v)
    {
      aCachePop (v) ;
      if (pp) *pp = v->parent ;
      *ap = arrayCopy (v->a) ;
      return TRUE ;
    }

  if (externalServer) externalServer (key, 0, 0, 0) ;
  if(!iskeyold(key))
    return 0;
  if (!aDiskArrayGet (key, pp, 0, ap)) return 0 ;

  if (swapData && format && *format)
    swapArray(*ap, format);
  aCacheKOPut (key, pp ? *pp : 0, *ap, format, FALSE) ;
  
  return TRUE ;
}
  

/**************************************************************/
 
void aCacheStore (KEY key, KEY parent, Array a, char *format)
{
  aCacheDoStore (key, parent, a, format, FALSE) ;
}

void aCacheStoreDestroy (KEY key, KEY parent, Array a, char *format)
{
  aCacheDoStore (key, parent, a, format, TRUE) ;
}

/**************************************************************/

int aCacheSaveAll(void)
{
  ACACHE v = knownACacheEnd, w ;
  int n = 0, total = 0 ;

  while (v)
    { w = v->up ; 
      total += v->a->max * v->a->size ;
      if(v->hasBeenModified) 
	{
	  n++ ;
	  aCacheDiskStore(v,0,0,0,0,FALSE) ;
	}
      v = w ;
    }
  /* this verif fails because i think of duplicateSessionObj
     which does a direct manipulation of the cache
     but i am not really sure why
     the crash shows only if i read models in session > 1
  if (total != totalCache)
    messcrash ("bad total count in aCacheSaveAll") ;
    */
  return n ;
}


#endif /* ACEDB5 */
/**************************************************************/
/**************************************************************/