• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <android-base/logging.h>
20 
21 #include "base/bit_utils.h"
22 
23 // ART specific morecore implementation defined in space.cc.
24 static void* art_heap_morecore(void* m, intptr_t increment);
25 #define MORECORE(x) art_heap_morecore(m, x)
26 
27 // Custom heap error handling.
28 #define PROCEED_ON_ERROR 0
29 static void art_heap_corruption(const char* function);
30 static void art_heap_usage_error(const char* function, void* p);
31 #define CORRUPTION_ERROR_ACTION(m) art_heap_corruption(__FUNCTION__)
32 #define USAGE_ERROR_ACTION(m, p) art_heap_usage_error(__FUNCTION__, p)
33 
34 // Ugly inclusion of C file so that ART specific #defines configure dlmalloc for our use for
35 // mspaces (regular dlmalloc is still declared in bionic).
36 #pragma GCC diagnostic push
37 #pragma GCC diagnostic ignored "-Wredundant-decls"
38 #pragma GCC diagnostic ignored "-Wempty-body"
39 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
40 #pragma GCC diagnostic ignored "-Wnull-pointer-arithmetic"
41 #include "../../../external/dlmalloc/malloc.c"
42 // Note: malloc.c uses a DEBUG define to drive debug code. This interferes with the DEBUG severity
43 //       of libbase, so undefine it now.
44 #undef DEBUG
45 #pragma GCC diagnostic pop
46 
art_heap_morecore(void * m,intptr_t increment)47 static void* art_heap_morecore(void* m, intptr_t increment) {
48   return ::art::gc::allocator::ArtDlMallocMoreCore(m, increment);
49 }
50 
art_heap_corruption(const char * function)51 static void art_heap_corruption(const char* function) {
52   LOG(FATAL) << "Corrupt heap detected in: " << function;
53 }
54 
art_heap_usage_error(const char * function,void * p)55 static void art_heap_usage_error(const char* function, void* p) {
56   LOG(FATAL) << "Incorrect use of function '" << function << "' argument " << p
57       << " not expected";
58 }
59 
60 #include <sys/mman.h>
61 
62 #include "base/globals.h"
63 #include "base/utils.h"
64 
DlmallocMadviseCallback(void * start,void * end,size_t used_bytes,void * arg)65 extern "C" void DlmallocMadviseCallback(void* start, void* end, size_t used_bytes, void* arg) {
66   // Is this chunk in use?
67   if (used_bytes != 0) {
68     return;
69   }
70   // Do we have any whole pages to give back?
71   start = reinterpret_cast<void*>(art::RoundUp(reinterpret_cast<uintptr_t>(start), art::kPageSize));
72   end = reinterpret_cast<void*>(art::RoundDown(reinterpret_cast<uintptr_t>(end), art::kPageSize));
73   if (end > start) {
74     size_t length = reinterpret_cast<uint8_t*>(end) - reinterpret_cast<uint8_t*>(start);
75     int rc = madvise(start, length, MADV_DONTNEED);
76     if (UNLIKELY(rc != 0)) {
77       errno = rc;
78       PLOG(FATAL) << "madvise failed during heap trimming";
79     }
80     size_t* reclaimed = reinterpret_cast<size_t*>(arg);
81     *reclaimed += length;
82   }
83 }
84 
DlmallocBytesAllocatedCallback(void * start ATTRIBUTE_UNUSED,void * end ATTRIBUTE_UNUSED,size_t used_bytes,void * arg)85 extern "C" void DlmallocBytesAllocatedCallback(void* start ATTRIBUTE_UNUSED,
86                                                void* end ATTRIBUTE_UNUSED,
87                                                size_t used_bytes,
88                                                void* arg) {
89   if (used_bytes == 0) {
90     return;
91   }
92   size_t* bytes_allocated = reinterpret_cast<size_t*>(arg);
93   *bytes_allocated += used_bytes + sizeof(size_t);
94 }
95 
DlmallocObjectsAllocatedCallback(void * start ATTRIBUTE_UNUSED,void * end ATTRIBUTE_UNUSED,size_t used_bytes,void * arg)96 extern "C" void DlmallocObjectsAllocatedCallback(void* start ATTRIBUTE_UNUSED,
97                                                  void* end ATTRIBUTE_UNUSED,
98                                                  size_t used_bytes,
99                                                  void* arg) {
100   if (used_bytes == 0) {
101     return;
102   }
103   size_t* objects_allocated = reinterpret_cast<size_t*>(arg);
104   ++(*objects_allocated);
105 }
106