aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/platform/windows/port.cc
diff options
context:
space:
mode:
Diffstat (limited to 'tensorflow/core/platform/windows/port.cc')
-rw-r--r--tensorflow/core/platform/windows/port.cc51
1 files changed, 47 insertions, 4 deletions
diff --git a/tensorflow/core/platform/windows/port.cc b/tensorflow/core/platform/windows/port.cc
index 3394524aa5..85b53e07c4 100644
--- a/tensorflow/core/platform/windows/port.cc
+++ b/tensorflow/core/platform/windows/port.cc
@@ -13,6 +13,10 @@ See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
+#ifdef TENSORFLOW_USE_JEMALLOC
+#include "jemalloc/jemalloc.h"
+#endif
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -53,16 +57,55 @@ int NumSchedulableCPUs() {
}
void* AlignedMalloc(size_t size, int minimum_alignment) {
+#ifdef TENSORFLOW_USE_JEMALLOC
+ void* ptr = NULL;
+ // posix_memalign requires that the requested alignment be at least
+ // sizeof(void*). In this case, fall back on malloc which should return
+ // memory aligned to at least the size of a pointer.
+ const int required_alignment = sizeof(void*);
+ if (minimum_alignment < required_alignment) return Malloc(size);
+ int err = jemalloc_posix_memalign(&ptr, minimum_alignment, size);
+ if (err != 0) {
+ return NULL;
+ } else {
+ return ptr;
+ }
+#else
return _aligned_malloc(size, minimum_alignment);
+#endif
}
-void AlignedFree(void* aligned_memory) { _aligned_free(aligned_memory); }
+void AlignedFree(void* aligned_memory) {
+#ifdef TENSORFLOW_USE_JEMALLOC
+ jemalloc_free(aligned_memory);
+#else
+ _aligned_free(aligned_memory);
+#endif
+}
-void* Malloc(size_t size) { return ::malloc(size); }
+void* Malloc(size_t size) {
+#ifdef TENSORFLOW_USE_JEMALLOC
+ return jemalloc_malloc(size);
+#else
+ return malloc(size);
+#endif
+}
-void* Realloc(void* ptr, size_t size) { return ::realloc(ptr, size); }
+void* Realloc(void* ptr, size_t size) {
+#ifdef TENSORFLOW_USE_JEMALLOC
+ return jemalloc_realloc(ptr, size);
+#else
+ return realloc(ptr, size);
+#endif
+}
-void Free(void* ptr) { ::free(ptr); }
+void Free(void* ptr) {
+#ifdef TENSORFLOW_USE_JEMALLOC
+ return jemalloc_free(ptr);
+#else
+ return free(ptr);
+#endif
+}
void MallocExtension_ReleaseToSystem(std::size_t num_bytes) {
// No-op.