• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_UIMG_ENABLED
29 #include "sns_island_util.h"
30 #include "sns_memmgr.h"
31 #endif  // CHRE_SLPI_UIMG_ENABLED
32 
33 }  // extern "C"
34 
35 namespace chre {
36 
memoryAlloc(size_t size)37 void *memoryAlloc(size_t size) {
38 #ifdef CHRE_SLPI_UIMG_ENABLED
39   void *ptr = sns_malloc(SNS_HEAP_CHRE_ISLAND, size);
40 
41   // Fall back to big image memory when uimg memory is exhausted.
42   // Must exclude size 0 as clients may not explicitly free memory of size 0,
43   // which may mistakenly hold the system in big image.
44   if (ptr == nullptr && size != 0) {
45     // Increment big image ref count to prevent system from entering uimg
46     // while big image memory is in use.
47     IslandVoteClientSingleton::get()->incrementBigImageRefCount();
48     ptr = memoryAllocBigImage(size);
49 
50     // Big image allocation failed too.
51     if (ptr == nullptr) {
52       IslandVoteClientSingleton::get()->decrementBigImageRefCount();
53     }
54   }
55 
56   return ptr;
57 #else
58   return malloc(size);
59 #endif  // CHRE_SLPI_UIMG_ENABLED
60 }
61 
memoryAllocBigImage(size_t size)62 void *memoryAllocBigImage(size_t size) {
63   return malloc(size);
64 }
65 
palSystemApiMemoryAlloc(size_t size)66 void *palSystemApiMemoryAlloc(size_t size) {
67   return malloc(size);
68 }
69 
memoryFree(void * pointer)70 void memoryFree(void *pointer) {
71 #ifdef CHRE_SLPI_UIMG_ENABLED
72   if (sns_island_is_island_ptr(reinterpret_cast<intptr_t>(pointer))) {
73     sns_free(pointer);
74   } else {
75     memoryFreeBigImage(pointer);
76 
77     // Must exclude nullptr as it's excluded in memoryAlloc() as well.
78     // Note currently sns_island_is_island_ptr returns true for nullptr,
79     // so this mainly serves as a protection in case the implementation of
80     // sns_island_is_island_ptr changes in the future.
81     if (pointer != nullptr) {
82       IslandVoteClientSingleton::get()->decrementBigImageRefCount();
83     }
84   }
85 #else
86   free(pointer);
87 #endif  // CHRE_SLPI_UIMG_ENABLED
88 }
89 
memoryFreeBigImage(void * pointer)90 void memoryFreeBigImage(void *pointer) {
91   free(pointer);
92 }
93 
palSystemApiMemoryFree(void * pointer)94 void palSystemApiMemoryFree(void *pointer) {
95   free(pointer);
96 }
97 
98 }  // namespace chre
99