summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2016-06-17 17:44:45 +0200
committerMax Kellermann <max@duempel.org>2016-06-17 17:57:40 +0200
commitef053035d07318cba05c9766aa5ed69cb380a464 (patch)
tree7450ccd86b32e04938e0fa72664c9aeb4a2bdc99 /src/util
parent35faafb32cae9bb48c68b3dd1f57bb820ed0605d (diff)
util/HugeAllocator: throw std::bad_alloc on error
Diffstat (limited to 'src/util')
-rw-r--r--src/util/HugeAllocator.cxx21
-rw-r--r--src/util/HugeAllocator.hxx27
-rw-r--r--src/util/SliceBuffer.hxx7
3 files changed, 30 insertions, 25 deletions
diff --git a/src/util/HugeAllocator.cxx b/src/util/HugeAllocator.cxx
index f26ffc814..bbff91650 100644
--- a/src/util/HugeAllocator.cxx
+++ b/src/util/HugeAllocator.cxx
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2013 Max Kellermann <max@duempel.org>
+ * Copyright (C) 2013-2016 Max Kellermann <max@duempel.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -54,7 +54,7 @@ AlignToPageSize(size_t size)
}
void *
-HugeAllocate(size_t size)
+HugeAllocate(size_t size) throw(std::bad_alloc)
{
size = AlignToPageSize(size);
@@ -63,7 +63,7 @@ HugeAllocate(size_t size)
PROT_READ|PROT_WRITE, flags,
-1, 0);
if (p == (void *)-1)
- return nullptr;
+ throw std::bad_alloc();
#ifdef MADV_HUGEPAGE
/* allow the Linux kernel to use "Huge Pages", which reduces page
@@ -94,4 +94,19 @@ HugeDiscard(void *p, size_t size)
#endif
}
+#elif defined(WIN32)
+
+void *
+HugeAllocate(size_t size) throw(std::bad_alloc)
+{
+ // TODO: use MEM_LARGE_PAGES
+ void *p = VirtualAlloc(nullptr, size,
+ MEM_COMMIT|MEM_RESERVE,
+ PAGE_READWRITE);
+ if (p == nullptr)
+ throw std::bad_alloc();
+
+ return p;
+}
+
#endif
diff --git a/src/util/HugeAllocator.hxx b/src/util/HugeAllocator.hxx
index fa45e5610..7619c5b96 100644
--- a/src/util/HugeAllocator.hxx
+++ b/src/util/HugeAllocator.hxx
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2013 Max Kellermann <max@duempel.org>
+ * Copyright (C) 2013-2016 Max Kellermann <max@duempel.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -32,6 +32,8 @@
#include "Compiler.h"
+#include <new>
+
#include <stddef.h>
#ifdef __linux__
@@ -43,7 +45,7 @@
*/
gcc_malloc
void *
-HugeAllocate(size_t size);
+HugeAllocate(size_t size) throw(std::bad_alloc);
/**
* @param p an allocation returned by HugeAllocate()
@@ -67,14 +69,8 @@ HugeDiscard(void *p, size_t size);
#include <windows.h>
gcc_malloc
-static inline void *
-HugeAllocate(size_t size)
-{
- // TODO: use MEM_LARGE_PAGES
- return VirtualAlloc(nullptr, size,
- MEM_COMMIT|MEM_RESERVE,
- PAGE_READWRITE);
-}
+void *
+HugeAllocate(size_t size) throw(std::bad_alloc);
static inline void
HugeFree(void *p, gcc_unused size_t size)
@@ -92,19 +88,20 @@ HugeDiscard(void *p, size_t size)
/* not Linux: fall back to standard C calls */
-#include <stdlib.h>
+#include <stdint.h>
gcc_malloc
static inline void *
-HugeAllocate(size_t size)
+HugeAllocate(size_t size) throw(std::bad_alloc)
{
- return malloc(size);
+ return new uint8_t[size];
}
static inline void
-HugeFree(void *p, size_t)
+HugeFree(void *_p, size_t)
{
- free(p);
+ auto *p = (uint8_t *)_p;
+ delete[] p;
}
static inline void
diff --git a/src/util/SliceBuffer.hxx b/src/util/SliceBuffer.hxx
index f37ff7659..c3ff51840 100644
--- a/src/util/SliceBuffer.hxx
+++ b/src/util/SliceBuffer.hxx
@@ -88,13 +88,6 @@ public:
SliceBuffer(const SliceBuffer &other) = delete;
SliceBuffer &operator=(const SliceBuffer &other) = delete;
- /**
- * @return true if buffer allocation (by the constructor) has failed
- */
- bool IsOOM() {
- return data == nullptr;
- }
-
unsigned GetCapacity() const {
return n_max;
}