blob: 4c8a35e0f71b4ffa87c7a2f2731d202a496c1971 [file] [log] [blame]
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -07001/*
2 * Copyright (C) 2011 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#ifndef ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_INL_H_
18#define ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_INL_H_
19
20#include "dlmalloc_space.h"
Ian Rogers6fac4472014-02-25 17:01:10 -080021#include "gc/allocator/dlmalloc.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070022#include "thread.h"
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070023
24namespace art {
25namespace gc {
26namespace space {
27
28inline mirror::Object* DlMallocSpace::AllocNonvirtual(Thread* self, size_t num_bytes,
Ian Rogers6fac4472014-02-25 17:01:10 -080029 size_t* bytes_allocated,
30 size_t* usable_size) {
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070031 mirror::Object* obj;
32 {
33 MutexLock mu(self, lock_);
Ian Rogers6fac4472014-02-25 17:01:10 -080034 obj = AllocWithoutGrowthLocked(self, num_bytes, bytes_allocated, usable_size);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070035 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070036 if (LIKELY(obj != NULL)) {
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070037 // Zero freshly allocated memory, done while not holding the space's lock.
38 memset(obj, 0, num_bytes);
39 }
40 return obj;
41}
42
Ian Rogers6fac4472014-02-25 17:01:10 -080043inline size_t DlMallocSpace::AllocationSizeNonvirtual(mirror::Object* obj, size_t* usable_size) {
44 void* obj_ptr = const_cast<void*>(reinterpret_cast<const void*>(obj));
45 size_t size = mspace_usable_size(obj_ptr);
46 if (usable_size != nullptr) {
47 *usable_size = size;
48 }
49 return size + kChunkOverhead;
50}
51
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070052inline mirror::Object* DlMallocSpace::AllocWithoutGrowthLocked(Thread* /*self*/, size_t num_bytes,
Ian Rogers6fac4472014-02-25 17:01:10 -080053 size_t* bytes_allocated,
54 size_t* usable_size) {
Mathieu Chartier31f44142014-04-08 14:40:03 -070055 mirror::Object* result = reinterpret_cast<mirror::Object*>(mspace_malloc(mspace_, num_bytes));
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070056 if (LIKELY(result != NULL)) {
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070057 if (kDebugSpaces) {
58 CHECK(Contains(result)) << "Allocation (" << reinterpret_cast<void*>(result)
59 << ") not in bounds of allocation space " << *this;
60 }
Ian Rogers6fac4472014-02-25 17:01:10 -080061 size_t allocation_size = AllocationSizeNonvirtual(result, usable_size);
Mathieu Chartiereb5710e2013-07-25 15:19:42 -070062 DCHECK(bytes_allocated != NULL);
63 *bytes_allocated = allocation_size;
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070064 }
65 return result;
66}
67
68} // namespace space
69} // namespace gc
70} // namespace art
71
72#endif // ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_INL_H_