Enhance the command-line completion extension to return the names of
[sqlite.git] / src / test_wsd.c
blob99e4a056581f58f4536fdb5a5df75c74698a8162
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.
11 *************************************************************************
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.
18 #if defined(SQLITE_OMIT_WSD) && defined(SQLITE_TEST)
20 #include "sqliteInt.h"
22 #define PLS_HASHSIZE 43
24 typedef struct ProcessLocalStorage ProcessLocalStorage;
25 typedef struct ProcessLocalVar ProcessLocalVar;
27 struct ProcessLocalStorage {
28 ProcessLocalVar *aData[PLS_HASHSIZE];
29 int nFree;
30 u8 *pFree;
33 struct ProcessLocalVar {
34 void *pKey;
35 ProcessLocalVar *pNext;
38 static ProcessLocalStorage *pGlobal = 0;
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];
51 return pGlobal ? SQLITE_OK : SQLITE_NOMEM;
54 void *sqlite3_wsd_find(void *K, int L){
55 int i;
56 int iHash = 0;
57 ProcessLocalVar *pVar;
59 /* Calculate a hash of K */
60 for(i=0; i<sizeof(void*); i++){
61 iHash = (iHash<<3) + ((unsigned char *)&K)[i];
63 iHash = iHash%PLS_HASHSIZE;
65 /* Search the hash table for K. */
66 for(pVar=pGlobal->aData[iHash]; pVar && pVar->pKey!=K; pVar=pVar->pNext);
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);
81 return (void *)&pVar[1];
84 #endif