1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ******************************************************************************
5 *
6 * Copyright (C) 2009-2011, International Business Machines
7 *                Corporation and others. All Rights Reserved.
8 *
9 ******************************************************************************
10 *   file name:  ucln_imp.h
11 *   encoding:   UTF-8
12 *   tab size:   8 (not used)
13 *   indentation:4
14 *
15 *   This file contains the platform specific implementation of per-library cleanup.
16 *
17 */
18 
19 
20 #ifndef __UCLN_IMP_H__
21 #define __UCLN_IMP_H__
22 
23 #include "ucln.h"
24 #include <stdlib.h>
25 
26 /**
27  * Auto cleanup of ICU libraries
28  * There are several methods in per library cleanup of icu libraries:
29  * 1) Compiler/Platform based cleanup:
30  *   a) Windows MSVC uses DllMain()
31  *   b) GCC uses destructor function attribute
32  *   c) Sun Studio, AIX VA, and HP-UX aCC uses a linker option to set the exit function
33  * 2) Using atexit()
34  * 3) Implementing own automatic cleanup functions
35  *
36  * For option 1, ensure that UCLN_NO_AUTO_CLEANUP is set to 0 by using --enable-auto-cleanup
37  * configure option or by otherwise setting UCLN_NO_AUTO_CLEANUP to 0
38  * For option 2, follow option 1 and also define UCLN_AUTO_ATEXIT
39  * For option 3, follow option 1 and also define UCLN_AUTO_LOCAL (see below for more information)
40  */
41 
42 #if !UCLN_NO_AUTO_CLEANUP
43 
44 /*
45  * The following declarations are for when UCLN_AUTO_LOCAL or UCLN_AUTO_ATEXIT
46  * are defined. They are commented out because they are static and will be defined
47  * later. The information is still here to provide some guidance for the developer
48  * who chooses to use UCLN_AUTO_LOCAL.
49  */
50 /**
51  * Give the library an opportunity to register an automatic cleanup.
52  * This may be called more than once.
53  */
54 /*static void ucln_registerAutomaticCleanup();*/
55 /**
56  * Unregister an automatic cleanup, if possible. Called from cleanup.
57  */
58 /*static void ucln_unRegisterAutomaticCleanup();*/
59 
60 #ifdef UCLN_TYPE_IS_COMMON
61 #   define UCLN_CLEAN_ME_UP u_cleanup()
62 #else
63 #   define UCLN_CLEAN_ME_UP ucln_cleanupOne(UCLN_TYPE)
64 #endif
65 
66 /* ------------ automatic cleanup: registration. Choose ONE ------- */
67 #if defined(UCLN_AUTO_LOCAL)
68 /* To use:
69  *  1. define UCLN_AUTO_LOCAL,
70  *  2. create ucln_local_hook.c containing implementations of
71  *           static void ucln_registerAutomaticCleanup()
72  *           static void ucln_unRegisterAutomaticCleanup()
73  */
74 #include "ucln_local_hook.c"
75 
76 #elif defined(UCLN_AUTO_ATEXIT)
77 /*
78  * Use the ANSI C 'atexit' function. Note that this mechanism does not
79  * guarantee the order of cleanup relative to other users of ICU!
80  */
81 static UBool gAutoCleanRegistered = FALSE;
82 
ucln_atexit_handler()83 static void ucln_atexit_handler()
84 {
85     UCLN_CLEAN_ME_UP;
86 }
87 
ucln_registerAutomaticCleanup()88 static void ucln_registerAutomaticCleanup()
89 {
90     if(!gAutoCleanRegistered) {
91         gAutoCleanRegistered = TRUE;
92         atexit(&ucln_atexit_handler);
93     }
94 }
95 
ucln_unRegisterAutomaticCleanup()96 static void ucln_unRegisterAutomaticCleanup () {
97 }
98 /* ------------end of automatic cleanup: registration. ------- */
99 
100 #elif defined (UCLN_FINI)
101 /**
102  * If UCLN_FINI is defined, it is the (versioned, etc) name of a cleanup
103  * entrypoint. Add a stub to call ucln_cleanupOne
104  * Used on AIX, Solaris, and HP-UX
105  */
106 U_CAPI void U_EXPORT2 UCLN_FINI (void);
107 
UCLN_FINI()108 U_CAPI void U_EXPORT2 UCLN_FINI ()
109 {
110     /* This function must be defined, if UCLN_FINI is defined, else link error. */
111      UCLN_CLEAN_ME_UP;
112 }
113 
114 /* Windows: DllMain */
115 #elif U_PLATFORM_HAS_WIN32_API
116 /*
117  * ICU's own DllMain.
118  */
119 
120 /* these are from putil.c */
121 /* READ READ READ READ!    Are you getting compilation errors from windows.h?
122           Any source file which includes this (ucln_imp.h) header MUST
123           be defined with language extensions ON. */
124 #ifndef WIN32_LEAN_AND_MEAN
125 #   define WIN32_LEAN_AND_MEAN
126 #endif
127 #   define VC_EXTRALEAN
128 #   define NOUSER
129 #   define NOSERVICE
130 #   define NOIME
131 #   define NOMCX
132 #   include <windows.h>
133 /*
134  * This is a stub DllMain function with icu specific process handling code.
135  */
DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)136 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
137 {
138     BOOL status = TRUE;
139 
140     switch(fdwReason) {
141         case DLL_PROCESS_ATTACH:
142              /* ICU does not trap process attach, but must pass these through properly. */
143             /* ICU specific process attach could go here */
144             break;
145 
146         case DLL_PROCESS_DETACH:
147             /* Here is the one we actually care about. */
148 
149             UCLN_CLEAN_ME_UP;
150 
151             break;
152 
153         case DLL_THREAD_ATTACH:
154             /* ICU does not trap thread attach, but must pass these through properly. */
155             /* ICU specific thread attach could go here */
156             break;
157 
158         case DLL_THREAD_DETACH:
159             /* ICU does not trap thread detach, but must pass these through properly. */
160             /* ICU specific thread detach could go here */
161             break;
162 
163     }
164     return status;
165 }
166 
167 #elif defined(__GNUC__)
168 /* GCC - use __attribute((destructor)) */
169 static void ucln_destructor()   __attribute__((destructor)) ;
170 
ucln_destructor()171 static void ucln_destructor()
172 {
173     UCLN_CLEAN_ME_UP;
174 }
175 
176 #endif
177 
178 #endif /* UCLN_NO_AUTO_CLEANUP */
179 
180 #else
181 #error This file can only be included once.
182 #endif
183