1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include "dlmalloc.h"
18
19 #include "base/bit_utils.h"
20 #include "base/logging.h"
21
22 // ART specific morecore implementation defined in space.cc.
23 static void* art_heap_morecore(void* m, intptr_t increment);
24 #define MORECORE(x) art_heap_morecore(m, x)
25
26 // Custom heap error handling.
27 #define PROCEED_ON_ERROR 0
28 static void art_heap_corruption(const char* function);
29 static void art_heap_usage_error(const char* function, void* p);
30 #define CORRUPTION_ERROR_ACTION(m) art_heap_corruption(__FUNCTION__)
31 #define USAGE_ERROR_ACTION(m, p) art_heap_usage_error(__FUNCTION__, p)
32
33 // Ugly inclusion of C file so that ART specific #defines configure dlmalloc for our use for
34 // mspaces (regular dlmalloc is still declared in bionic).
35 #pragma GCC diagnostic push
36 #pragma GCC diagnostic ignored "-Wredundant-decls"
37 #pragma GCC diagnostic ignored "-Wempty-body"
38 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
39 #include "../../../bionic/libc/upstream-dlmalloc/malloc.c"
40 #pragma GCC diagnostic pop
41
art_heap_morecore(void * m,intptr_t increment)42 static void* art_heap_morecore(void* m, intptr_t increment) {
43 return ::art::gc::allocator::ArtDlMallocMoreCore(m, increment);
44 }
45
art_heap_corruption(const char * function)46 static void art_heap_corruption(const char* function) {
47 LOG(::art::FATAL) << "Corrupt heap detected in: " << function;
48 }
49
art_heap_usage_error(const char * function,void * p)50 static void art_heap_usage_error(const char* function, void* p) {
51 LOG(::art::FATAL) << "Incorrect use of function '" << function << "' argument " << p
52 << " not expected";
53 }
54
55 #include "globals.h"
56 #include "utils.h"
57 #include <sys/mman.h>
58
DlmallocMadviseCallback(void * start,void * end,size_t used_bytes,void * arg)59 extern "C" void DlmallocMadviseCallback(void* start, void* end, size_t used_bytes, void* arg) {
60 // Is this chunk in use?
61 if (used_bytes != 0) {
62 return;
63 }
64 // Do we have any whole pages to give back?
65 start = reinterpret_cast<void*>(art::RoundUp(reinterpret_cast<uintptr_t>(start), art::kPageSize));
66 end = reinterpret_cast<void*>(art::RoundDown(reinterpret_cast<uintptr_t>(end), art::kPageSize));
67 if (end > start) {
68 size_t length = reinterpret_cast<uint8_t*>(end) - reinterpret_cast<uint8_t*>(start);
69 int rc = madvise(start, length, MADV_DONTNEED);
70 if (UNLIKELY(rc != 0)) {
71 errno = rc;
72 PLOG(::art::FATAL) << "madvise failed during heap trimming";
73 }
74 size_t* reclaimed = reinterpret_cast<size_t*>(arg);
75 *reclaimed += length;
76 }
77 }
78
DlmallocBytesAllocatedCallback(void * start ATTRIBUTE_UNUSED,void * end ATTRIBUTE_UNUSED,size_t used_bytes,void * arg)79 extern "C" void DlmallocBytesAllocatedCallback(void* start ATTRIBUTE_UNUSED,
80 void* end ATTRIBUTE_UNUSED, size_t used_bytes,
81 void* arg) {
82 if (used_bytes == 0) {
83 return;
84 }
85 size_t* bytes_allocated = reinterpret_cast<size_t*>(arg);
86 *bytes_allocated += used_bytes + sizeof(size_t);
87 }
88
DlmallocObjectsAllocatedCallback(void * start,void * end,size_t used_bytes,void * arg)89 extern "C" void DlmallocObjectsAllocatedCallback(void* start, void* end, size_t used_bytes,
90 void* arg) {
91 UNUSED(start);
92 UNUSED(end);
93 if (used_bytes == 0) {
94 return;
95 }
96 size_t* objects_allocated = reinterpret_cast<size_t*>(arg);
97 ++(*objects_allocated);
98 }
99