• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 <cstdint>
18 #define LOG_TAG "sharedtest"
19 #include <utils/Log.h>
20 
21 // Test library which is dynamicly loaded by library_tests.
22 
23 // Static variable construction.
24 // Calls A constructor on library load, A destructor on library unload.
25 
26 int32_t *gPtr = nullptr;  // this pointer is filled with the location to set memory
27                           // when ~A() is called.
28                           // we cannot use anything internal to this file as the
29                           // data segment may no longer exist after unloading the library.
30 struct A {
AA31     A() {
32         ALOGD("%s: gPtr:%p", __func__, gPtr);
33     }
34 
~AA35     ~A() {
36         ALOGD("%s: gPtr:%p", __func__, gPtr);
37         if (gPtr != nullptr) {
38             *gPtr = 1;
39         }
40     }
41 } gA;
42 
43 //  __attribute__((constructor)) methods occur before any static variable construction.
44 // Libraries that use __attribute__((constructor)) should not rely on global constructors
45 // before method call because they will not be initialized before use.
46 // See heapprofd_client_api.
47 // NOTE: is this right? Shouldn't it occur after construction?
48  __attribute__((constructor))
onConstruction()49 void onConstruction() {
50     ALOGD("%s: in progress", __func__);  // for logcat analysis
51 }
52 
53 // __attribute__((destructor)) methods occur before any static variable destruction.
54  __attribute__((destructor))
onDestruction()55 void onDestruction() {
56     ALOGD("%s: in progress", __func__);  // for logcat analysis
57 }
58