summaryrefslogtreecommitdiffstats
path: root/tests/libs/atexit_testlib.cpp
blob: d35f57b7432d19a271b369d8d70d70a3c62b21b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdio.h>
#include <stdlib.h>

#include <string>

// use external control number from main test
static std::string* atexit_sequence = NULL;
static bool* atexit_valid_this_in_static_dtor = NULL;

static class AtExitStaticClass {
 public:
  AtExitStaticClass() { expected_this = this; }
  ~AtExitStaticClass() {
    if (atexit_valid_this_in_static_dtor) {
      *atexit_valid_this_in_static_dtor = (expected_this == this);
    }
  }
 private:
  static const AtExitStaticClass* expected_this;

} static_obj;

const AtExitStaticClass* AtExitStaticClass::expected_this = NULL;

// 4
static void atexit_handler_from_atexit_from_atexit2() {
  *atexit_sequence += " on";
}

// 3
static void atexit_handler_from_atexit_from_atexit1() {
  *atexit_sequence += " sat";
}

// 2
static void atexit_handler_from_atexit() {
  *atexit_sequence += " Dumpty";
  // register 2 others
  atexit(atexit_handler_from_atexit_from_atexit2);
  atexit(atexit_handler_from_atexit_from_atexit1);
}

// 1
static void atexit_handler_with_atexit() {
  *atexit_sequence += "Humpty";
  atexit(atexit_handler_from_atexit);
}

// last
static void atexit_handler_regular() {
  *atexit_sequence += " a wall";
}

extern "C" void register_atexit(std::string* sequence, bool* valid_this_in_static_dtor) {
  atexit_sequence = sequence;
  atexit_valid_this_in_static_dtor = valid_this_in_static_dtor;
  atexit(atexit_handler_regular);
  atexit(atexit_handler_with_atexit);
}