summaryrefslogtreecommitdiff
path: root/klm/util/scoped.cc
diff options
context:
space:
mode:
Diffstat (limited to 'klm/util/scoped.cc')
-rw-r--r--klm/util/scoped.cc29
1 files changed, 29 insertions, 0 deletions
diff --git a/klm/util/scoped.cc b/klm/util/scoped.cc
new file mode 100644
index 00000000..e7066ee4
--- /dev/null
+++ b/klm/util/scoped.cc
@@ -0,0 +1,29 @@
+#include "util/scoped.hh"
+
+#include <cstdlib>
+
+namespace util {
+
+MallocException::MallocException(std::size_t requested) throw() {
+ *this << "for " << requested << " bytes ";
+}
+
+MallocException::~MallocException() throw() {}
+
+void *MallocOrThrow(std::size_t requested) {
+ void *ret;
+ UTIL_THROW_IF_ARG(!(ret = std::malloc(requested)), MallocException, (requested), "in malloc");
+ return ret;
+}
+
+scoped_malloc::~scoped_malloc() {
+ std::free(p_);
+}
+
+void scoped_malloc::call_realloc(std::size_t to) {
+ void *ret;
+ UTIL_THROW_IF_ARG(!(ret = std::realloc(p_, to)) && to, MallocException, (to), "in realloc");
+ p_ = ret;
+}
+
+} // namespace util