1/* Copyright (c) 2008, Google Inc.
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * --
31 * Author: Craig Silverstein
32 *
33 * C shims for the C++ malloc_extension.h.  See malloc_extension.h for
34 * details.  Note these C shims always work on
35 * MallocExtension::instance(); it is not possible to have more than
36 * one MallocExtension object in C applications.
37 */
38
39#ifndef _MALLOC_EXTENSION_C_H_
40#define _MALLOC_EXTENSION_C_H_
41
42#include <stddef.h>
43#include <sys/types.h>
44
45/* Annoying stuff for windows -- makes sure clients can import these fns */
46#ifndef PERFTOOLS_DLL_DECL
47# ifdef _WIN32
48#   define PERFTOOLS_DLL_DECL  __declspec(dllimport)
49# else
50#   define PERFTOOLS_DLL_DECL
51# endif
52#endif
53
54#ifdef __cplusplus
55extern "C" {
56#endif
57
58#define kMallocExtensionHistogramSize 64
59
60PERFTOOLS_DLL_DECL int MallocExtension_VerifyAllMemory(void);
61PERFTOOLS_DLL_DECL int MallocExtension_VerifyNewMemory(const void* p);
62PERFTOOLS_DLL_DECL int MallocExtension_VerifyArrayNewMemory(const void* p);
63PERFTOOLS_DLL_DECL int MallocExtension_VerifyMallocMemory(const void* p);
64PERFTOOLS_DLL_DECL int MallocExtension_MallocMemoryStats(int* blocks, size_t* total,
65                                      int histogram[kMallocExtensionHistogramSize]);
66PERFTOOLS_DLL_DECL void MallocExtension_GetStats(char* buffer, int buffer_length);
67
68/* TODO(csilvers): write a C version of these routines, that perhaps
69 * takes a function ptr and a void *.
70 */
71/* void MallocExtension_GetHeapSample(string* result); */
72/* void MallocExtension_GetHeapGrowthStacks(string* result); */
73
74PERFTOOLS_DLL_DECL int MallocExtension_GetNumericProperty(const char* property, size_t* value);
75PERFTOOLS_DLL_DECL int MallocExtension_SetNumericProperty(const char* property, size_t value);
76PERFTOOLS_DLL_DECL void MallocExtension_MarkThreadIdle(void);
77PERFTOOLS_DLL_DECL void MallocExtension_MarkThreadBusy(void);
78PERFTOOLS_DLL_DECL void MallocExtension_ReleaseToSystem(size_t num_bytes);
79PERFTOOLS_DLL_DECL void MallocExtension_ReleaseFreeMemory(void);
80PERFTOOLS_DLL_DECL size_t MallocExtension_GetEstimatedAllocatedSize(size_t size);
81PERFTOOLS_DLL_DECL size_t MallocExtension_GetAllocatedSize(const void* p);
82
83/*
84 * NOTE: These enum values MUST be kept in sync with the version in
85 *       malloc_extension.h
86 */
87typedef enum {
88  MallocExtension_kUnknownOwnership = 0,
89  MallocExtension_kOwned,
90  MallocExtension_kNotOwned
91} MallocExtension_Ownership;
92
93PERFTOOLS_DLL_DECL MallocExtension_Ownership MallocExtension_GetOwnership(const void* p);
94
95#ifdef __cplusplus
96}   // extern "C"
97#endif
98
99#endif /* _MALLOC_EXTENSION_C_H_ */
100