blob: 99e4a056581f58f4536fdb5a5df75c74698a8162 [file] [log] [blame]
danielk1977075c23a2008-09-01 18:34:20 +00001/*
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.
danielk1977075c23a2008-09-01 18:34:20 +000016*/
17
18#if defined(SQLITE_OMIT_WSD) && defined(SQLITE_TEST)
19
20#include "sqliteInt.h"
21
22#define PLS_HASHSIZE 43
23
24typedef struct ProcessLocalStorage ProcessLocalStorage;
25typedef struct ProcessLocalVar ProcessLocalVar;
26
27struct ProcessLocalStorage {
28 ProcessLocalVar *aData[PLS_HASHSIZE];
29 int nFree;
30 u8 *pFree;
31};
32
33struct ProcessLocalVar {
34 void *pKey;
35 ProcessLocalVar *pNext;
36};
37
38static ProcessLocalStorage *pGlobal = 0;
39
40int 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
54void *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++){
danielk1977a8f83bf2008-09-02 16:22:28 +000061 iHash = (iHash<<3) + ((unsigned char *)&K)[i];
danielk1977075c23a2008-09-01 18:34:20 +000062 }
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 ){
danielk1977bc739712009-03-23 04:33:32 +000070 int nByte = ROUND8(sizeof(ProcessLocalVar) + L);
danielk1977075c23a2008-09-01 18:34:20 +000071 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