2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // Some helper functions for mallctl.
21 #include <folly/Likely.h>
22 #include <folly/Malloc.h>
30 [[noreturn]] void handleMallctlError(const char* cmd, int err);
33 void mallctlHelper(const char* cmd, T* out, T* in) {
34 if (UNLIKELY(!usingJEMalloc())) {
35 throw std::logic_error("Calling mallctl when not using jemalloc.");
38 size_t outLen = sizeof(T);
39 int err = mallctl(cmd, out, out ? &outLen : nullptr, in, in ? sizeof(T) : 0);
40 if (UNLIKELY(err != 0)) {
41 handleMallctlError(cmd, err);
48 void mallctlRead(const char* cmd, T* out) {
49 detail::mallctlHelper(cmd, out, static_cast<T*>(nullptr));
53 void mallctlWrite(const char* cmd, T in) {
54 detail::mallctlHelper(cmd, static_cast<T*>(nullptr), &in);
58 void mallctlReadWrite(const char* cmd, T* out, T in) {
59 detail::mallctlHelper(cmd, out, &in);
62 inline void mallctlCall(const char* cmd) {
63 // Use <unsigned> rather than <void> to avoid sizeof(void).
64 mallctlRead<unsigned>(cmd, nullptr);