-
-
Save nielsmh/af22db0fc00f37afc857343f333e7c2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/core/alloc_func.hpp b/src/core/alloc_func.hpp | |
index c7e1742..3d958e4 100644 | |
--- a/src/core/alloc_func.hpp | |
+++ b/src/core/alloc_func.hpp | |
@@ -12,6 +12,8 @@ | |
#ifndef ALLOC_FUNC_HPP | |
#define ALLOC_FUNC_HPP | |
+#include <type_traits> | |
+ | |
/* | |
* Functions to exit badly with an error message. | |
* It has to be linked so the error messages are not | |
@@ -65,6 +67,8 @@ static inline T *MallocT(size_t num_elements) | |
*/ | |
if (num_elements == 0) return NULL; | |
+ static_assert(std::is_pod<T>(), "Malloc functions may only be used with POD types"); | |
+ | |
/* Ensure the size does not overflow. */ | |
CheckAllocationConstraints<T>(num_elements); | |
@@ -93,6 +97,8 @@ static inline T *CallocT(size_t num_elements) | |
*/ | |
if (num_elements == 0) return NULL; | |
+ static_assert(std::is_pod<T>(), "Malloc functions may only be used with POD types"); | |
+ | |
T *t_ptr = (T*)calloc(num_elements, sizeof(T)); | |
if (t_ptr == NULL) MallocError(num_elements * sizeof(T)); | |
return t_ptr; | |
@@ -122,6 +128,8 @@ static inline T *ReallocT(T *t_ptr, size_t num_elements) | |
return NULL; | |
} | |
+ static_assert(std::is_pod<T>(), "Malloc functions may only be used with POD types"); | |
+ | |
/* Ensure the size does not overflow. */ | |
CheckAllocationConstraints<T>(num_elements); | |
diff --git a/src/core/alloc_type.hpp b/src/core/alloc_type.hpp | |
index 9c25cc9..2c12ebd 100644 | |
--- a/src/core/alloc_type.hpp | |
+++ b/src/core/alloc_type.hpp | |
@@ -13,6 +13,7 @@ | |
#define ALLOC_TYPE_HPP | |
#include "alloc_func.hpp" | |
+#include <type_traits> | |
/** | |
* A small 'wrapper' for allocations that can be done on most OSes on the | |
@@ -191,7 +192,12 @@ class AutoFreePtr | |
public: | |
AutoFreePtr(T *ptr) : ptr(ptr) {} | |
- ~AutoFreePtr() { free(this->ptr); } | |
+ ~AutoFreePtr() | |
+ { | |
+ static_assert(std::is_pod<T>(), "Can only have auto-free() pointers of POD types"); | |
+ | |
+ free(this->ptr); | |
+ } | |
/** | |
* Take ownership of a new pointer and free the old one if needed. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment