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