• 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 #pragma GCC diagnostic ignored "-Wexpansion-to-defined"
42 #include "../../../external/dlmalloc/malloc.c"
43 // Note: malloc.c uses a DEBUG define to drive debug code. This interferes with the DEBUG severity
44 //       of libbase, so undefine it now.
45 #undef DEBUG
46 #pragma GCC diagnostic pop
47 
art_heap_morecore(void * m,intptr_t increment)48 static void* art_heap_morecore(void* m, intptr_t increment) {
49   return ::art::gc::allocator::ArtDlMallocMoreCore(m, increment);
50 }
51 
art_heap_corruption(const char * function)52 static void art_heap_corruption(const char* function) {
53   LOG(FATAL) << "Corrupt heap detected in: " << function;
54 }
55 
art_heap_usage_error(const char * function,void * p)56 static void art_heap_usage_error(const char* function, void* p) {
57   LOG(FATAL) << "Incorrect use of function '" << function << "' argument " << p
58       << " not expected";
59 }
60 
61 #include <sys/mman.h>
62 
63 #include "base/utils.h"
64 #include "runtime_globals.h"
65 
DlmallocMadviseCallback(void * start,void * end,size_t used_bytes,void * arg)66 extern "C" void DlmallocMadviseCallback(void* start, void* end, size_t used_bytes, void* arg) {
67   // Is this chunk in use?
68   if (used_bytes != 0) {
69     return;
70   }
71   // Do we have any whole pages to give back?
72   start = reinterpret_cast<void*>(art::RoundUp(reinterpret_cast<uintptr_t>(start), art::kPageSize));
73   end = reinterpret_cast<void*>(art::RoundDown(reinterpret_cast<uintptr_t>(end), art::kPageSize));
74   if (end > start) {
75     size_t length = reinterpret_cast<uint8_t*>(end) - reinterpret_cast<uint8_t*>(start);
76     int rc = madvise(start, length, MADV_DONTNEED);
77     if (UNLIKELY(rc != 0)) {
78       errno = rc;
79       PLOG(FATAL) << "madvise failed during heap trimming";
80     }
81     size_t* reclaimed = reinterpret_cast<size_t*>(arg);
82     *reclaimed += length;
83   }
84 }
85 
DlmallocBytesAllocatedCallback(void * start ATTRIBUTE_UNUSED,void * end ATTRIBUTE_UNUSED,size_t used_bytes,void * arg)86 extern "C" void DlmallocBytesAllocatedCallback(void* start ATTRIBUTE_UNUSED,
87                                                void* end ATTRIBUTE_UNUSED,
88                                                size_t used_bytes,
89                                                void* arg) {
90   if (used_bytes == 0) {
91     return;
92   }
93   size_t* bytes_allocated = reinterpret_cast<size_t*>(arg);
94   *bytes_allocated += used_bytes + sizeof(size_t);
95 }
96 
DlmallocObjectsAllocatedCallback(void * start ATTRIBUTE_UNUSED,void * end ATTRIBUTE_UNUSED,size_t used_bytes,void * arg)97 extern "C" void DlmallocObjectsAllocatedCallback(void* start ATTRIBUTE_UNUSED,
98                                                  void* end ATTRIBUTE_UNUSED,
99                                                  size_t used_bytes,
100                                                  void* arg) {
101   if (used_bytes == 0) {
102     return;
103   }
104   size_t* objects_allocated = reinterpret_cast<size_t*>(arg);
105   ++(*objects_allocated);
106 }
107