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 <memory>
18 #include <sys/mman.h>
19
20 #include <gtest/gtest.h>
21
22 #include <ion/ion.h>
23 #include "ion_test_fixture.h"
24
25 class Allocate : public IonAllHeapsTest {
26 };
27
TEST_F(Allocate,Allocate)28 TEST_F(Allocate, Allocate)
29 {
30 static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
31 for (unsigned int heapMask : m_allHeaps) {
32 for (size_t size : allocationSizes) {
33 SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
34 SCOPED_TRACE(::testing::Message() << "size " << size);
35 ion_user_handle_t handle = 0;
36 ASSERT_EQ(0, ion_alloc(m_ionFd, size, 0, heapMask, 0, &handle));
37 ASSERT_TRUE(handle != 0);
38 ASSERT_EQ(0, ion_free(m_ionFd, handle));
39 }
40 }
41 }
42
TEST_F(Allocate,AllocateCached)43 TEST_F(Allocate, AllocateCached)
44 {
45 static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
46 for (unsigned int heapMask : m_allHeaps) {
47 for (size_t size : allocationSizes) {
48 SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
49 SCOPED_TRACE(::testing::Message() << "size " << size);
50 ion_user_handle_t handle = 0;
51 ASSERT_EQ(0, ion_alloc(m_ionFd, size, 0, heapMask, ION_FLAG_CACHED, &handle));
52 ASSERT_TRUE(handle != 0);
53 ASSERT_EQ(0, ion_free(m_ionFd, handle));
54 }
55 }
56 }
57
TEST_F(Allocate,AllocateCachedNeedsSync)58 TEST_F(Allocate, AllocateCachedNeedsSync)
59 {
60 static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
61 for (unsigned int heapMask : m_allHeaps) {
62 for (size_t size : allocationSizes) {
63 SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
64 SCOPED_TRACE(::testing::Message() << "size " << size);
65 ion_user_handle_t handle = 0;
66 ASSERT_EQ(0, ion_alloc(m_ionFd, size, 0, heapMask, ION_FLAG_CACHED_NEEDS_SYNC, &handle));
67 ASSERT_TRUE(handle != 0);
68 ASSERT_EQ(0, ion_free(m_ionFd, handle));
69 }
70 }
71 }
72
TEST_F(Allocate,RepeatedAllocate)73 TEST_F(Allocate, RepeatedAllocate)
74 {
75 static const size_t allocationSizes[] = {4*1024, 64*1024, 1024*1024, 2*1024*1024};
76 for (unsigned int heapMask : m_allHeaps) {
77 for (size_t size : allocationSizes) {
78 SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
79 SCOPED_TRACE(::testing::Message() << "size " << size);
80 ion_user_handle_t handle = 0;
81
82 for (unsigned int i = 0; i < 1024; i++) {
83 SCOPED_TRACE(::testing::Message() << "iteration " << i);
84 ASSERT_EQ(0, ion_alloc(m_ionFd, size, 0, heapMask, 0, &handle));
85 ASSERT_TRUE(handle != 0);
86 ASSERT_EQ(0, ion_free(m_ionFd, handle));
87 }
88 }
89 }
90 }
91
TEST_F(Allocate,Zeroed)92 TEST_F(Allocate, Zeroed)
93 {
94 auto zeroes_ptr = std::make_unique<char[]>(4096);
95
96 for (unsigned int heapMask : m_allHeaps) {
97 SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
98 int fds[16];
99 for (unsigned int i = 0; i < 16; i++) {
100 int map_fd = -1;
101
102 ASSERT_EQ(0, ion_alloc_fd(m_ionFd, 4096, 0, heapMask, 0, &map_fd));
103 ASSERT_GE(map_fd, 0);
104
105 void *ptr = NULL;
106 ptr = mmap(NULL, 4096, PROT_WRITE, MAP_SHARED, map_fd, 0);
107 ASSERT_TRUE(ptr != NULL);
108
109 memset(ptr, 0xaa, 4096);
110
111 ASSERT_EQ(0, munmap(ptr, 4096));
112 fds[i] = map_fd;
113 }
114
115 for (unsigned int i = 0; i < 16; i++) {
116 ASSERT_EQ(0, close(fds[i]));
117 }
118
119 int newIonFd = ion_open();
120 int map_fd = -1;
121
122 ASSERT_EQ(0, ion_alloc_fd(newIonFd, 4096, 0, heapMask, 0, &map_fd));
123 ASSERT_GE(map_fd, 0);
124
125 void *ptr = NULL;
126 ptr = mmap(NULL, 4096, PROT_READ, MAP_SHARED, map_fd, 0);
127 ASSERT_TRUE(ptr != NULL);
128
129 ASSERT_EQ(0, memcmp(ptr, zeroes_ptr.get(), 4096));
130
131 ASSERT_EQ(0, munmap(ptr, 4096));
132 ASSERT_EQ(0, close(map_fd));
133 }
134 }
135
TEST_F(Allocate,Large)136 TEST_F(Allocate, Large)
137 {
138 for (unsigned int heapMask : m_allHeaps) {
139 SCOPED_TRACE(::testing::Message() << "heap " << heapMask);
140 ion_user_handle_t handle = 0;
141 ASSERT_EQ(-ENOMEM, ion_alloc(m_ionFd, 3UL*1024*1024*1024, 0, heapMask, 0, &handle));
142 }
143 }
144