test_wsd.c 2.2 KB

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