/*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
// Functions to provide smarter use of jemalloc, if jemalloc is being used.
// http://www.canonware.com/download/jemalloc/jemalloc-latest/doc/jemalloc.html
-#ifndef FOLLY_MALLOC_H_
-#define FOLLY_MALLOC_H_
+#pragma once
/**
* Define various MALLOCX_* macros normally provided by jemalloc. We define
#if defined(_GLIBCXX_USE_FB) && !defined(_LIBSTDCXX_FBSTRING)
#include <folly/detail/Malloc.h>
+#include <folly/portability/BitsFunctexcept.h>
#include <string>
__attribute__((__weak__));
extern "C" void dallocx(void*, int)
__attribute__((__weak__));
+extern "C" void sdallocx(void*, size_t, int)
+__attribute__((__weak__));
extern "C" size_t nallocx(size_t, int)
__attribute__((__weak__));
extern "C" int mallctl(const char*, void*, size_t*, void*, size_t)
__attribute__((__weak__));
#include <bits/functexcept.h>
+
#define FOLLY_HAVE_MALLOC_H 1
-#else
+
+#else // !defined(_LIBSTDCXX_FBSTRING)
+
#include <folly/detail/Malloc.h> /* nolint */
+#include <folly/portability/BitsFunctexcept.h> /* nolint */
+
#endif
// for malloc_usable_size
-// NOTE: FreeBSD 9 doesn't have malloc.h. It's defitions
+// NOTE: FreeBSD 9 doesn't have malloc.h. Its definitions
// are found in stdlib.h.
#if FOLLY_HAVE_MALLOC_H
#include <malloc.h>
#endif
// Cannot depend on Portability.h when _LIBSTDCXX_FBSTRING.
-#ifdef __GNUC__
+// Disabled for nvcc because it fails on attributes on lambdas.
+#if defined(__GNUC__) && !defined(__NVCC__)
#define FOLLY_MALLOC_NOINLINE __attribute__((__noinline__))
#else
#define FOLLY_MALLOC_NOINLINE
// in the form if (mallctl != nullptr). Not if (mallctl) or if (!mallctl)
// (!!). http://goo.gl/xpmctm
if (mallocx == nullptr || rallocx == nullptr || xallocx == nullptr
- || sallocx == nullptr || dallocx == nullptr || nallocx == nullptr
- || mallctl == nullptr || mallctlnametomib == nullptr
- || mallctlbymib == nullptr) {
+ || sallocx == nullptr || dallocx == nullptr || sdallocx == nullptr
+ || nallocx == nullptr || mallctl == nullptr
+ || mallctlnametomib == nullptr || mallctlbymib == nullptr) {
return false;
}
return result;
}
-/**
- * For jemalloc's size classes, see
- * http://www.canonware.com/download/jemalloc/jemalloc-latest/doc/jemalloc.html
- */
inline size_t goodMallocSize(size_t minSize) noexcept {
+ if (minSize == 0) {
+ return 0;
+ }
+
if (!usingJEMalloc()) {
// Not using jemalloc - no smarts
return minSize;
}
- size_t goodSize;
- if (minSize <= 64) {
- // Choose smallest allocation to be 64 bytes - no tripping over
- // cache line boundaries, and small string optimization takes care
- // of short strings anyway.
- goodSize = 64;
- } else if (minSize <= 512) {
- // Round up to the next multiple of 64; we don't want to trip over
- // cache line boundaries.
- goodSize = (minSize + 63) & ~size_t(63);
- } else {
- // Boundaries between size classes depend on numerious factors, some of
- // which can even be modified at run-time. Determine the good allocation
- // size by calling nallocx() directly.
- goodSize = nallocx(minSize, 0);
- }
- assert(nallocx(goodSize, 0) == goodSize);
- return goodSize;
+
+ return nallocx(minSize, 0);
}
// We always request "good" sizes for allocation, so jemalloc can
} // folly
#endif // !defined(_GLIBCXX_USE_FB) || defined(_LIBSTDCXX_FBSTRING)
-
-#endif // FOLLY_MALLOC_H_