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