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
44 #ifdef WDL_HEAPBUF_INTF_ONLY
45 void *Resize(int newsize
, bool resizedown
=true);
46 void CopyFrom(const WDL_HeapBuf
*hb
, bool exactCopyOfConfig
=false);
48 void *Get() const { return m_size
?m_buf
:NULL
; } // returns NULL if size is 0
49 void *GetFast() const { return m_buf
; } // returns last buffer if size is 0
50 int GetSize() const { return m_size
; }
51 void *GetAligned(int align
) const { return (void *)(((UINT_PTR
)Get() + (align
-1)) & ~(UINT_PTR
)(align
-1)); }
53 void SetGranul(int granul
) { m_granul
= granul
; }
54 int GetGranul() const { return m_granul
; }
55 void Prealloc(int sz
) { if (m_alloc
< sz
&& WDL_NORMALLY(m_size
< sz
)) { const int oldsz
= m_size
; Resize(sz
,false); m_size
=oldsz
; } }
56 int GetAlloc() const { return m_alloc
; }
58 void *ResizeOK(int newsize
, bool resizedown
= true) { void *p
=Resize(newsize
, resizedown
); return GetSize() == newsize
? p
: NULL
; }
60 WDL_HeapBuf(const WDL_HeapBuf
&cp
)
65 WDL_HeapBuf
&operator=(const WDL_HeapBuf
&cp
)
71 void ResizeToCurrent()
73 if (!m_buf
|| !m_size
)
79 else if (m_alloc
!= m_size
)
81 void *nb
= realloc(m_buf
,m_size
);
82 if (WDL_NORMALLY(nb
!=NULL
))
90 explicit WDL_HeapBuf(int granul
=4096) : m_buf(NULL
), m_alloc(0), m_size(0), m_granul(granul
)
95 #ifdef WDL_HEAPBUF__TRACE_ACTION
96 if (m_buf
) WDL_HEAPBUF__TRACE_ACTION(heapbuf_delete_free
);
101 #endif // !WDL_HEAPBUF_IMPL_ONLY
103 // implementation bits
104 #ifndef WDL_HEAPBUF_INTF_ONLY
105 #ifdef WDL_HEAPBUF_IMPL_ONLY
106 void *WDL_HeapBuf::Resize(int newsize
, bool resizedown
)
108 void *Resize(int newsize
, bool resizedown
=true)
111 if (newsize
<0) newsize
=0;
112 #ifdef DEBUG_TIGHT_ALLOC // horribly slow, do not use for release builds
113 if (newsize
== m_size
) return m_buf
;
116 if (a
> m_size
) a
=m_size
;
117 void *newbuf
= newsize
? malloc(newsize
) : 0;
118 if (!newbuf
&& newsize
)
120 #ifdef WDL_HEAPBUF_ONMALLOCFAIL
121 WDL_HEAPBUF_ONMALLOCFAIL(newsize
)
125 if (newbuf
&&m_buf
) memcpy(newbuf
,m_buf
,a
);
126 m_size
=m_alloc
=newsize
;
131 if (newsize
!=m_size
|| (resizedown
&& newsize
< m_alloc
/2))
133 int resizedown_under
= 0;
134 if (resizedown
&& newsize
< m_size
)
136 // shrinking buffer: only shrink if allocation decreases to min(alloc/2, alloc-granul*4) or 0
137 resizedown_under
= m_alloc
- (m_granul
<<2);
138 if (resizedown_under
> m_alloc
/2) resizedown_under
= m_alloc
/2;
139 if (resizedown_under
< 1) resizedown_under
=1;
142 if (newsize
> m_alloc
|| newsize
< resizedown_under
)
144 int granul
=newsize
/2;
146 if (granul
< m_granul
) granul
=m_granul
;
148 if (newsize
<1) newalloc
=0;
149 else if (m_granul
<4096) newalloc
=newsize
+granul
;
153 if (granul
< 4096) granul
=4096;
154 else if (granul
>4*1024*1024) granul
=4*1024*1024;
155 newalloc
= ((newsize
+ granul
+ 96)&~4095)-96;
158 if (newalloc
!= m_alloc
)
163 #ifdef WDL_HEAPBUF__TRACE_ACTION
164 if (m_buf
) WDL_HEAPBUF__TRACE_ACTION(heapbuf_free
);
172 #ifdef WDL_HEAPBUF__TRACE_ACTION
173 if (m_buf
) WDL_HEAPBUF__TRACE_ACTION(heapbuf_realloc
);
174 else WDL_HEAPBUF__TRACE_ACTION(heapbuf_malloc
);
176 void *nbuf
=realloc(m_buf
,newalloc
);
179 #ifdef WDL_HEAPBUF__TRACE_ACTION
180 WDL_HEAPBUF__TRACE_ACTION(heapbuf_mallocfree
);
182 if (!(nbuf
=malloc(newalloc
)))
184 #ifdef WDL_HEAPBUF_ONMALLOCFAIL
185 WDL_HEAPBUF_ONMALLOCFAIL(newalloc
);
187 return m_size
?m_buf
:0; // failed, do not resize
192 int sz
=newsize
<m_size
?newsize
:m_size
;
193 if (sz
>0) memcpy(nbuf
,m_buf
,sz
);
200 } // alloc size change
201 } // need size up or down
204 return m_size
?m_buf
:0;
207 #ifdef WDL_HEAPBUF_IMPL_ONLY
208 void WDL_HeapBuf::CopyFrom(const WDL_HeapBuf
*hb
, bool exactCopyOfConfig
)
210 void CopyFrom(const WDL_HeapBuf
*hb
, bool exactCopyOfConfig
=false)
213 if (exactCopyOfConfig
) // copy all settings
217 m_granul
= hb
->m_granul
;
220 m_buf
=hb
->m_buf
&& hb
->m_alloc
>0 ? malloc(m_alloc
= hb
->m_alloc
) : NULL
;
221 #ifdef WDL_HEAPBUF_ONMALLOCFAIL
222 if (!m_buf
&& m_alloc
) { WDL_HEAPBUF_ONMALLOCFAIL(m_alloc
) } ;
224 if (m_buf
) memcpy(m_buf
,hb
->m_buf
,m_size
= hb
->m_size
);
227 else // copy just the data + size
229 const int newsz
=hb
->GetSize();
231 if (GetSize()!=newsz
) Resize(0);
232 else memcpy(Get(),hb
->Get(),newsz
);
236 #endif // ! WDL_HEAPBUF_INTF_ONLY
238 #ifndef WDL_HEAPBUF_IMPL_ONLY
246 #if defined(_WIN64) || defined(__LP64__)
248 int ___pad
; // keep size 8 byte aligned
253 template<class PTRTYPE
> class WDL_TypedBuf
256 PTRTYPE
*Get() const { return (PTRTYPE
*) m_hb
.Get(); }
257 PTRTYPE
*GetFast() const { return (PTRTYPE
*) m_hb
.GetFast(); }
258 int GetSize() const { return m_hb
.GetSize()/(unsigned int)sizeof(PTRTYPE
); }
259 int GetSizeBytes() const { return m_hb
.GetSize(); }
260 int GetAlloc() const { return m_hb
.GetAlloc()/(unsigned int)sizeof(PTRTYPE
); }
262 PTRTYPE
*Resize(int newsize
, bool resizedown
= true) { return (PTRTYPE
*)m_hb
.Resize(newsize
*sizeof(PTRTYPE
),resizedown
); }
263 PTRTYPE
*ResizeOK(int newsize
, bool resizedown
= true) { return (PTRTYPE
*)m_hb
.ResizeOK(newsize
*sizeof(PTRTYPE
), resizedown
); }
265 void Prealloc(int sz
) { return m_hb
.Prealloc(sz
*sizeof(PTRTYPE
)); }
266 void ResizeToCurrent() { m_hb
.ResizeToCurrent(); }
268 void SetToZero() { memset(m_hb
.Get(), 0, m_hb
.GetSize()); }
270 PTRTYPE
*GetAligned(int align
) const { return (PTRTYPE
*) m_hb
.GetAligned(align
); }
272 PTRTYPE
*Add(PTRTYPE val
)
274 const int sz
=GetSize();
275 PTRTYPE
* p
=ResizeOK(sz
+1,false);
283 PTRTYPE
*Add(const PTRTYPE
*buf
, int bufsz
)
287 const int sz
=GetSize();
288 PTRTYPE
* p
=ResizeOK(sz
+bufsz
,false);
292 if (buf
) memcpy(p
,buf
,bufsz
*sizeof(PTRTYPE
));
293 else memset((char*)p
,0,bufsz
*sizeof(PTRTYPE
));
299 PTRTYPE
*Set(const PTRTYPE
*buf
, int bufsz
)
303 PTRTYPE
* p
=ResizeOK(bufsz
,false);
306 if (buf
) memcpy(p
,buf
,bufsz
*sizeof(PTRTYPE
));
307 else memset((char*)p
,0,bufsz
*sizeof(PTRTYPE
));
313 PTRTYPE
* Insert(PTRTYPE val
, int idx
)
315 const int sz
=GetSize();
316 if (idx
>= 0 && idx
<= sz
)
318 PTRTYPE
* p
=ResizeOK(sz
+1,false);
321 memmove(p
+idx
+1, p
+idx
, (sz
-idx
)*sizeof(PTRTYPE
));
332 const int sz
=GetSize();
333 if (idx
>= 0 && idx
< sz
)
335 memmove(p
+idx
, p
+idx
+1, (sz
-idx
-1)*sizeof(PTRTYPE
));
340 void DeleteRange(int index
, int count
)
344 if (list
&& count
> 0 && index
>= 0 && index
< size
)
346 if (count
> size
- index
) count
= size
- index
;
349 memmove(list
+index
,list
+index
+count
,(unsigned int)sizeof(PTRTYPE
)*(size
-index
));
354 void SetGranul(int gran
) { m_hb
.SetGranul(gran
); }
356 int Find(PTRTYPE val
) const
358 const PTRTYPE
* p
=Get();
359 const int sz
=GetSize();
361 for (i
=0; i
< sz
; ++i
) if (p
[i
] == val
) return i
;
365 explicit WDL_TypedBuf(int granul
=4096) : m_hb(granul
) { }
368 WDL_HeapBuf
*GetHeapBuf() { return &m_hb
; }
369 const WDL_HeapBuf
*GetHeapBuf() const { return &m_hb
; }
371 int DeleteBatch(bool (*proc
)(PTRTYPE
*p
, void *ctx
), void *ctx
=NULL
) // proc returns true to delete item. returns number deleted
373 const int sz
= GetSize();
375 PTRTYPE
*rd
= Get(), *wr
= rd
;
376 for (int x
= 0; x
< sz
; x
++)
380 if (rd
!= wr
) *wr
=*rd
;
386 if (cnt
< sz
) Resize(cnt
,false);
390 const PTRTYPE
*begin() const { return Get(); }
391 const PTRTYPE
*end() const { return Get() + GetSize(); }
392 PTRTYPE
*begin() { return Get(); }
393 PTRTYPE
*end() { return Get() + GetSize(); }
399 #endif // ! WDL_HEAPBUF_IMPL_ONLY
401 #endif // _WDL_HEAPBUF_H_