Annotation of embedaddon/sqlite3/src/test_wsd.c, revision 1.1

1.1     ! misho       1: /*
        !             2: ** 2008 September 1
        !             3: **
        !             4: ** The author disclaims copyright to this source code.  In place of
        !             5: ** a legal notice, here is a blessing:
        !             6: **
        !             7: **    May you do good and not evil.
        !             8: **    May you find forgiveness for yourself and forgive others.
        !             9: **    May you share freely, never taking more than you give.
        !            10: **
        !            11: *************************************************************************
        !            12: **
        !            13: ** The code in this file contains sample implementations of the 
        !            14: ** sqlite3_wsd_init() and sqlite3_wsd_find() functions required if the
        !            15: ** SQLITE_OMIT_WSD symbol is defined at build time.
        !            16: */
        !            17: 
        !            18: #if defined(SQLITE_OMIT_WSD) && defined(SQLITE_TEST)
        !            19: 
        !            20: #include "sqliteInt.h"
        !            21: 
        !            22: #define PLS_HASHSIZE 43
        !            23: 
        !            24: typedef struct ProcessLocalStorage ProcessLocalStorage;
        !            25: typedef struct ProcessLocalVar ProcessLocalVar;
        !            26: 
        !            27: struct ProcessLocalStorage {
        !            28:   ProcessLocalVar *aData[PLS_HASHSIZE];
        !            29:   int nFree;
        !            30:   u8 *pFree;
        !            31: };
        !            32: 
        !            33: struct ProcessLocalVar {
        !            34:   void *pKey;
        !            35:   ProcessLocalVar *pNext;
        !            36: };
        !            37: 
        !            38: static ProcessLocalStorage *pGlobal = 0;
        !            39: 
        !            40: int sqlite3_wsd_init(int N, int J){
        !            41:   if( !pGlobal ){
        !            42:     int nMalloc = N + sizeof(ProcessLocalStorage) + J*sizeof(ProcessLocalVar);
        !            43:     pGlobal = (ProcessLocalStorage *)malloc(nMalloc);
        !            44:     if( pGlobal ){
        !            45:       memset(pGlobal, 0, sizeof(ProcessLocalStorage));
        !            46:       pGlobal->nFree = nMalloc - sizeof(ProcessLocalStorage);
        !            47:       pGlobal->pFree = (u8 *)&pGlobal[1];
        !            48:     }
        !            49:   }
        !            50: 
        !            51:   return pGlobal ? SQLITE_OK : SQLITE_NOMEM;
        !            52: }
        !            53: 
        !            54: void *sqlite3_wsd_find(void *K, int L){
        !            55:   int i;
        !            56:   int iHash = 0;
        !            57:   ProcessLocalVar *pVar;
        !            58: 
        !            59:   /* Calculate a hash of K */
        !            60:   for(i=0; i<sizeof(void*); i++){
        !            61:     iHash = (iHash<<3) + ((unsigned char *)&K)[i];
        !            62:   }
        !            63:   iHash = iHash%PLS_HASHSIZE;
        !            64: 
        !            65:   /* Search the hash table for K. */
        !            66:   for(pVar=pGlobal->aData[iHash]; pVar && pVar->pKey!=K; pVar=pVar->pNext);
        !            67: 
        !            68:   /* If no entry for K was found, create and populate a new one. */
        !            69:   if( !pVar ){
        !            70:     int nByte = ROUND8(sizeof(ProcessLocalVar) + L);
        !            71:     assert( pGlobal->nFree>=nByte );
        !            72:     pVar = (ProcessLocalVar *)pGlobal->pFree;
        !            73:     pVar->pKey = K;
        !            74:     pVar->pNext = pGlobal->aData[iHash];
        !            75:     pGlobal->aData[iHash] = pVar;
        !            76:     pGlobal->nFree -= nByte;
        !            77:     pGlobal->pFree += nByte;
        !            78:     memcpy(&pVar[1], K, L);
        !            79:   }
        !            80: 
        !            81:   return (void *)&pVar[1];
        !            82: }
        !            83: 
        !            84: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>