1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "b.h"
16 #include <cstdio>
17
18 namespace libb {
19
20 static int next_object_id = 0;
21
ClassB(unsigned value)22 ClassB::ClassB(unsigned value):
23 object_id(next_object_id++),
24 value(value) {
25 Log("ctor");
26 }
27
~ClassB()28 ClassB::~ClassB() {
29 Log("dtor");
30 }
31
Instance()32 ClassB& ClassB::Instance() {
33 static ClassB instance(0);
34 return instance;
35 }
36
Log(const char * message) const37 void ClassB::Log(const char* message) const {
38 printf("[ClassB::Log] \t[%s] \toid:%d \tvalue:%d\n", message, object_id, value);
39
40 }
41
Value() const42 unsigned ClassB::Value() const {
43 return value;
44 }
45
SetFlag(unsigned bit)46 void ClassB::SetFlag(unsigned bit) {
47 Log("pre-SetFlag");
48 value |= 1<<bit;
49 Log("post-SetFlag");
50 }
51
HasFlag(unsigned bit) const52 bool ClassB::HasFlag(unsigned bit) const {
53 return !!(value & (1<<bit));
54 }
55
init_b_0()56 void __attribute__((constructor)) init_b_0() {
57 printf("called libb::init_b_0\n");
58 ClassB::Instance().SetFlag(INIT_0);
59 }
60
init_b_1()61 void __attribute__((constructor)) init_b_1() {
62 printf("called libb::init_b_1\n");
63 ClassB::Instance().SetFlag(INIT_1);
64 }
65
init_b_2()66 void __attribute__((constructor)) init_b_2() {
67 printf("called libb::init_b_2\n");
68 ClassB::Instance().SetFlag(INIT_2);
69 }
70
fini_b_0()71 void __attribute__((destructor)) fini_b_0() {
72 printf("called libb::fini_b_0\n");
73 ClassB::Instance().SetFlag(FINI_0);
74 }
75
fini_b_1()76 void __attribute__((destructor)) fini_b_1() {
77 printf("called libb::fini_b_1\n");
78 ClassB::Instance().SetFlag(FINI_1);
79 }
80
fini_b_2()81 void __attribute__((destructor)) fini_b_2() {
82 printf("called libb::fini_b_2\n");
83 ClassB::Instance().SetFlag(FINI_2);
84 }
85
fini_b_3()86 void __attribute__((destructor)) fini_b_3() {
87 printf("called libb::fini_b_3\n");
88 ClassB::Instance().SetFlag(FINI_3);
89 }
90
91 const ClassB
92 class_b_instance_0(42+0),
93 class_b_instance_1(42+1),
94 class_b_instance_2(42+2);
95
96 } // namespace libb