Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #include "SkTLS.h" |
michael@0 | 2 | |
michael@0 | 3 | // enable to help debug TLS storage |
michael@0 | 4 | //#define SK_TRACE_TLS_LIFETIME |
michael@0 | 5 | |
michael@0 | 6 | |
michael@0 | 7 | #ifdef SK_TRACE_TLS_LIFETIME |
michael@0 | 8 | #include "SkThread.h" |
michael@0 | 9 | static int32_t gTLSRecCount; |
michael@0 | 10 | #endif |
michael@0 | 11 | |
michael@0 | 12 | struct SkTLSRec { |
michael@0 | 13 | SkTLSRec* fNext; |
michael@0 | 14 | void* fData; |
michael@0 | 15 | SkTLS::CreateProc fCreateProc; |
michael@0 | 16 | SkTLS::DeleteProc fDeleteProc; |
michael@0 | 17 | |
michael@0 | 18 | #ifdef SK_TRACE_TLS_LIFETIME |
michael@0 | 19 | SkTLSRec() { |
michael@0 | 20 | int n = sk_atomic_inc(&gTLSRecCount); |
michael@0 | 21 | SkDebugf(" SkTLSRec[%d]\n", n); |
michael@0 | 22 | } |
michael@0 | 23 | #endif |
michael@0 | 24 | |
michael@0 | 25 | ~SkTLSRec() { |
michael@0 | 26 | if (fDeleteProc) { |
michael@0 | 27 | fDeleteProc(fData); |
michael@0 | 28 | } |
michael@0 | 29 | // else we leak fData, or it will be managed by the caller |
michael@0 | 30 | |
michael@0 | 31 | #ifdef SK_TRACE_TLS_LIFETIME |
michael@0 | 32 | int n = sk_atomic_dec(&gTLSRecCount); |
michael@0 | 33 | SkDebugf("~SkTLSRec[%d]\n", n - 1); |
michael@0 | 34 | #endif |
michael@0 | 35 | } |
michael@0 | 36 | }; |
michael@0 | 37 | |
michael@0 | 38 | void SkTLS::Destructor(void* ptr) { |
michael@0 | 39 | #ifdef SK_TRACE_TLS_LIFETIME |
michael@0 | 40 | SkDebugf("SkTLS::Destructor(%p)\n", ptr); |
michael@0 | 41 | #endif |
michael@0 | 42 | |
michael@0 | 43 | SkTLSRec* rec = (SkTLSRec*)ptr; |
michael@0 | 44 | do { |
michael@0 | 45 | SkTLSRec* next = rec->fNext; |
michael@0 | 46 | SkDELETE(rec); |
michael@0 | 47 | rec = next; |
michael@0 | 48 | } while (NULL != rec); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | void* SkTLS::Get(CreateProc createProc, DeleteProc deleteProc) { |
michael@0 | 52 | if (NULL == createProc) { |
michael@0 | 53 | return NULL; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | void* ptr = SkTLS::PlatformGetSpecific(true); |
michael@0 | 57 | |
michael@0 | 58 | if (ptr) { |
michael@0 | 59 | const SkTLSRec* rec = (const SkTLSRec*)ptr; |
michael@0 | 60 | do { |
michael@0 | 61 | if (rec->fCreateProc == createProc) { |
michael@0 | 62 | SkASSERT(rec->fDeleteProc == deleteProc); |
michael@0 | 63 | return rec->fData; |
michael@0 | 64 | } |
michael@0 | 65 | } while ((rec = rec->fNext) != NULL); |
michael@0 | 66 | // not found, so create a new one |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | // add a new head of our change |
michael@0 | 70 | SkTLSRec* rec = new SkTLSRec; |
michael@0 | 71 | rec->fNext = (SkTLSRec*)ptr; |
michael@0 | 72 | |
michael@0 | 73 | SkTLS::PlatformSetSpecific(rec); |
michael@0 | 74 | |
michael@0 | 75 | rec->fData = createProc(); |
michael@0 | 76 | rec->fCreateProc = createProc; |
michael@0 | 77 | rec->fDeleteProc = deleteProc; |
michael@0 | 78 | return rec->fData; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | void* SkTLS::Find(CreateProc createProc) { |
michael@0 | 82 | if (NULL == createProc) { |
michael@0 | 83 | return NULL; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | void* ptr = SkTLS::PlatformGetSpecific(false); |
michael@0 | 87 | |
michael@0 | 88 | if (ptr) { |
michael@0 | 89 | const SkTLSRec* rec = (const SkTLSRec*)ptr; |
michael@0 | 90 | do { |
michael@0 | 91 | if (rec->fCreateProc == createProc) { |
michael@0 | 92 | return rec->fData; |
michael@0 | 93 | } |
michael@0 | 94 | } while ((rec = rec->fNext) != NULL); |
michael@0 | 95 | } |
michael@0 | 96 | return NULL; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | void SkTLS::Delete(CreateProc createProc) { |
michael@0 | 100 | if (NULL == createProc) { |
michael@0 | 101 | return; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | void* ptr = SkTLS::PlatformGetSpecific(false); |
michael@0 | 105 | |
michael@0 | 106 | SkTLSRec* curr = (SkTLSRec*)ptr; |
michael@0 | 107 | SkTLSRec* prev = NULL; |
michael@0 | 108 | while (curr) { |
michael@0 | 109 | SkTLSRec* next = curr->fNext; |
michael@0 | 110 | if (curr->fCreateProc == createProc) { |
michael@0 | 111 | if (prev) { |
michael@0 | 112 | prev->fNext = next; |
michael@0 | 113 | } else { |
michael@0 | 114 | // we have a new head of our chain |
michael@0 | 115 | SkTLS::PlatformSetSpecific(next); |
michael@0 | 116 | } |
michael@0 | 117 | SkDELETE(curr); |
michael@0 | 118 | break; |
michael@0 | 119 | } |
michael@0 | 120 | prev = curr; |
michael@0 | 121 | curr = next; |
michael@0 | 122 | } |
michael@0 | 123 | } |