allocator_extension.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_ALLOCATOR_ALLOCATOR_EXTENSION_H_
  5. #define BASE_ALLOCATOR_ALLOCATOR_EXTENSION_H_
  6. #include <stddef.h> // for size_t
  7. #include <string>
  8. #include "base/base_export.h"
  9. #include "build/build_config.h"
  10. namespace base {
  11. namespace allocator {
  12. // Callback types for alloc and free.
  13. using AllocHookFunc = void (*)(const void*, size_t);
  14. using FreeHookFunc = void (*)(const void*);
  15. // Request that the allocator release any free memory it knows about to the
  16. // system.
  17. BASE_EXPORT void ReleaseFreeMemory();
  18. // Get the named property's |value|. Returns true if the property is known.
  19. // Returns false if the property is not a valid property name for the current
  20. // allocator implementation.
  21. // |name| or |value| cannot be NULL
  22. BASE_EXPORT bool GetNumericProperty(const char* name, size_t* value);
  23. // Set the named property's |value|. Returns true if the property is known and
  24. // writable. Returns false if the property is not a valid property name for the
  25. // current allocator implementation, or is not writable. |name| cannot be NULL.
  26. BASE_EXPORT bool SetNumericProperty(const char* name, size_t value);
  27. // Outputs to |writer| a sample of live objects and the stack traces
  28. // that allocated these objects. The format of the returned output
  29. // is equivalent to the output of the heap profiler and can
  30. // therefore be passed to "pprof".
  31. // NOTE: by default, the allocator does not do any heap sampling, and this
  32. // function will always return an empty sample. To get useful
  33. // data from GetHeapSample, you must also set the numeric property
  34. // "tcmalloc.sampling_period_bytes" to a value such as 524288.
  35. BASE_EXPORT void GetHeapSample(std::string* writer);
  36. BASE_EXPORT bool IsHeapProfilerRunning();
  37. // Register callbacks for alloc and free. Can only store one callback at a time
  38. // for each of alloc and free.
  39. BASE_EXPORT void SetHooks(AllocHookFunc alloc_hook, FreeHookFunc free_hook);
  40. // Attempts to unwind the call stack from the current location where this
  41. // function is being called from. Must be called from a hook function registered
  42. // by calling SetSingle{Alloc,Free}Hook, directly or indirectly.
  43. //
  44. // Arguments:
  45. // stack: pointer to a pre-allocated array of void*'s.
  46. // max_stack_size: indicates the size of the array in |stack|.
  47. //
  48. // Returns the number of call stack frames stored in |stack|, or 0 if no call
  49. // stack information is available.
  50. BASE_EXPORT int GetCallStack(void** stack, int max_stack_size);
  51. } // namespace allocator
  52. } // namespace base
  53. #endif // BASE_ALLOCATOR_ALLOCATOR_EXTENSION_H_