blob: 346fed80ce6cea1acedbb53eb3eafd468afb90ea (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkTLS.h"
// enable to help debug TLS storage
//#define SK_TRACE_TLS_LIFETIME
#ifdef SK_TRACE_TLS_LIFETIME
#include "SkAtomics.h"
static int32_t gTLSRecCount;
#endif
struct SkTLSRec {
SkTLSRec* fNext;
void* fData;
SkTLS::CreateProc fCreateProc;
SkTLS::DeleteProc fDeleteProc;
#ifdef SK_TRACE_TLS_LIFETIME
SkTLSRec() {
int n = sk_atomic_inc(&gTLSRecCount);
SkDebugf(" SkTLSRec[%d]\n", n);
}
#endif
~SkTLSRec() {
if (fDeleteProc) {
fDeleteProc(fData);
}
// else we leak fData, or it will be managed by the caller
#ifdef SK_TRACE_TLS_LIFETIME
int n = sk_atomic_dec(&gTLSRecCount);
SkDebugf("~SkTLSRec[%d]\n", n - 1);
#endif
}
};
void SkTLS::Destructor(void* ptr) {
#ifdef SK_TRACE_TLS_LIFETIME
SkDebugf("SkTLS::Destructor(%p)\n", ptr);
#endif
SkTLSRec* rec = (SkTLSRec*)ptr;
do {
SkTLSRec* next = rec->fNext;
SkDELETE(rec);
rec = next;
} while (rec);
}
void* SkTLS::Get(CreateProc createProc, DeleteProc deleteProc) {
if (NULL == createProc) {
return NULL;
}
void* ptr = SkTLS::PlatformGetSpecific(true);
if (ptr) {
const SkTLSRec* rec = (const SkTLSRec*)ptr;
do {
if (rec->fCreateProc == createProc) {
SkASSERT(rec->fDeleteProc == deleteProc);
return rec->fData;
}
} while ((rec = rec->fNext) != NULL);
// not found, so create a new one
}
// add a new head of our change
SkTLSRec* rec = new SkTLSRec;
rec->fNext = (SkTLSRec*)ptr;
SkTLS::PlatformSetSpecific(rec);
rec->fData = createProc();
rec->fCreateProc = createProc;
rec->fDeleteProc = deleteProc;
return rec->fData;
}
void* SkTLS::Find(CreateProc createProc) {
if (NULL == createProc) {
return NULL;
}
void* ptr = SkTLS::PlatformGetSpecific(false);
if (ptr) {
const SkTLSRec* rec = (const SkTLSRec*)ptr;
do {
if (rec->fCreateProc == createProc) {
return rec->fData;
}
} while ((rec = rec->fNext) != NULL);
}
return NULL;
}
void SkTLS::Delete(CreateProc createProc) {
if (NULL == createProc) {
return;
}
void* ptr = SkTLS::PlatformGetSpecific(false);
SkTLSRec* curr = (SkTLSRec*)ptr;
SkTLSRec* prev = NULL;
while (curr) {
SkTLSRec* next = curr->fNext;
if (curr->fCreateProc == createProc) {
if (prev) {
prev->fNext = next;
} else {
// we have a new head of our chain
SkTLS::PlatformSetSpecific(next);
}
SkDELETE(curr);
break;
}
prev = curr;
curr = next;
}
}
|