150b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi/*
250b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi * Copyright (C) 2011 The Android Open Source Project
350b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi *
450b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi * Licensed under the Apache License, Version 2.0 (the "License");
550b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi * you may not use this file except in compliance with the License.
650b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi * You may obtain a copy of the License at
750b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi *
850b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi *      http://www.apache.org/licenses/LICENSE-2.0
950b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi *
1050b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi * Unless required by applicable law or agreed to in writing, software
1150b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi * distributed under the License is distributed on an "AS IS" BASIS,
1250b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1350b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi * See the License for the specific language governing permissions and
1450b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi * limitations under the License.
1550b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi */
1650b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi
1750b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi#ifndef ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_INL_H_
1850b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi#define ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_INL_H_
1950b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi
2050b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi#include "dlmalloc_space.h"
216fac447555dc94a935b78198479cce645c837b89Ian Rogers#include "gc/allocator/dlmalloc.h"
22cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi#include "thread.h"
2350b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi
2450b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchinamespace art {
2550b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchinamespace gc {
2650b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchinamespace space {
2750b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi
2850b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchiinline mirror::Object* DlMallocSpace::AllocNonvirtual(Thread* self, size_t num_bytes,
296fac447555dc94a935b78198479cce645c837b89Ian Rogers                                                      size_t* bytes_allocated,
304460a84be92b5a94ecfb5c650aef4945ab849c93Hiroshi Yamauchi                                                      size_t* usable_size,
314460a84be92b5a94ecfb5c650aef4945ab849c93Hiroshi Yamauchi                                                      size_t* bytes_tl_bulk_allocated) {
3250b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi  mirror::Object* obj;
3350b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi  {
3450b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi    MutexLock mu(self, lock_);
354460a84be92b5a94ecfb5c650aef4945ab849c93Hiroshi Yamauchi    obj = AllocWithoutGrowthLocked(self, num_bytes, bytes_allocated, usable_size,
364460a84be92b5a94ecfb5c650aef4945ab849c93Hiroshi Yamauchi                                   bytes_tl_bulk_allocated);
3750b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi  }
382cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  if (LIKELY(obj != nullptr)) {
3950b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi    // Zero freshly allocated memory, done while not holding the space's lock.
4050b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi    memset(obj, 0, num_bytes);
4150b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi  }
4250b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi  return obj;
4350b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi}
4450b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi
456fac447555dc94a935b78198479cce645c837b89Ian Rogersinline size_t DlMallocSpace::AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size) {
466fac447555dc94a935b78198479cce645c837b89Ian Rogers  void* obj_ptr = const_cast<void*>(reinterpret_cast<const void*>(obj));
476fac447555dc94a935b78198479cce645c837b89Ian Rogers  size_t size = mspace_usable_size(obj_ptr);
486fac447555dc94a935b78198479cce645c837b89Ian Rogers  if (usable_size != nullptr) {
496fac447555dc94a935b78198479cce645c837b89Ian Rogers    *usable_size = size;
506fac447555dc94a935b78198479cce645c837b89Ian Rogers  }
516fac447555dc94a935b78198479cce645c837b89Ian Rogers  return size + kChunkOverhead;
526fac447555dc94a935b78198479cce645c837b89Ian Rogers}
536fac447555dc94a935b78198479cce645c837b89Ian Rogers
544460a84be92b5a94ecfb5c650aef4945ab849c93Hiroshi Yamauchiinline mirror::Object* DlMallocSpace::AllocWithoutGrowthLocked(
554460a84be92b5a94ecfb5c650aef4945ab849c93Hiroshi Yamauchi    Thread* /*self*/, size_t num_bytes,
564460a84be92b5a94ecfb5c650aef4945ab849c93Hiroshi Yamauchi    size_t* bytes_allocated,
574460a84be92b5a94ecfb5c650aef4945ab849c93Hiroshi Yamauchi    size_t* usable_size,
584460a84be92b5a94ecfb5c650aef4945ab849c93Hiroshi Yamauchi    size_t* bytes_tl_bulk_allocated) {
5931f441464c0c8f840aba37e236ad133f30308d70Mathieu Chartier  mirror::Object* result = reinterpret_cast<mirror::Object*>(mspace_malloc(mspace_, num_bytes));
602cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  if (LIKELY(result != nullptr)) {
6150b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi    if (kDebugSpaces) {
6250b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi      CHECK(Contains(result)) << "Allocation (" << reinterpret_cast<void*>(result)
6350b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi            << ") not in bounds of allocation space " << *this;
6450b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi    }
656fac447555dc94a935b78198479cce645c837b89Ian Rogers    size_t allocation_size = AllocationSizeNonvirtual(result, usable_size);
662cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier    DCHECK(bytes_allocated != nullptr);
67eb5710eba75bf338da56386ca29039df9d5134cbMathieu Chartier    *bytes_allocated = allocation_size;
684460a84be92b5a94ecfb5c650aef4945ab849c93Hiroshi Yamauchi    *bytes_tl_bulk_allocated = allocation_size;
6950b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi  }
7050b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi  return result;
7150b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi}
7250b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi
7350b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi}  // namespace space
7450b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi}  // namespace gc
7550b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi}  // namespace art
7650b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi
7750b2928501fe489c108472e7648ec98cdca62e10Hiroshi Yamauchi#endif  // ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_INL_H_
78