1 /* Cockos SWELL (Simple/Small Win32 Emulation Layer for Linux/OSX)
2 Copyright (C) 2006 and later, Cockos, Inc.
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.
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:
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.
20 // used for HDC/HGDIOBJ pooling (to avoid excess heap use), used by swell-gdi.mm and swell-gdi-generic.cpp
24 #define SWELL_GDI_DEBUG
27 static WDL_Mutex
*m_ctxpool_mutex
;
28 #ifdef SWELL_GDI_DEBUG
30 #include "../ptrlist.h"
31 static WDL_PtrList
<HDC__
> *m_ctxpool_debug
;
32 static WDL_PtrList
<HGDIOBJ__
> *m_objpool_debug
;
34 static HDC__
*m_ctxpool
;
35 static int m_ctxpool_size
;
36 static HGDIOBJ__
*m_objpool
;
37 static int m_objpool_size
;
42 HDC__
*SWELL_GDP_CTX_NEW()
44 if (!m_ctxpool_mutex
) m_ctxpool_mutex
=new WDL_Mutex
;
47 #ifdef SWELL_GDI_DEBUG
48 m_ctxpool_mutex
->Enter();
49 if (!m_ctxpool_debug
) m_ctxpool_debug
= new WDL_PtrList
<HDC__
>;
50 if (m_ctxpool_debug
->GetSize() > 8192)
52 p
= m_ctxpool_debug
->Get(0);
53 m_ctxpool_debug
->Delete(0);
54 memset(p
,0,sizeof(*p
));
56 m_ctxpool_mutex
->Leave();
60 m_ctxpool_mutex
->Enter();
65 memset(p
,0,sizeof(*p
));
67 m_ctxpool_mutex
->Leave();
72 // printf("alloc ctx\n");
73 p
=(HDC__
*)calloc(sizeof(HDC__
)+128,1); // extra space in case things want to use it (i.e. swell-gdi-lice does)
77 static void SWELL_GDP_CTX_DELETE(HDC__
*p
)
79 if (!m_ctxpool_mutex
) m_ctxpool_mutex
=new WDL_Mutex
;
85 #ifdef SWELL_GDI_DEBUG
86 assert(!p
->_infreelist
);
91 memset(p
,0,sizeof(*p
));
93 #ifdef SWELL_GDI_DEBUG
94 m_ctxpool_mutex
->Enter();
96 if (!m_ctxpool_debug
) m_ctxpool_debug
= new WDL_PtrList
<HDC__
>;
97 m_ctxpool_debug
->Add(p
);
98 m_ctxpool_mutex
->Leave();
100 if (m_ctxpool_size
<100)
102 m_ctxpool_mutex
->Enter();
104 p
->_next
= m_ctxpool
;
107 m_ctxpool_mutex
->Leave();
111 // printf("free ctx\n");
116 static HGDIOBJ__
*GDP_OBJECT_NEW()
118 if (!m_ctxpool_mutex
) m_ctxpool_mutex
=new WDL_Mutex
;
120 #ifdef SWELL_GDI_DEBUG
121 m_ctxpool_mutex
->Enter();
122 if (!m_objpool_debug
) m_objpool_debug
= new WDL_PtrList
<HGDIOBJ__
>;
123 if (m_objpool_debug
->GetSize()>8192)
125 p
= m_objpool_debug
->Get(0);
126 m_objpool_debug
->Delete(0);
127 memset(p
,0,sizeof(*p
));
129 m_ctxpool_mutex
->Leave();
133 m_ctxpool_mutex
->Enter();
136 m_objpool
= p
->_next
;
138 memset(p
,0,sizeof(*p
));
140 m_ctxpool_mutex
->Leave();
145 // printf("alloc obj\n");
146 p
=(HGDIOBJ__
*)calloc(sizeof(HGDIOBJ__
),1);
150 static void GDP_OBJECT_DELETE(HGDIOBJ__
*p
)
152 if (!m_ctxpool_mutex
) m_ctxpool_mutex
=new WDL_Mutex
;
157 #ifdef SWELL_GDI_DEBUG
158 assert(!p
->_infreelist
);
163 memset(p
,0,sizeof(*p
));
164 #ifdef SWELL_GDI_DEBUG
165 m_ctxpool_mutex
->Enter();
166 p
->_infreelist
= true;
167 if (!m_objpool_debug
) m_objpool_debug
= new WDL_PtrList
<HGDIOBJ__
>;
168 m_objpool_debug
->Add(p
);
169 m_ctxpool_mutex
->Leave();
171 if (m_objpool_size
<200)
173 m_ctxpool_mutex
->Enter();
174 p
->_infreelist
= true;
175 p
->_next
= m_objpool
;
178 m_ctxpool_mutex
->Leave();
182 // printf("free obj\n");
188 static bool HGDIOBJ_VALID(HGDIOBJ__
*p
, int reqType
=0)
190 if (p
== (HGDIOBJ__
*)TYPE_PEN
|| p
== (HGDIOBJ__
*)TYPE_BRUSH
||
191 p
== (HGDIOBJ__
*)TYPE_FONT
|| p
== (HGDIOBJ__
*)TYPE_BITMAP
) return false;
192 #ifdef SWELL_GDI_DEBUG
193 if (p
) { assert(!p
->_infreelist
); }
195 // insert breakpoints in these parts for debugging
196 if (p
&& !p
->_infreelist
)
198 #ifdef SWELL_GDI_DEBUG
199 if (reqType
) { assert(reqType
== p
->type
); }
202 return !reqType
|| reqType
== p
->type
;
207 static bool HDC_VALID(HDC__
*ct
)
209 #ifdef SWELL_GDI_DEBUG
210 if (ct
) { assert(!ct
->_infreelist
); }
212 // insert breakpoints in these parts for debugging
213 return ct
&& !ct
->_infreelist
;
217 #if !defined(SWELL_GDI_DEBUG) && defined(SWELL_CLEANUP_ON_UNLOAD)
219 class _swellGdiUnloader
222 _swellGdiUnloader() { }
226 HDC__
*p
= m_ctxpool
;
236 HGDIOBJ__
*p
= m_objpool
;
246 delete m_ctxpool_mutex
;
247 m_ctxpool_mutex
=NULL
;
251 _swellGdiUnloader __swell__swellGdiUnloader
;