xref: /aosp_15_r20/frameworks/av/media/utils/tests/sharedtest.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1*ec779b8eSAndroid Build Coastguard Worker /*
2*ec779b8eSAndroid Build Coastguard Worker  * Copyright (C) 2022 The Android Open Source Project
3*ec779b8eSAndroid Build Coastguard Worker  *
4*ec779b8eSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*ec779b8eSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*ec779b8eSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*ec779b8eSAndroid Build Coastguard Worker  *
8*ec779b8eSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*ec779b8eSAndroid Build Coastguard Worker  *
10*ec779b8eSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*ec779b8eSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*ec779b8eSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*ec779b8eSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*ec779b8eSAndroid Build Coastguard Worker  * limitations under the License.
15*ec779b8eSAndroid Build Coastguard Worker  */
16*ec779b8eSAndroid Build Coastguard Worker 
17*ec779b8eSAndroid Build Coastguard Worker #include <cstdint>
18*ec779b8eSAndroid Build Coastguard Worker #define LOG_TAG "sharedtest"
19*ec779b8eSAndroid Build Coastguard Worker #include <utils/Log.h>
20*ec779b8eSAndroid Build Coastguard Worker 
21*ec779b8eSAndroid Build Coastguard Worker // Test library which is dynamicly loaded by library_tests.
22*ec779b8eSAndroid Build Coastguard Worker 
23*ec779b8eSAndroid Build Coastguard Worker // Static variable construction.
24*ec779b8eSAndroid Build Coastguard Worker // Calls A constructor on library load, A destructor on library unload.
25*ec779b8eSAndroid Build Coastguard Worker 
26*ec779b8eSAndroid Build Coastguard Worker int32_t *gPtr = nullptr;  // this pointer is filled with the location to set memory
27*ec779b8eSAndroid Build Coastguard Worker                           // when ~A() is called.
28*ec779b8eSAndroid Build Coastguard Worker                           // we cannot use anything internal to this file as the
29*ec779b8eSAndroid Build Coastguard Worker                           // data segment may no longer exist after unloading the library.
30*ec779b8eSAndroid Build Coastguard Worker struct A {
AA31*ec779b8eSAndroid Build Coastguard Worker     A() {
32*ec779b8eSAndroid Build Coastguard Worker         ALOGD("%s: gPtr:%p", __func__, gPtr);
33*ec779b8eSAndroid Build Coastguard Worker     }
34*ec779b8eSAndroid Build Coastguard Worker 
~AA35*ec779b8eSAndroid Build Coastguard Worker     ~A() {
36*ec779b8eSAndroid Build Coastguard Worker         ALOGD("%s: gPtr:%p", __func__, gPtr);
37*ec779b8eSAndroid Build Coastguard Worker         if (gPtr != nullptr) {
38*ec779b8eSAndroid Build Coastguard Worker             *gPtr = 1;
39*ec779b8eSAndroid Build Coastguard Worker         }
40*ec779b8eSAndroid Build Coastguard Worker     }
41*ec779b8eSAndroid Build Coastguard Worker } gA;
42*ec779b8eSAndroid Build Coastguard Worker 
43*ec779b8eSAndroid Build Coastguard Worker //  __attribute__((constructor)) methods occur before any static variable construction.
44*ec779b8eSAndroid Build Coastguard Worker // Libraries that use __attribute__((constructor)) should not rely on global constructors
45*ec779b8eSAndroid Build Coastguard Worker // before method call because they will not be initialized before use.
46*ec779b8eSAndroid Build Coastguard Worker // See heapprofd_client_api.
47*ec779b8eSAndroid Build Coastguard Worker // NOTE: is this right? Shouldn't it occur after construction?
48*ec779b8eSAndroid Build Coastguard Worker  __attribute__((constructor))
onConstruction()49*ec779b8eSAndroid Build Coastguard Worker void onConstruction() {
50*ec779b8eSAndroid Build Coastguard Worker     ALOGD("%s: in progress", __func__);  // for logcat analysis
51*ec779b8eSAndroid Build Coastguard Worker }
52*ec779b8eSAndroid Build Coastguard Worker 
53*ec779b8eSAndroid Build Coastguard Worker // __attribute__((destructor)) methods occur before any static variable destruction.
54*ec779b8eSAndroid Build Coastguard Worker  __attribute__((destructor))
onDestruction()55*ec779b8eSAndroid Build Coastguard Worker void onDestruction() {
56*ec779b8eSAndroid Build Coastguard Worker     ALOGD("%s: in progress", __func__);  // for logcat analysis
57*ec779b8eSAndroid Build Coastguard Worker }
58