Blame mfbt/TaggedAnonymousMemory.h

Packit f0b94e
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Packit f0b94e
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
Packit f0b94e
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit f0b94e
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
// Some Linux kernels -- specifically, newer versions of Android and
Packit f0b94e
// some B2G devices -- have a feature for assigning names to ranges of
Packit f0b94e
// anonymous memory (i.e., memory that doesn't have a "name" in the
Packit f0b94e
// form of an underlying mapped file).  These names are reported in
Packit f0b94e
// /proc/<pid>/smaps alongside system-level memory usage information
Packit f0b94e
// such as Proportional Set Size (memory usage adjusted for sharing
Packit f0b94e
// between processes), which allows reporting this information at a
Packit f0b94e
// finer granularity than would otherwise be possible (e.g.,
Packit f0b94e
// separating malloc() heap from JS heap).
Packit f0b94e
//
Packit f0b94e
// Existing memory can be tagged with MozTagAnonymousMemory(); it will
Packit f0b94e
// tag the range of complete pages containing the given interval, so
Packit f0b94e
// the results may be inexact if the range isn't page-aligned.
Packit f0b94e
// MozTaggedAnonymousMmap() can be used like mmap() with an extra
Packit f0b94e
// parameter, and will tag the returned memory if the mapping was
Packit f0b94e
// successful (and if it was in fact anonymous).
Packit f0b94e
//
Packit f0b94e
// NOTE: The pointer given as the "tag" argument MUST remain valid as
Packit f0b94e
// long as the mapping exists.  The referenced string is read when
Packit f0b94e
// /proc/<pid>/smaps or /proc/<pid>/maps is read, not when the tag is
Packit f0b94e
// established, so freeing it or changing its contents will have
Packit f0b94e
// unexpected results.  Using a static string is probably best.
Packit f0b94e
//
Packit f0b94e
// Also note that this header can be used by both C and C++ code.
Packit f0b94e
Packit f0b94e
#ifndef mozilla_TaggedAnonymousMemory_h
Packit f0b94e
#define mozilla_TaggedAnonymousMemory_h
Packit f0b94e
Packit f0b94e
#ifndef XP_WIN
Packit f0b94e
Packit f0b94e
#include <sys/types.h>
Packit f0b94e
#include <sys/mman.h>
Packit f0b94e
Packit f0b94e
#include "mozilla/Types.h"
Packit f0b94e
Packit f0b94e
#ifdef ANDROID
Packit f0b94e
Packit f0b94e
#ifdef __cplusplus
Packit f0b94e
extern "C" {
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
MFBT_API void MozTagAnonymousMemory(const void* aPtr, size_t aLength,
Packit f0b94e
                                    const char* aTag);
Packit f0b94e
Packit f0b94e
MFBT_API void* MozTaggedAnonymousMmap(void* aAddr, size_t aLength, int aProt,
Packit f0b94e
                                      int aFlags, int aFd, off_t aOffset,
Packit f0b94e
                                      const char* aTag);
Packit f0b94e
Packit f0b94e
MFBT_API int MozTaggedMemoryIsSupported(void);
Packit f0b94e
Packit f0b94e
#ifdef __cplusplus
Packit f0b94e
}  // extern "C"
Packit f0b94e
#endif
Packit f0b94e
Packit f0b94e
#else  // ANDROID
Packit f0b94e
Packit f0b94e
static inline void MozTagAnonymousMemory(const void* aPtr, size_t aLength,
Packit f0b94e
                                         const char* aTag) {}
Packit f0b94e
Packit f0b94e
static inline void* MozTaggedAnonymousMmap(void* aAddr, size_t aLength,
Packit f0b94e
                                           int aProt, int aFlags, int aFd,
Packit f0b94e
                                           off_t aOffset, const char* aTag) {
Packit f0b94e
  return mmap(aAddr, aLength, aProt, aFlags, aFd, aOffset);
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
static inline int MozTaggedMemoryIsSupported(void) { return 0; }
Packit f0b94e
Packit f0b94e
#endif  // ANDROID
Packit f0b94e
Packit f0b94e
#endif  // !XP_WIN
Packit f0b94e
Packit f0b94e
#endif  // mozilla_TaggedAnonymousMemory_h