Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2013 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 "allocator.h"
     18 
     19 #include <inttypes.h>
     20 #include <stdlib.h>
     21 
     22 #include "atomic.h"
     23 #include "base/logging.h"
     24 #include "thread-inl.h"
     25 
     26 namespace art {
     27 
     28 Atomic<uint64_t> TrackedAllocators::bytes_used_[kAllocatorTagCount];
     29 Atomic<uint64_t> TrackedAllocators::max_bytes_used_[kAllocatorTagCount];
     30 Atomic<uint64_t> TrackedAllocators::total_bytes_used_[kAllocatorTagCount];
     31 
     32 class MallocAllocator : public Allocator {
     33  public:
     34   explicit MallocAllocator() {}
     35   ~MallocAllocator() {}
     36 
     37   virtual void* Alloc(size_t size) {
     38     return calloc(sizeof(uint8_t), size);
     39   }
     40 
     41   virtual void Free(void* p) {
     42     free(p);
     43   }
     44 
     45  private:
     46   DISALLOW_COPY_AND_ASSIGN(MallocAllocator);
     47 };
     48 
     49 MallocAllocator g_malloc_allocator;
     50 
     51 class NoopAllocator : public Allocator {
     52  public:
     53   explicit NoopAllocator() {}
     54   ~NoopAllocator() {}
     55 
     56   virtual void* Alloc(size_t size) {
     57     LOG(FATAL) << "NoopAllocator::Alloc should not be called";
     58     return NULL;
     59   }
     60 
     61   virtual void Free(void* p) {
     62     // Noop.
     63   }
     64 
     65  private:
     66   DISALLOW_COPY_AND_ASSIGN(NoopAllocator);
     67 };
     68 
     69 NoopAllocator g_noop_allocator;
     70 
     71 Allocator* Allocator::GetMallocAllocator() {
     72   return &g_malloc_allocator;
     73 }
     74 
     75 Allocator* Allocator::GetNoopAllocator() {
     76   return &g_noop_allocator;
     77 }
     78 
     79 void TrackedAllocators::Dump(std::ostream& os) {
     80   if (kEnableTrackingAllocator) {
     81     os << "Dumping native memory usage\n";
     82     for (size_t i = 0; i < kAllocatorTagCount; ++i) {
     83       uint64_t bytes_used = bytes_used_[i].LoadRelaxed();
     84       uint64_t max_bytes_used = max_bytes_used_[i].LoadRelaxed();
     85       uint64_t total_bytes_used = total_bytes_used_[i].LoadRelaxed();
     86       if (total_bytes_used != 0) {
     87         os << static_cast<AllocatorTag>(i) << " active=" << bytes_used << " max="
     88            << max_bytes_used << " total=" << total_bytes_used << "\n";
     89       }
     90     }
     91   }
     92 }
     93 
     94 }  // namespace art
     95