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 ** $Id: test_wsd.c,v 1.2 2008/09/02 16:22:29 danielk1977 Exp $ 18 */ 19 20 #if defined(SQLITE_OMIT_WSD) && defined(SQLITE_TEST) 21 22 #include "sqliteInt.h" 23 24 #define PLS_HASHSIZE 43 25 26 typedef struct ProcessLocalStorage ProcessLocalStorage; 27 typedef struct ProcessLocalVar ProcessLocalVar; 28 29 struct ProcessLocalStorage { 30 ProcessLocalVar *aData[PLS_HASHSIZE]; 31 int nFree; 32 u8 *pFree; 33 }; 34 35 struct ProcessLocalVar { 36 void *pKey; 37 ProcessLocalVar *pNext; 38 }; 39 40 static ProcessLocalStorage *pGlobal = 0; 41 42 int sqlite3_wsd_init(int N, int J){ 43 if( !pGlobal ){ 44 int nMalloc = N + sizeof(ProcessLocalStorage) + J*sizeof(ProcessLocalVar); 45 pGlobal = (ProcessLocalStorage *)malloc(nMalloc); 46 if( pGlobal ){ 47 memset(pGlobal, 0, sizeof(ProcessLocalStorage)); 48 pGlobal->nFree = nMalloc - sizeof(ProcessLocalStorage); 49 pGlobal->pFree = (u8 *)&pGlobal[1]; 50 } 51 } 52 53 return pGlobal ? SQLITE_OK : SQLITE_NOMEM; 54 } 55 56 void *sqlite3_wsd_find(void *K, int L){ 57 int i; 58 int iHash = 0; 59 ProcessLocalVar *pVar; 60 61 /* Calculate a hash of K */ 62 for(i=0; i<sizeof(void*); i++){ 63 iHash = (iHash<<3) + ((unsigned char *)&K)[i]; 64 } 65 iHash = iHash%PLS_HASHSIZE; 66 67 /* Search the hash table for K. */ 68 for(pVar=pGlobal->aData[iHash]; pVar && pVar->pKey!=K; pVar=pVar->pNext); 69 70 /* If no entry for K was found, create and populate a new one. */ 71 if( !pVar ){ 72 int nByte = (sizeof(ProcessLocalVar) + L + 7)&~7; 73 assert( pGlobal->nFree>=nByte ); 74 pVar = (ProcessLocalVar *)pGlobal->pFree; 75 pVar->pKey = K; 76 pVar->pNext = pGlobal->aData[iHash]; 77 pGlobal->aData[iHash] = pVar; 78 pGlobal->nFree -= nByte; 79 pGlobal->pFree += nByte; 80 memcpy(&pVar[1], K, L); 81 } 82 83 return (void *)&pVar[1]; 84 } 85 86 #endif 87 88