aboutsummaryrefslogtreecommitdiff
path: root/Src/pfc/mem_block.h
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/pfc/mem_block.h
parent4bde6044fddf053f31795b9eaccdd2a5a527d21f (diff)
parent20d28e80a5c861a9d5f449ea911ab75b4f37ad0d (diff)
downloadwinamp-bab614c421ed7ae329d26bf028c4a3b1d2450f5a.tar.gz
Merge pull request #5 from WinampDesktop/community
Merge to main
Diffstat (limited to 'Src/pfc/mem_block.h')
-rw-r--r--Src/pfc/mem_block.h55
1 files changed, 55 insertions, 0 deletions
diff --git a/Src/pfc/mem_block.h b/Src/pfc/mem_block.h
new file mode 100644
index 00000000..73357315
--- /dev/null
+++ b/Src/pfc/mem_block.h
@@ -0,0 +1,55 @@
+#ifndef _PFC_MEM_BLOCK_H_
+#define _PFC_MEM_BLOCK_H_
+
+class mem_block
+{
+private:
+ void * data;
+ int size;
+public:
+ mem_block() {data=0;size=0;}
+ ~mem_block() {if (data) free(data);}
+
+ int get_size() const {return size;}
+
+ void * get_ptr() const {return data;}
+
+ void * set_size(int new_size)
+ {
+ if (data==0) data = malloc(size = new_size);
+ else if (size!=new_size)
+ {
+ void * new_data = realloc(data,new_size);
+ if (!new_data) return 0;
+ data = new_data;
+ size = new_size;
+ }
+ return data;
+ }
+
+ void * check_size(int new_size)
+ {
+ if (size<new_size) return set_size(new_size);
+ else return data;
+ }
+
+ operator void * () const {return data;}
+
+};
+
+template<class T>
+class mem_block_t : private mem_block
+{
+public:
+ int get_size() const {return mem_block::get_size()/sizeof(T);}
+
+ T* get_ptr() const {return static_cast<T*>(mem_block::get_ptr());}
+
+ T* set_size(int new_size) {return static_cast<T*>(mem_block::set_size(new_size*sizeof(T)));}
+
+ T* check_size(int new_size) {return static_cast<T*>(mem_block::check_size(new_size*sizeof(T)));}
+
+ operator T * () const {return get_ptr();}
+};
+
+#endif \ No newline at end of file