LMMS
Loading...
Searching...
No Matches
swell-gdi-internalpool.h
Go to the documentation of this file.
1/* Cockos SWELL (Simple/Small Win32 Emulation Layer for Linux/OSX)
2 Copyright (C) 2006 and later, Cockos, Inc.
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 // used for HDC/HGDIOBJ pooling (to avoid excess heap use), used by swell-gdi.mm and swell-gdi-generic.cpp
21*/
22
23#if defined(_DEBUG)
24 #define SWELL_GDI_DEBUG
25#endif
26
28#ifdef SWELL_GDI_DEBUG
29 #include "../ptrlist.h"
30 static WDL_PtrList<HDC__> *m_ctxpool_debug;
31 static WDL_PtrList<HGDIOBJ__> *m_objpool_debug;
32#else
34 static int m_ctxpool_size;
36 static int m_objpool_size;
37#endif
38
39
40
42{
44
45 HDC__ *p=NULL;
46#ifdef SWELL_GDI_DEBUG
47 m_ctxpool_mutex->Enter();
48 if (!m_ctxpool_debug) m_ctxpool_debug = new WDL_PtrList<HDC__>;
49 if (m_ctxpool_debug->GetSize() > 8192)
50 {
51 p = m_ctxpool_debug->Get(0);
52 m_ctxpool_debug->Delete(0);
53 memset(p,0,sizeof(*p));
54 }
55 m_ctxpool_mutex->Leave();
56#else
57 if (m_ctxpool)
58 {
59 m_ctxpool_mutex->Enter();
60 if ((p=m_ctxpool))
61 {
62 m_ctxpool=p->_next;
64 memset(p,0,sizeof(*p));
65 }
66 m_ctxpool_mutex->Leave();
67 }
68#endif
69 if (!p)
70 {
71// printf("alloc ctx\n");
72 p=(HDC__ *)calloc(sizeof(HDC__)+128,1); // extra space in case things want to use it (i.e. swell-gdi-lice does)
73 }
74 return p;
75}
77{
79
80 if (WDL_NOT_NORMALLY(!p || p->_infreelist)) return;
81
82 memset(p,0,sizeof(*p));
83
84#ifdef SWELL_GDI_DEBUG
85 m_ctxpool_mutex->Enter();
86 p->_infreelist=true;
87 if (!m_ctxpool_debug) m_ctxpool_debug = new WDL_PtrList<HDC__>;
88 m_ctxpool_debug->Add(p);
89 m_ctxpool_mutex->Leave();
90#else
91 if (m_ctxpool_size<100)
92 {
93 m_ctxpool_mutex->Enter();
94 p->_infreelist=true;
95 p->_next = m_ctxpool;
96 m_ctxpool = p;
98 m_ctxpool_mutex->Leave();
99 }
100 else
101 {
102 // printf("free ctx\n");
103 free(p);
104 }
105#endif
106}
108{
111#ifdef SWELL_GDI_DEBUG
112 m_ctxpool_mutex->Enter();
113 if (!m_objpool_debug) m_objpool_debug = new WDL_PtrList<HGDIOBJ__>;
114 if (m_objpool_debug->GetSize()>8192)
115 {
116 p = m_objpool_debug->Get(0);
117 m_objpool_debug->Delete(0);
118 memset(p,0,sizeof(*p));
119 }
120 m_ctxpool_mutex->Leave();
121#else
122 if (m_objpool)
123 {
124 m_ctxpool_mutex->Enter();
125 if ((p=m_objpool))
126 {
127 m_objpool = p->_next;
129 memset(p,0,sizeof(*p));
130 }
131 m_ctxpool_mutex->Leave();
132 }
133#endif
134 if (!p)
135 {
136 // printf("alloc obj\n");
137 p=(HGDIOBJ__ *)calloc(sizeof(HGDIOBJ__),1);
138 }
139 return p;
140}
141
142static bool HGDIOBJ_VALID(HGDIOBJ__ *p, int reqType=0)
143{
144 return p &&
146 p != (HGDIOBJ__*)TYPE_FONT && p != (HGDIOBJ__*)TYPE_BITMAP) &&
147 WDL_NORMALLY(!p->_infreelist) &&
148 WDL_NORMALLY(!reqType || reqType == p->type);
149}
150
152{
154 if (WDL_NOT_NORMALLY(!p) || !HGDIOBJ_VALID(p)) return;
155
156 memset(p,0,sizeof(*p));
157#ifdef SWELL_GDI_DEBUG
158 m_ctxpool_mutex->Enter();
159 p->_infreelist = true;
160 if (!m_objpool_debug) m_objpool_debug = new WDL_PtrList<HGDIOBJ__>;
161 m_objpool_debug->Add(p);
162 m_ctxpool_mutex->Leave();
163#else
164 if (m_objpool_size<200)
165 {
166 m_ctxpool_mutex->Enter();
167 p->_infreelist = true;
168 p->_next = m_objpool;
169 m_objpool = p;
171 m_ctxpool_mutex->Leave();
172 }
173 else
174 {
175 // printf("free obj\n");
176 free(p);
177 }
178#endif
179}
180
181static bool HDC_VALID(HDC__ *ct)
182{
183 return ct && WDL_NORMALLY(!ct->_infreelist);
184}
185
186
187#if !defined(SWELL_GDI_DEBUG) && defined(SWELL_CLEANUP_ON_UNLOAD)
188
189class _swellGdiUnloader
190{
191 public:
192 _swellGdiUnloader() { }
193 ~_swellGdiUnloader()
194 {
195 {
196 HDC__ *p = m_ctxpool;
197 m_ctxpool = NULL;
198 while (p)
199 {
200 HDC__ *t = p;
201 p = p->_next;
202 free(t);
203 }
204 }
205 {
206 HGDIOBJ__ *p = m_objpool;
207 m_objpool = NULL;
208 while (p)
209 {
210 HGDIOBJ__ *t = p;
211 p = p->_next;
212 free(t);
213 }
214 }
215
216 delete m_ctxpool_mutex;
218 }
219};
220
221_swellGdiUnloader __swell__swellGdiUnloader;
222#endif
#define NULL
Definition CarlaBridgeFormat.cpp:30
Definition mutex.h:59
Definition ptrlist.h:40
PTRTYPE * Add(PTRTYPE *item)
Definition ptrlist.h:82
PTRTYPE * Get(INT_PTR index) const
Definition ptrlist.h:51
void Delete(int index)
Definition ptrlist.h:126
int GetSize(void) const
Definition ptrlist.h:58
struct huft * t
Definition inflate.c:943
Definition swell-internal.h:908
bool _infreelist
Definition swell-internal.h:930
Definition swell-internal.h:893
static HDC__ * m_ctxpool
Definition swell-gdi-internalpool.h:33
static int m_objpool_size
Definition swell-gdi-internalpool.h:36
static void SWELL_GDP_CTX_DELETE(HDC__ *p)
Definition swell-gdi-internalpool.h:76
static HGDIOBJ__ * GDP_OBJECT_NEW()
Definition swell-gdi-internalpool.h:107
static HGDIOBJ__ * m_objpool
Definition swell-gdi-internalpool.h:35
static int m_ctxpool_size
Definition swell-gdi-internalpool.h:34
static bool HDC_VALID(HDC__ *ct)
Definition swell-gdi-internalpool.h:181
HDC__ * SWELL_GDP_CTX_NEW()
Definition swell-gdi-internalpool.h:41
static void GDP_OBJECT_DELETE(HGDIOBJ__ *p)
Definition swell-gdi-internalpool.h:151
static bool HGDIOBJ_VALID(HGDIOBJ__ *p, int reqType=0)
Definition swell-gdi-internalpool.h:142
static WDL_Mutex * m_ctxpool_mutex
Definition swell-gdi-internalpool.h:27
#define TYPE_BITMAP
Definition swell-internal.h:984
#define TYPE_BRUSH
Definition swell-internal.h:982
#define TYPE_FONT
Definition swell-internal.h:983
#define TYPE_PEN
Definition swell-internal.h:981
uch * p
Definition crypt.c:594
#define WDL_NOT_NORMALLY(x)
Definition wdltypes.h:166
#define WDL_NORMALLY(x)
Definition wdltypes.h:165