blob: 36393e738d12fc20e1db292b1afc73f621b1253b [file] [log] [blame]
Dmitriy Ivanov6b566912014-04-29 08:41:29 -07001/*
2 * Copyright (C) 2014 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#include <stdio.h>
17#include <stdlib.h>
18
19#include <string>
20
21// use external control number from main test
22static std::string* atexit_sequence = NULL;
23static bool* atexit_valid_this_in_static_dtor = NULL;
24
25class AtExitStaticClass;
26
27static const AtExitStaticClass* valid_this = NULL;
28
29static class AtExitStaticClass {
30public:
31 AtExitStaticClass() { valid_this = this; }
32 ~AtExitStaticClass() {
33 if (atexit_valid_this_in_static_dtor) {
34 *atexit_valid_this_in_static_dtor = (valid_this == this);
35 }
36 }
37} staticObj;
38
39// 4
40static void atexit_handler_from_atexit_from_atexit2() {
41 *atexit_sequence += " on";
42}
43
44// 3
45static void atexit_handler_from_atexit_from_atexit1() {
46 *atexit_sequence += " sat";
47}
48
49// 2
50static void atexit_handler_from_atexit() {
51 *atexit_sequence += " Dumpty";
52 // register 2 others
53 atexit(atexit_handler_from_atexit_from_atexit2);
54 atexit(atexit_handler_from_atexit_from_atexit1);
55}
56
57// 1
58static void atexit_handler_with_atexit() {
59 *atexit_sequence += "Humpty";
60 atexit(atexit_handler_from_atexit);
61}
62
63// last
64static void atexit_handler_regular() {
65 *atexit_sequence += " a wall";
66}
67
68extern "C" void register_atexit(std::string* sequence, bool* valid_this_in_static_dtor) {
69 atexit_sequence = sequence;
70 atexit_valid_this_in_static_dtor = valid_this_in_static_dtor;
71 atexit(atexit_handler_regular);
72 atexit(atexit_handler_with_atexit);
73 atexit(NULL);
74}
75