3 Copyright (C) 2005 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.
25 This file provides the interface and implementation for WDL_HeapBuf, a simple
26 malloc() wrapper for resizeable blocks.
28 Also in this file is WDL_TypedBuf which is a templated version WDL_HeapBuf
29 that manages type and type-size.
33 #ifndef _WDL_HEAPBUF_H_
34 #define _WDL_HEAPBUF_H_
36 #ifndef WDL_HEAPBUF_IMPL_ONLY
39 //#define WDL_HEAPBUF_TRACE
41 #ifdef WDL_HEAPBUF_TRACE
43 #define WDL_HEAPBUF_TRACEPARM(x) ,(x)
45 #define WDL_HEAPBUF_TRACEPARM(x)
53 explicit WDL_HeapBuf(int granul
=4096
54 #ifdef WDL_HEAPBUF_TRACE
55 , const char *tracetype
="WDL_HeapBuf"
57 ) : m_alloc(0), m_size(0), m_mas(0)
62 #ifdef WDL_HEAPBUF_TRACE
63 m_tracetype
= tracetype
;
65 wsprintf(tmp
,"WDL_HeapBuf: created type: %s granul=%d\n",tracetype
,granul
);
66 OutputDebugString(tmp
);
71 #ifdef WDL_HEAPBUF_TRACE
73 wsprintf(tmp
,"WDL_HeapBuf: destroying type: %s (alloc=%d, size=%d)\n",m_tracetype
,m_alloc
,m_size
);
74 OutputDebugString(tmp
);
80 WDL_HeapBuf(const WDL_HeapBuf
&cp
)
85 WDL_HeapBuf
&operator=(const WDL_HeapBuf
&cp
)
91 void CopyFrom(const WDL_HeapBuf
*hb
, bool exactCopyOfConfig
=false)
93 if (exactCopyOfConfig
) // copy all settings
97 #ifdef WDL_HEAPBUF_TRACE
98 m_tracetype
= hb
->m_tracetype
;
100 m_granul
= hb
->m_granul
;
104 m_buf
=hb
->m_buf
&& hb
->m_alloc
>0 ? malloc(m_alloc
= hb
->m_alloc
) : NULL
;
105 #ifdef WDL_HEAPBUF_ONMALLOCFAIL
106 if (!m_buf
&& m_alloc
) { WDL_HEAPBUF_ONMALLOCFAIL(m_alloc
) } ;
108 if (m_buf
) memcpy(m_buf
,hb
->m_buf
,m_size
= hb
->m_size
);
111 else // copy just the data + size
113 int newsz
=hb
->GetSize();
115 if (GetSize()!=newsz
) Resize(0);
116 else memcpy(Get(),hb
->Get(),newsz
);
121 void *Get() const { return m_size
?m_buf
:NULL
; }
122 int GetSize() const { return m_size
; }
124 void SetGranul(int granul
)
129 void SetMinAllocSize(int mas
)
133 #define WDL_HEAPBUF_PREFIX
135 #define WDL_HEAPBUF_PREFIX WDL_HeapBuf::
138 void * WDL_HEAPBUF_PREFIX
Resize(int newsize
, bool resizedown
139 #ifdef WDL_HEAPBUF_INTF_ONLY
142 #ifdef WDL_HEAPBUF_IMPL_ONLY
148 #ifdef DEBUG_TIGHT_ALLOC // horribly slow, do not use for release builds
149 if (newsize
== m_size
) return m_buf
;
152 if (a
> m_size
) a
=m_size
;
153 void *newbuf
= newsize
? malloc(newsize
) : 0;
154 if (!newbuf
&& newsize
)
156 #ifdef WDL_HEAPBUF_ONMALLOCFAIL
157 WDL_HEAPBUF_ONMALLOCFAIL(newsize
)
161 if (newbuf
&&m_buf
) memcpy(newbuf
,m_buf
,a
);
162 m_size
=m_alloc
=newsize
;
167 //#define WDL_HEAPBUF_DYNAMIC
168 #ifdef WDL_HEAPBUF_DYNAMIC
169 // ignoring m_granul and m_mas
173 if ((newsize
> m_size
&& newsize
<= m_alloc
) || (newsize
< m_size
&& !resizedown
))
179 // next highest power of 2
205 void* newbuf
= realloc(m_buf
, n
); // realloc==free when size==0
206 #ifdef WDL_HEAPBUF_ONMALLOCFAIL
207 if (!newbuf
&& n
) { WDL_HEAPBUF_ONMALLOCFAIL(n
) } ;
209 if (newbuf
|| !newsize
)
217 return (m_size
? m_buf
: 0);
219 #else // WDL_HEAPBUF_DYNAMIC
222 // if we are not using m_smallbuf or we need to not use it
223 // and if if growing or resizing down
225 (newsize
> m_alloc
||
226 (resizedown
&& newsize
< m_size
&&
227 newsize
< m_alloc
/2 &&
228 newsize
< m_alloc
- (m_granul
<<2))))
230 int granul
=newsize
/2;
232 if (granul
< m_granul
) granul
=m_granul
;
234 if (m_granul
<4096) newalloc
=newsize
+granul
;
238 if (granul
< 4096) granul
=4096;
239 else if (granul
>4*1024*1024) granul
=4*1024*1024;
240 newalloc
= ((newsize
+ granul
+ 96)&~4095)-96;
243 if (newalloc
< m_mas
) newalloc
=m_mas
;
245 if (newalloc
!= m_alloc
)
247 #ifdef WDL_HEAPBUF_TRACE
249 wsprintf(tmp
,"WDL_HeapBuf: type %s realloc(%d) from %d\n",m_tracetype
,newalloc
,m_alloc
);
250 OutputDebugString(tmp
);
252 void *nbuf
= realloc(m_buf
,newalloc
);
253 if (!nbuf
&& newalloc
)
255 if (!(nbuf
=malloc(newalloc
)))
257 #ifdef WDL_HEAPBUF_ONMALLOCFAIL
258 WDL_HEAPBUF_ONMALLOCFAIL(newalloc
);
260 return m_size
?m_buf
:0; // failed, do not resize
265 int sz
=newsize
<m_size
?newsize
:m_size
;
266 if (sz
>0) memcpy(nbuf
,m_buf
,sz
);
273 } // alloc size change
274 } // need size up or down
277 return m_size
?m_buf
:0;
279 #endif // WDL_HEAPBUF_DYNAMIC
281 #endif // !WDL_HEAPBUF_IMPL_ONLY, I think
283 #ifndef WDL_HEAPBUF_IMPL_ONLY
286 #if !defined(_WIN64) && !defined(__LP64__)
287 int ___pad
; // keep this 8 byte aligned on osx32
294 #ifdef WDL_HEAPBUF_TRACE
295 const char *m_tracetype
;
299 template<class PTRTYPE
> class WDL_TypedBuf
302 explicit WDL_TypedBuf(int granul
=4096
303 #ifdef WDL_HEAPBUF_TRACE
304 , const char *tracetype
="WDL_TypedBuf"
306 ) : m_hb(granul
WDL_HEAPBUF_TRACEPARM(tracetype
))
312 PTRTYPE
*Get() { return (PTRTYPE
*) m_hb
.Get(); }
313 int GetSize() { return m_hb
.GetSize()/sizeof(PTRTYPE
); }
315 PTRTYPE
*Resize(int newsize
, bool resizedown
=true) { return (PTRTYPE
*)m_hb
.Resize(newsize
*sizeof(PTRTYPE
),resizedown
); }
317 PTRTYPE
*Add(PTRTYPE val
)
320 PTRTYPE
*p
=Resize(sz
+1);
321 if (p
&& GetSize() == sz
+1)
329 void SetGranul(int gran
)
331 m_hb
.SetGranul(gran
);
334 int Find(PTRTYPE val
)
338 for (i
=0; i
< GetSize(); ++i
)
340 if (p
[i
] == val
) return i
;