]> git.ipfire.org Git - thirdparty/pdns.git/blobdiff - pdns/lazy_allocator.hh
Merge pull request #9073 from pieterlexis/runtime-dirs-virtual-hosting
[thirdparty/pdns.git] / pdns / lazy_allocator.hh
index 4851687ba6f3c1079533b0325119f70e99b7c14c..986f91bdaed8c0f22dad20c14d5f7968a461a15d 100644 (file)
@@ -19,9 +19,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
-#ifndef LAZY_ALLOCATOR_HH
-#define LAZY_ALLOCATOR_HH
-
+#pragma once
 #include <cstddef>
 #include <utility>
 #include <type_traits>
@@ -43,16 +41,29 @@ struct lazy_allocator {
 
     pointer
     allocate (size_type const n) {
+#ifdef __OpenBSD__
         void *p = mmap(nullptr, n * sizeof(value_type),
           PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_STACK, -1, 0);
         if (p == MAP_FAILED)
           throw std::bad_alloc();
         return static_cast<pointer>(p);
+#else
+        return static_cast<pointer>(::operator new (n * sizeof(value_type)));
+#endif
     }
 
     void
     deallocate (pointer const ptr, size_type const n) noexcept {
+#ifdef __OpenBSD__
         munmap(ptr, n * sizeof(value_type));
+#else
+#if defined(__cpp_sized_deallocation) &&  (__cpp_sized_deallocation >= 201309)
+        ::operator delete (ptr, n * sizeof(value_type));
+#else
+        (void) n;
+        ::operator delete (ptr);
+#endif
+#endif
     }
 
     void construct (T*) const noexcept {}
@@ -73,5 +84,3 @@ template <typename T> inline
 bool operator!= (lazy_allocator<T> const&, lazy_allocator<T> const&) noexcept {
     return false;
 }
-
-#endif // LAZY_ALLOCATOR_HH