1 /*
2  * Copyright (C) 2016 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 "chre/platform/memory.h"
18 #include "chre/platform/slpi/memory.h"
19 
20 #ifdef CHRE_SLPI_SEE
21 #include "chre/platform/slpi/see/island_vote_client.h"
22 #endif
23 
24 #include <cstdlib>
25 
26 extern "C" {
27 
28 #ifdef CHRE_SLPI_SEE
29 #include "sns_island_util.h"
30 #endif  // CHRE_SLPI_SEE
31 
32 #if defined(CHRE_SLPI_SMGR) || defined (CHRE_SLPI_SEE)
33 #include "sns_memmgr.h"
34 #endif
35 
36 } // extern "C"
37 
38 namespace chre {
39 
memoryAlloc(size_t size)40 void *memoryAlloc(size_t size) {
41 #ifdef CHRE_SLPI_UIMG_ENABLED
42   #if defined(CHRE_SLPI_SMGR)
43     return SNS_OS_U_MALLOC(SNS_CHRE, size);
44   #elif defined(CHRE_SLPI_SEE)
45     void *ptr = sns_malloc(SNS_HEAP_CHRE_ISLAND, size);
46 
47     // Fall back to big image memory when uimg memory is exhausted.
48     // Must exclude size 0 as clients may not explicitly free memory of size 0,
49     // which may mistakenly hold the system in big image.
50     if (ptr == nullptr && size != 0) {
51       // Increment big image ref count to prevent system from entering uimg
52       // while big image memory is in use.
53       IslandVoteClientSingleton::get()->incrementBigImageRefCount();
54       ptr = memoryAllocBigImage(size);
55 
56       // Big image allocation failed too.
57       if (ptr == nullptr) {
58         IslandVoteClientSingleton::get()->decrementBigImageRefCount();
59       }
60     }
61 
62     return ptr;
63   #else
64     #error SLPI UIMG memory allocation not supported
65   #endif
66 #else
67   return malloc(size);
68 #endif // CHRE_SLPI_UIMG_ENABLED
69 }
70 
memoryAllocBigImage(size_t size)71 void *memoryAllocBigImage(size_t size) {
72   return malloc(size);
73 }
74 
palSystemApiMemoryAlloc(size_t size)75 void *palSystemApiMemoryAlloc(size_t size) {
76   return malloc(size);
77 }
78 
memoryFree(void * pointer)79 void memoryFree(void *pointer) {
80 #ifdef CHRE_SLPI_UIMG_ENABLED
81   #if defined(CHRE_SLPI_SMGR)
82     SNS_OS_FREE(pointer);
83   #elif defined(CHRE_SLPI_SEE)
84     if (sns_island_is_island_ptr(reinterpret_cast<intptr_t>(pointer))) {
85       sns_free(pointer);
86     } else {
87       memoryFreeBigImage(pointer);
88 
89       // Must exclude nullptr as it's excluded in memoryAlloc() as well.
90       // Note currently sns_island_is_island_ptr returns true for nullptr,
91       // so this mainly serves as a protection in case the implementation of
92       // sns_island_is_island_ptr changes in the future.
93       if (pointer != nullptr) {
94         IslandVoteClientSingleton::get()->decrementBigImageRefCount();
95       }
96     }
97   #else
98     #error SLPI UIMG memory free not supported
99   #endif
100 #else
101   free(pointer);
102 #endif // CHRE_SLPI_UIMG_ENABLED
103 }
104 
memoryFreeBigImage(void * pointer)105 void memoryFreeBigImage(void *pointer) {
106   free(pointer);
107 }
108 
palSystemApiMemoryFree(void * pointer)109 void palSystemApiMemoryFree(void *pointer) {
110   free(pointer);
111 }
112 
113 }  // namespace chre
114