aboutsummaryrefslogtreecommitdiff
path: root/Src/replicant/nu/AutoBuffer.h
diff options
context:
space:
mode:
authorJef <jef@targetspot.com>2024-09-24 08:54:57 -0400
committerJef <jef@targetspot.com>2024-09-24 08:54:57 -0400
commit20d28e80a5c861a9d5f449ea911ab75b4f37ad0d (patch)
tree12f17f78986871dd2cfb0a56e5e93b545c1ae0d0 /Src/replicant/nu/AutoBuffer.h
parent537bcbc86291b32fc04ae4133ce4d7cac8ebe9a7 (diff)
downloadwinamp-20d28e80a5c861a9d5f449ea911ab75b4f37ad0d.tar.gz
Initial community commit
Diffstat (limited to 'Src/replicant/nu/AutoBuffer.h')
-rw-r--r--Src/replicant/nu/AutoBuffer.h49
1 files changed, 49 insertions, 0 deletions
diff --git a/Src/replicant/nu/AutoBuffer.h b/Src/replicant/nu/AutoBuffer.h
new file mode 100644
index 00000000..a972f327
--- /dev/null
+++ b/Src/replicant/nu/AutoBuffer.h
@@ -0,0 +1,49 @@
+#pragma once
+#include "foundation/error.h"
+#include <stdlib.h>
+
+/* an automatically growing buffer */
+
+class AutoBuffer
+{
+public:
+ AutoBuffer()
+ {
+ buffer=0;
+ buffer_length=0;
+ }
+
+ ~AutoBuffer()
+ {
+ free(buffer);
+ }
+
+ operator void *()
+ {
+ return buffer;
+ }
+
+ template <class ptr_t>
+ ptr_t Get()
+ {
+ return (ptr_t)buffer;
+ }
+
+ int Reserve(size_t new_size)
+ {
+ if (new_size <= buffer_length)
+ return NErr_Success;
+
+ void *new_buffer = realloc(buffer, new_size);
+ if (!new_buffer)
+ return NErr_OutOfMemory;
+
+ buffer = new_buffer;
+ buffer_length = new_size;
+ return NErr_Success;
+ }
+
+private:
+ void *buffer;
+ size_t buffer_length;
+};