blob: a7fad263ad0dba8bf77ea177f14525389c837804 [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.
16**
drh18472fa2008-10-07 15:25:48 +000017** $Id: test_wsd.c,v 1.3 2008/10/07 15:25:49 drh Exp $
danielk1977075c23a2008-09-01 18:34:20 +000018*/
19
20#if defined(SQLITE_OMIT_WSD) && defined(SQLITE_TEST)
21
22#include "sqliteInt.h"
23
24#define PLS_HASHSIZE 43
25
26typedef struct ProcessLocalStorage ProcessLocalStorage;
27typedef struct ProcessLocalVar ProcessLocalVar;
28
29struct ProcessLocalStorage {
30 ProcessLocalVar *aData[PLS_HASHSIZE];
31 int nFree;
32 u8 *pFree;
33};
34
35struct ProcessLocalVar {
36 void *pKey;
37 ProcessLocalVar *pNext;
38};
39
40static ProcessLocalStorage *pGlobal = 0;
41
42int 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
56void *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++){
danielk1977a8f83bf2008-09-02 16:22:28 +000063 iHash = (iHash<<3) + ((unsigned char *)&K)[i];
danielk1977075c23a2008-09-01 18:34:20 +000064 }
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