1*84e33947SAndroid Build Coastguard Worker /* 2*84e33947SAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project 3*84e33947SAndroid Build Coastguard Worker * 4*84e33947SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*84e33947SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*84e33947SAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*84e33947SAndroid Build Coastguard Worker * 8*84e33947SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*84e33947SAndroid Build Coastguard Worker * 10*84e33947SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*84e33947SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*84e33947SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*84e33947SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*84e33947SAndroid Build Coastguard Worker * limitations under the License. 15*84e33947SAndroid Build Coastguard Worker */ 16*84e33947SAndroid Build Coastguard Worker 17*84e33947SAndroid Build Coastguard Worker #include "chre/platform/memory.h" 18*84e33947SAndroid Build Coastguard Worker #include "chre/platform/slpi/memory.h" 19*84e33947SAndroid Build Coastguard Worker 20*84e33947SAndroid Build Coastguard Worker #ifdef CHRE_SLPI_SEE 21*84e33947SAndroid Build Coastguard Worker #include "chre/platform/slpi/see/island_vote_client.h" 22*84e33947SAndroid Build Coastguard Worker #endif 23*84e33947SAndroid Build Coastguard Worker 24*84e33947SAndroid Build Coastguard Worker #include <cstdlib> 25*84e33947SAndroid Build Coastguard Worker 26*84e33947SAndroid Build Coastguard Worker extern "C" { 27*84e33947SAndroid Build Coastguard Worker 28*84e33947SAndroid Build Coastguard Worker #ifdef CHRE_SLPI_UIMG_ENABLED 29*84e33947SAndroid Build Coastguard Worker #include "sns_island_util.h" 30*84e33947SAndroid Build Coastguard Worker #include "sns_memmgr.h" 31*84e33947SAndroid Build Coastguard Worker #endif // CHRE_SLPI_UIMG_ENABLED 32*84e33947SAndroid Build Coastguard Worker 33*84e33947SAndroid Build Coastguard Worker } // extern "C" 34*84e33947SAndroid Build Coastguard Worker 35*84e33947SAndroid Build Coastguard Worker namespace chre { 36*84e33947SAndroid Build Coastguard Worker memoryAlloc(size_t size)37*84e33947SAndroid Build Coastguard Workervoid *memoryAlloc(size_t size) { 38*84e33947SAndroid Build Coastguard Worker #ifdef CHRE_SLPI_UIMG_ENABLED 39*84e33947SAndroid Build Coastguard Worker void *ptr = sns_malloc(SNS_HEAP_CHRE_ISLAND, size); 40*84e33947SAndroid Build Coastguard Worker 41*84e33947SAndroid Build Coastguard Worker // Fall back to big image memory when uimg memory is exhausted. 42*84e33947SAndroid Build Coastguard Worker // Must exclude size 0 as clients may not explicitly free memory of size 0, 43*84e33947SAndroid Build Coastguard Worker // which may mistakenly hold the system in big image. 44*84e33947SAndroid Build Coastguard Worker if (ptr == nullptr && size != 0) { 45*84e33947SAndroid Build Coastguard Worker // Increment big image ref count to prevent system from entering uimg 46*84e33947SAndroid Build Coastguard Worker // while big image memory is in use. 47*84e33947SAndroid Build Coastguard Worker IslandVoteClientSingleton::get()->incrementBigImageRefCount(); 48*84e33947SAndroid Build Coastguard Worker ptr = memoryAllocBigImage(size); 49*84e33947SAndroid Build Coastguard Worker 50*84e33947SAndroid Build Coastguard Worker // Big image allocation failed too. 51*84e33947SAndroid Build Coastguard Worker if (ptr == nullptr) { 52*84e33947SAndroid Build Coastguard Worker IslandVoteClientSingleton::get()->decrementBigImageRefCount(); 53*84e33947SAndroid Build Coastguard Worker } 54*84e33947SAndroid Build Coastguard Worker } 55*84e33947SAndroid Build Coastguard Worker 56*84e33947SAndroid Build Coastguard Worker return ptr; 57*84e33947SAndroid Build Coastguard Worker #else 58*84e33947SAndroid Build Coastguard Worker return malloc(size); 59*84e33947SAndroid Build Coastguard Worker #endif // CHRE_SLPI_UIMG_ENABLED 60*84e33947SAndroid Build Coastguard Worker } 61*84e33947SAndroid Build Coastguard Worker memoryAllocBigImage(size_t size)62*84e33947SAndroid Build Coastguard Workervoid *memoryAllocBigImage(size_t size) { 63*84e33947SAndroid Build Coastguard Worker return malloc(size); 64*84e33947SAndroid Build Coastguard Worker } 65*84e33947SAndroid Build Coastguard Worker palSystemApiMemoryAlloc(size_t size)66*84e33947SAndroid Build Coastguard Workervoid *palSystemApiMemoryAlloc(size_t size) { 67*84e33947SAndroid Build Coastguard Worker return malloc(size); 68*84e33947SAndroid Build Coastguard Worker } 69*84e33947SAndroid Build Coastguard Worker memoryFree(void * pointer)70*84e33947SAndroid Build Coastguard Workervoid memoryFree(void *pointer) { 71*84e33947SAndroid Build Coastguard Worker #ifdef CHRE_SLPI_UIMG_ENABLED 72*84e33947SAndroid Build Coastguard Worker if (sns_island_is_island_ptr(reinterpret_cast<intptr_t>(pointer))) { 73*84e33947SAndroid Build Coastguard Worker sns_free(pointer); 74*84e33947SAndroid Build Coastguard Worker } else { 75*84e33947SAndroid Build Coastguard Worker memoryFreeBigImage(pointer); 76*84e33947SAndroid Build Coastguard Worker 77*84e33947SAndroid Build Coastguard Worker // Must exclude nullptr as it's excluded in memoryAlloc() as well. 78*84e33947SAndroid Build Coastguard Worker // Note currently sns_island_is_island_ptr returns true for nullptr, 79*84e33947SAndroid Build Coastguard Worker // so this mainly serves as a protection in case the implementation of 80*84e33947SAndroid Build Coastguard Worker // sns_island_is_island_ptr changes in the future. 81*84e33947SAndroid Build Coastguard Worker if (pointer != nullptr) { 82*84e33947SAndroid Build Coastguard Worker IslandVoteClientSingleton::get()->decrementBigImageRefCount(); 83*84e33947SAndroid Build Coastguard Worker } 84*84e33947SAndroid Build Coastguard Worker } 85*84e33947SAndroid Build Coastguard Worker #else 86*84e33947SAndroid Build Coastguard Worker free(pointer); 87*84e33947SAndroid Build Coastguard Worker #endif // CHRE_SLPI_UIMG_ENABLED 88*84e33947SAndroid Build Coastguard Worker } 89*84e33947SAndroid Build Coastguard Worker memoryFreeBigImage(void * pointer)90*84e33947SAndroid Build Coastguard Workervoid memoryFreeBigImage(void *pointer) { 91*84e33947SAndroid Build Coastguard Worker free(pointer); 92*84e33947SAndroid Build Coastguard Worker } 93*84e33947SAndroid Build Coastguard Worker palSystemApiMemoryFree(void * pointer)94*84e33947SAndroid Build Coastguard Workervoid palSystemApiMemoryFree(void *pointer) { 95*84e33947SAndroid Build Coastguard Worker free(pointer); 96*84e33947SAndroid Build Coastguard Worker } 97*84e33947SAndroid Build Coastguard Worker 98*84e33947SAndroid Build Coastguard Worker } // namespace chre 99