3 Copyright (C) 2006 and later, Cockos Incorporated
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
22 This file defines a template for a class that stores a list of objects, and allows the caller
23 to periodically get an object, do something with it, and add it back into the pool.
25 When the caller does this, it can set ownership of the object, and an expiration for that ownership.
28 The PTYPE1 and PTYPE2entries for the template are there to store additional information (for use with poollist.h)
31 This is pretty esoteric. But we use it for some things.
39 // resource pool (time based)
44 class WDL_ResourcePool_ResInfo
// include in class RTYPE as WDL_ResourcePool_ResInfo m_rpoolinfo;
47 WDL_ResourcePool_ResInfo(){ m_owneduntil
=0; m_ownerptr
=0; next
=0; }
48 ~WDL_ResourcePool_ResInfo() {}
50 unsigned int m_owneduntil
;
57 template<class RTYPE
, class EXTRAINFOTYPE
> class WDL_ResourcePool
60 WDL_ResourcePool(char *identstr
)
62 WDL_POOLLIST_refcnt
=0;
63 WDL_POOLLIST_identstr
=identstr
;
73 m_rlist
=(RTYPE
*)m_rlist
->m_rpoolinfo
.next
;
84 m_rlist
=(RTYPE
*)m_rlist
->m_rpoolinfo
.next
;
95 void AddResource(RTYPE
*item
, void *own
, unsigned int until
)
97 item
->m_rpoolinfo
.m_ownerptr
= own
;
98 item
->m_rpoolinfo
.m_owneduntil
= until
;
101 item
->m_rpoolinfo
.next
= m_rlist
;
107 void ReleaseResources(void *own
)
113 if (ent
->m_rpoolinfo
.m_ownerptr
== own
)
115 ent
->m_rpoolinfo
.m_ownerptr
= 0;
116 ent
->m_rpoolinfo
.m_owneduntil
=0;
118 ent
=(RTYPE
*)ent
->m_rpoolinfo
.next
;
123 RTYPE
*GetResource(void *own
, unsigned int now
)
126 RTYPE
*ent
=m_rlist
, *lastent
=NULL
, *bestent
=NULL
, *bestlastent
=NULL
;
127 bool bestnoown
=false;
130 if (ent
->m_rpoolinfo
.m_ownerptr
== own
)
132 if (lastent
) lastent
->m_rpoolinfo
.next
= ent
->m_rpoolinfo
.next
;
133 else m_rlist
= (RTYPE
*)ent
->m_rpoolinfo
.next
;
138 if (!bestnoown
&& (!ent
->m_rpoolinfo
.m_ownerptr
|| ent
->m_rpoolinfo
.m_owneduntil
< now
))
142 if (!ent
->m_rpoolinfo
.m_ownerptr
|| !ent
->m_rpoolinfo
.m_owneduntil
) bestnoown
=true;
145 ent
=(RTYPE
*)ent
->m_rpoolinfo
.next
;
150 if (bestlastent
) bestlastent
->m_rpoolinfo
.next
= bestent
->m_rpoolinfo
.next
;
151 else m_rlist
= (RTYPE
*)bestent
->m_rpoolinfo
.next
;
158 int WDL_POOLLIST_refcnt
;
159 char *WDL_POOLLIST_identstr
;
162 EXTRAINFOTYPE
*extraInfo
;