aboutsummaryrefslogtreecommitdiff
path: root/Src/Wasabi/bfc/memblock.cpp
diff options
context:
space:
mode:
authorJean-Francois Mauguit <jfmauguit@mac.com>2024-09-24 09:03:25 -0400
committerGitHub <noreply@github.com>2024-09-24 09:03:25 -0400
commitbab614c421ed7ae329d26bf028c4a3b1d2450f5a (patch)
tree12f17f78986871dd2cfb0a56e5e93b545c1ae0d0 /Src/Wasabi/bfc/memblock.cpp
parent4bde6044fddf053f31795b9eaccdd2a5a527d21f (diff)
parent20d28e80a5c861a9d5f449ea911ab75b4f37ad0d (diff)
downloadwinamp-bab614c421ed7ae329d26bf028c4a3b1d2450f5a.tar.gz
Merge pull request #5 from WinampDesktop/community
Merge to main
Diffstat (limited to 'Src/Wasabi/bfc/memblock.cpp')
-rw-r--r--Src/Wasabi/bfc/memblock.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/Src/Wasabi/bfc/memblock.cpp b/Src/Wasabi/bfc/memblock.cpp
new file mode 100644
index 00000000..668e1ccd
--- /dev/null
+++ b/Src/Wasabi/bfc/memblock.cpp
@@ -0,0 +1,66 @@
+#include "memblock.h"
+#include <bfc/bfc_assert.h>
+
+#ifdef _DEBUG
+int memblocks_totalsize=0;
+#endif
+
+VoidMemBlock::VoidMemBlock(int _size, const void *data) {
+ mem = NULL;
+ size = 0;
+ setSize(_size);
+ if (data != NULL && size > 0) MEMCPY(mem, data, size);
+}
+
+VoidMemBlock::~VoidMemBlock() {
+#ifdef _DEBUG
+ memblocks_totalsize -= size;
+#endif
+ FREE(mem);
+}
+
+void *VoidMemBlock::setSize(int newsize) {
+#ifdef _DEBUG
+ memblocks_totalsize -= size;
+#endif
+ ASSERT(newsize >= 0);
+ if (newsize < 0) newsize = 0;
+ if (newsize == 0) {
+ FREE(mem);
+ mem = NULL;
+ } else if (size != newsize) {
+ mem = REALLOC(mem, newsize);
+ }
+ size = newsize;
+#ifdef _DEBUG
+ memblocks_totalsize += size;
+#endif
+ return getMemory();
+}
+
+void *VoidMemBlock::setMinimumSize(int newminsize, int increment) {
+ if (newminsize > size) setSize(newminsize+increment);
+ return getMemory();
+}
+
+void VoidMemBlock::setMemory(const void *data, int datalen, int offsetby) {
+ if (datalen <= 0) return;
+ ASSERT(mem != NULL);
+ ASSERT(offsetby >= 0);
+ char *ptr = reinterpret_cast<char *>(mem);
+ ASSERT(ptr + offsetby + datalen <= ptr + size);
+ MEMCPY(ptr + offsetby, data, datalen);
+}
+
+int VoidMemBlock::getSize() const {
+ return size;
+}
+
+int VoidMemBlock::isMine(void *ptr) {
+ return (ptr >= mem && ptr < (char*)mem + size);
+}
+
+void VoidMemBlock::zeroMemory() {
+ if (mem == NULL || size < 1) return;
+ MEMZERO(mem, size);
+}