summaryrefslogtreecommitdiff
path: root/store/source/storcach.cxx
blob: 86d1d936e577d946c7a0dc9e4a7294b7ec911e99 (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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/*************************************************************************
 *
 *  $RCSfile: storcach.cxx,v $
 *
 *  $Revision: 1.2 $
 *
 *  last change: $Author: mhu $ $Date: 2001-03-13 20:54:25 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): Matthias Huetsch <matthias.huetsch@sun.com>
 *
 *
 ************************************************************************/

#define _STORE_STORCACH_CXX "$Revision: 1.2 $"

#ifndef _SAL_TYPES_H_
#include <sal/types.h>
#endif

#ifndef _RTL_MEMORY_H_
#include <rtl/memory.h>
#endif

#ifndef _OSL_DIAGNOSE_H_
#include <osl/diagnose.h>
#endif
#ifndef _OSL_MUTEX_HXX_
#include <osl/mutex.hxx>
#endif

#ifndef _STORE_TYPES_H_
#include <store/types.h>
#endif

#ifndef _STORE_STORBASE_HXX_
#include <storbase.hxx>
#endif
#ifndef _STORE_STORCACH_HXX_
#include <storcach.hxx>
#endif

using namespace store;

/*========================================================================
 *
 * OStorePageCache internals.
 *
 *======================================================================*/
/*
 * __store_memcpy.
 */
#include <string.h>
inline void __store_memcpy (void *dst, const void *src, sal_uInt32 n)
{
    ::memcpy (dst, src, n);
}

/*========================================================================
 *
 * OStorePageCacheEntry.
 *
 *======================================================================*/
namespace store
{

struct OStorePageCacheEntry
{
    typedef OStorePageCacheEntry self;
    typedef OStorePageData       data;
    typedef OStorePageDescriptor D;

    /** Representation.
    */
    D     m_aDescr;
    data *m_pData;
    self *m_pNext;
    self *m_pPrev;

    /** Construction.
    */
    OStorePageCacheEntry (const D& rDescr, const data& rData)
        : m_aDescr (rDescr)
    {
        sal_uInt16 nSize = m_aDescr.m_nSize;
        m_pData = new(nSize) data(nSize);
        __store_memcpy (m_pData, &rData, nSize);
        m_pNext = m_pPrev = this;
    }

    /** Data assignment.
    */
    void assign (const D& rDescr, const data& rData)
    {
        m_aDescr.m_nAddr = rDescr.m_nAddr;
        if (!(m_aDescr.m_nSize == rDescr.m_nSize))
        {
            delete m_pData;
            m_pData = new(rDescr.m_nSize) data(rDescr.m_nSize);
            m_aDescr.m_nSize = rDescr.m_nSize;
        }
        __store_memcpy (m_pData, &rData, m_aDescr.m_nSize);
    }

    /** Destruction.
    */
    ~OStorePageCacheEntry (void)
    {
        delete m_pData;
    }

    /** Comparison.
    */
    enum CompareResult
    {
        COMPARE_LESS    = -1,
        COMPARE_EQUAL   =  0,
        COMPARE_GREATER =  1
    };

    CompareResult compare (const D& rDescr) const
    {
        if (m_aDescr.m_nAddr == rDescr.m_nAddr)
            return COMPARE_EQUAL;
        if (m_aDescr.m_nAddr < rDescr.m_nAddr)
            return COMPARE_LESS;
        else
            return COMPARE_GREATER;
    }

    CompareResult compare (const self& rOther) const
    {
        return compare (rOther.m_aDescr);
    }

    /** Address operation.
    */
    void invalidate (void)
    {
        m_aDescr.m_nAddr = STORE_PAGE_NULL;
    }

    sal_Bool isValid (void) const
    {
        return (m_aDescr.m_nAddr != STORE_PAGE_NULL);
    }

    /** Index operation.
    */
    sal_uInt16 index (void) const
    {
        return (m_aDescr.m_nUsed & 0x7fff);
    }

    void index (sal_uInt16 nIndex)
    {
        m_aDescr.m_nUsed = ((m_aDescr.m_nUsed & 0x8000) | (nIndex & 0x7fff));
    }

    /** DirtyBit operation.
    */
    void clean (void)
    {
        m_aDescr.m_nUsed &= 0x7fff;
    }

    void dirty (void)
    {
        m_aDescr.m_nUsed |= 0x8000;
    }

    sal_Bool isDirty (void) const
    {
        return ((m_aDescr.m_nUsed & 0x8000) == 0x8000);
    }

    /** List operation.
    */
    void backlink (self& rOther)
    {
        rOther.m_pNext = this;
        rOther.m_pPrev = m_pPrev;
        m_pPrev = &rOther;
        rOther.m_pPrev->m_pNext = &rOther;
    }

    void unlink (void)
    {
        m_pNext->m_pPrev = m_pPrev;
        m_pPrev->m_pNext = m_pNext;
        m_pNext = this;
        m_pPrev = this;
    }
};

} // namespace store

/*========================================================================
 *
 * OStorePageCache debug internals.
 *
 *======================================================================*/
/*
 * __store_check_entry.
 */
static sal_Bool __store_check_entry (
    OStorePageCacheEntry **ppData, sal_uInt16 nUsed)
{
    if (nUsed > 1)
    {
        for (sal_uInt16 i = 0; i < nUsed - 1; i++)
        {
            sal_uInt32 ai = ppData[i    ]->m_aDescr.m_nAddr;
            sal_uInt32 ak = ppData[i + 1]->m_aDescr.m_nAddr;
            if (!(ai <= ak))
                return sal_False;
            if (!(i == ppData[i]->index()))
                return sal_False;
        }
    }
    return sal_True;
}

/*
 * __store_find_entry.
 */
static sal_uInt16 __store_find_entry (
    const OStorePageDescriptor &rDescr,
    const OStorePageCacheEntry *pHead)
{
    if (pHead)
    {
        if (pHead->m_aDescr.m_nAddr == rDescr.m_nAddr)
            return pHead->index();

        OStorePageCacheEntry *pEntry = pHead->m_pNext;
        while (pEntry != pHead)
        {
            if (pEntry->m_aDescr.m_nAddr == rDescr.m_nAddr)
                return pEntry->index();
            else
                pEntry = pEntry->m_pNext;
        }
    }
    return ((sal_uInt16)(-1));
}

/*========================================================================
 *
 * OStorePageCache implementation.
 *
 * (two-way association (sorted address array, LRU chain)).
 * (external OStorePageData representation).
 *
 *======================================================================*/
/*
 * OStorePageCache.
 */
OStorePageCache::OStorePageCache (sal_uInt16 nPages)
    : m_nSize    (STORE_LIMIT_CACHEPAGES),
      m_nUsed    (0),
      m_pHead    (0),
      m_nHit     (0),
      m_nMissed  (0),
      m_nUpdHit  (0),
      m_nUpdLRU  (0),
      m_nWrtBack (0)
{
    for (sal_uInt16 i = 0; i < m_nSize; i++)
        m_pData[i] = NULL;
    if (nPages < m_nSize)
        m_nSize = nPages;
}

/*
 * ~OStorePageCache.
 */
OStorePageCache::~OStorePageCache (void)
{
    double x = hitRatio();
    for (sal_uInt16 i = 0; i < m_nSize; i++)
        delete m_pData[i];
}

/*
 * find.
 */
sal_uInt16 OStorePageCache::find (const OStorePageDescriptor &rDescr) const
{
    register sal_Int32 l = 0;
    register sal_Int32 r = m_nUsed - 1;

    while (l < r)
    {
        register sal_Int32 m = ((l + r) >> 1);

        if (m_pData[m]->m_aDescr.m_nAddr == rDescr.m_nAddr)
            return ((sal_uInt16)(m));
        if (m_pData[m]->m_aDescr.m_nAddr < rDescr.m_nAddr)
            l = m + 1;
        else
            r = m - 1;
    }

    // Match or insert position. Caller must do final compare.
    return ((sal_uInt16)(r));
}

/*
 * move.
 */
void OStorePageCache::move (sal_uInt16 nSI, sal_uInt16 nDI)
{
    entry *p = m_pData[nSI];
    if (nSI < nDI)
    {
        // shift left.
        rtl_moveMemory (
            &m_pData[nSI    ],
            &m_pData[nSI + 1],
            (nDI - nSI) * sizeof(entry*));

        // re-index.
        for (sal_uInt16 i = nSI; i < nDI; i++)
            m_pData[i]->index(i);
    }
    if (nSI > nDI)
    {
        // shift right.
        rtl_moveMemory (
            &m_pData[nDI + 1],
            &m_pData[nDI    ],
            (nSI - nDI) * sizeof(entry*));

        // re-index.
        for (sal_uInt16 i = nSI; i > nDI; i--)
            m_pData[i]->index(i);
    }
    m_pData[nDI] = p;
    m_pData[nDI]->index(nDI);

#if 0  /* DEBUG */
    OSL_POSTCOND(
        __store_check_entry(&m_pData[0], m_nUsed),
        "OStorePageCache::move(): check_entry() failed");
#endif /* DEBUG */
}

/*
 * insert.
 */
storeError OStorePageCache::insert (
    sal_uInt16                  nDI,
    const OStorePageDescriptor &rDescr,
    const OStorePageData       &rData,
    OStorePageBIOS             &rBIOS,
    InsertMode                  eMode)
{
#if 0  /* DEBUG */
    OSL_PRECOND(
        __store_check_entry(&m_pData[0], m_nUsed),
        "OStorePageCache::insert(): check_entry() failed");
#endif /* DEBUG */

    entry::CompareResult result = entry::COMPARE_EQUAL;
    if (nDI < m_nUsed)
    {
        result = m_pData[nDI]->compare (rDescr);
        if (result == entry::COMPARE_LESS)
            nDI += 1;
    }

    if (nDI == (sal_uInt16)(-1))
        nDI = 0;
    if (nDI == m_nSize)
        nDI -= 1;

    if (m_nUsed < m_nSize)
    {
        // Allocate cache entry.
        m_pData[m_nUsed] = new entry (rDescr, rData);
        move (m_nUsed, nDI);
        m_nUsed++;

        // Update LRU.
        if (m_pHead)
            m_pHead->backlink (*m_pData[nDI]);
        m_pHead = m_pData[nDI];
    }
    else
    {
        // Check for invalidated cache entry.
        sal_uInt16 nSI = m_nUsed - 1;
        if (m_pData[nSI]->isValid())
        {
            // Replace least recently used cache entry.
            m_nUpdLRU++;

            m_pHead = m_pHead->m_pPrev;
            nSI     = m_pHead->index();

            // Check DirtyBit.
            if (m_pHead->isDirty())
            {
                // Save PageDescriptor.
                OStorePageDescriptor aDescr (m_pHead->m_aDescr);

                // Write page.
                storeError eErrCode = rBIOS.write (
                    aDescr.m_nAddr, m_pHead->m_pData, aDescr.m_nSize);
                if (eErrCode != store_E_None)
                    return eErrCode;

                // Mark as clean.
                m_pHead->clean();
                m_nWrtBack++;
            }
        }
        else
        {
            // Replace invalidated cache entry. Check LRU.
            if (!(m_pData[nSI] == m_pHead))
            {
                // Update LRU.
                m_pData[nSI]->unlink();
                m_pHead->backlink (*m_pData[nSI]);
                m_pHead = m_pData[nSI];
            }
        }

        // Check source and destination indices.
        if (nSI < nDI)
        {
            result = m_pData[nDI]->compare(rDescr);
            if (result == entry::COMPARE_GREATER)
                nDI -= 1;
        }

        // Assign data.
        m_pData[nSI]->assign (rDescr, rData);
        move (nSI, nDI);
    }

    // Check InsertMode.
    if (eMode == INSERT_CLEAN)
        m_pHead->clean();
    else
        m_pHead->dirty();

    // Done.
    return store_E_None;
}

/*
 * load.
 */
storeError OStorePageCache::load (
    const OStorePageDescriptor &rDescr,
    OStorePageData             &rData,
    OStorePageBIOS             &rBIOS,
    osl::Mutex                 *pMutex)
{
    // Enter.
    STORE_METHOD_ENTER(pMutex);

    // Find cache index.
    sal_uInt16 i = find (rDescr);
    if (i < m_nUsed)
    {
        if (m_pData[i]->compare(rDescr) == entry::COMPARE_EQUAL)
        {
            // Cache hit.
            m_nHit++;

            if (!(m_pData[i] == m_pHead))
            {
                // Update LRU.
                m_pData[i]->unlink();
                m_pHead->backlink (*m_pData[i]);
                m_pHead = m_pData[i];
            }

            // Load data and Leave.
            __store_memcpy (&rData, m_pHead->m_pData, rDescr.m_nSize);
            STORE_METHOD_LEAVE(pMutex, store_E_None);
        }
    }

    // Cache miss.
    m_nMissed++;

    // Load data.
    storeError eErrCode = rBIOS.read (
        rDescr.m_nAddr, &rData, rDescr.m_nSize);
    if (eErrCode != store_E_None)
        STORE_METHOD_LEAVE(pMutex, eErrCode);

    // Insert data.
    eErrCode = insert (i, rDescr, rData, rBIOS, INSERT_CLEAN);
    if (eErrCode != store_E_None)
        STORE_METHOD_LEAVE(pMutex, eErrCode);

    // Leave with pending verification.
    STORE_METHOD_LEAVE(pMutex, store_E_Pending);
}

/*
 * update.
 */
storeError OStorePageCache::update (
    const OStorePageDescriptor &rDescr,
    const OStorePageData       &rData,
    OStorePageBIOS             &rBIOS,
    osl::Mutex                 *pMutex,
    UpdateMode                  eMode)
{
    // Enter.
    STORE_METHOD_ENTER(pMutex);

    // Check UpdateMode.
    if (eMode == UPDATE_WRITE_THROUGH)
    {
        // Save data.
        storeError eErrCode = rBIOS.write (
            rDescr.m_nAddr, &rData, rDescr.m_nSize);
        if (eErrCode != store_E_None)
            STORE_METHOD_LEAVE(pMutex, eErrCode);
        m_nWrtBack++;
    }

    // Find cache index.
    sal_uInt16 i = find (rDescr);
    if (i < m_nUsed)
    {
        if (m_pData[i]->compare(rDescr) == entry::COMPARE_EQUAL)
        {
            // Cache hit. Check LRU.
            m_nUpdHit++;
            if (!(m_pData[i] == m_pHead))
            {
                // Update LRU.
                m_pData[i]->unlink();
                m_pHead->backlink (*m_pData[i]);
                m_pHead = m_pData[i];
            }

            // Check UpdateMode.
            if (eMode == UPDATE_WRITE_THROUGH)
                m_pHead->clean();
            else
                m_pHead->dirty();

            // Update data and leave.
            __store_memcpy (m_pHead->m_pData, &rData, rDescr.m_nSize);
            STORE_METHOD_LEAVE(pMutex, store_E_None);
        }
    }

    // Cache miss. Insert data and leave.
    storeError eErrCode = insert (
        i, rDescr, rData, rBIOS,
        ((eMode == UPDATE_WRITE_THROUGH) ? INSERT_CLEAN : INSERT_DIRTY));
    STORE_METHOD_LEAVE(pMutex, eErrCode);
}

/*
 * invalidate.
 */
storeError OStorePageCache::invalidate (
    const OStorePageDescriptor &rDescr,
    osl::Mutex                 *pMutex)
{
    // Enter.
    STORE_METHOD_ENTER(pMutex);

    // Find cache index.
    sal_uInt16 i = find (rDescr);
    if (i < m_nUsed)
    {
        if (m_pData[i]->compare(rDescr) == entry::COMPARE_EQUAL)
        {
            // Cache hit. Update LRU.
            if (!(m_pData[i] == m_pHead))
            {
                m_pData[i]->unlink();
                m_pHead->backlink (*m_pData[i]);
            }
            else
            {
                m_pHead = m_pHead->m_pNext;
            }

            // Invalidate.
            m_pData[i]->clean();
            m_pData[i]->invalidate();
            move (i, m_nUsed - 1);
        }
    }

    // Leave.
    STORE_METHOD_LEAVE(pMutex, store_E_None);
}

/*
 * flush.
 */
storeError OStorePageCache::flush (
    OStorePageBIOS &rBIOS,
    osl::Mutex     *pMutex)
{
    // Enter.
    STORE_METHOD_ENTER(pMutex);

    // Check all entries.
    for (sal_uInt16 i = 0; i < m_nUsed; i++)
    {
        // Check for dirty entry.
        if (m_pData[i]->isDirty() && m_pData[i]->isValid())
        {
            // Save PageDescriptor.
            OStorePageDescriptor aDescr (m_pData[i]->m_aDescr);

            // Write page.
            storeError eErrCode = rBIOS.write (
                aDescr.m_nAddr, m_pData[i]->m_pData, aDescr.m_nSize);
            OSL_POSTCOND(
                eErrCode == store_E_None,
                "OStorePageCache::flush(): write() failed");

            // Mark entry clean.
            if (eErrCode == store_E_None)
                m_pData[i]->clean();
            m_nWrtBack++;
        }
    }

    // Leave.
    STORE_METHOD_LEAVE(pMutex, store_E_None);
}