Dmitriy Ivanov | 6b56691 | 2014-04-29 08:41:29 -0700 | [diff] [blame^] | 1 | /* |
| 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 |
| 22 | static std::string* atexit_sequence = NULL; |
| 23 | static bool* atexit_valid_this_in_static_dtor = NULL; |
| 24 | |
| 25 | class AtExitStaticClass; |
| 26 | |
| 27 | static const AtExitStaticClass* valid_this = NULL; |
| 28 | |
| 29 | static class AtExitStaticClass { |
| 30 | public: |
| 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 |
| 40 | static void atexit_handler_from_atexit_from_atexit2() { |
| 41 | *atexit_sequence += " on"; |
| 42 | } |
| 43 | |
| 44 | // 3 |
| 45 | static void atexit_handler_from_atexit_from_atexit1() { |
| 46 | *atexit_sequence += " sat"; |
| 47 | } |
| 48 | |
| 49 | // 2 |
| 50 | static 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 |
| 58 | static void atexit_handler_with_atexit() { |
| 59 | *atexit_sequence += "Humpty"; |
| 60 | atexit(atexit_handler_from_atexit); |
| 61 | } |
| 62 | |
| 63 | // last |
| 64 | static void atexit_handler_regular() { |
| 65 | *atexit_sequence += " a wall"; |
| 66 | } |
| 67 | |
| 68 | extern "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 | |