1// Copyright (C) 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: US-ASCII 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 */ 81static UBool gAutoCleanRegistered = FALSE; 82 83static void ucln_atexit_handler() 84{ 85 UCLN_CLEAN_ME_UP; 86} 87 88static void ucln_registerAutomaticCleanup() 89{ 90 if(!gAutoCleanRegistered) { 91 gAutoCleanRegistered = TRUE; 92 atexit(&ucln_atexit_handler); 93 } 94} 95 96static 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 */ 106U_CAPI void U_EXPORT2 UCLN_FINI (void); 107 108U_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# define WIN32_LEAN_AND_MEAN 125# define VC_EXTRALEAN 126# define NOUSER 127# define NOSERVICE 128# define NOIME 129# define NOMCX 130# include <windows.h> 131/* 132 * This is a stub DllMain function with icu specific process handling code. 133 */ 134BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) 135{ 136 BOOL status = TRUE; 137 138 switch(fdwReason) { 139 case DLL_PROCESS_ATTACH: 140 /* ICU does not trap process attach, but must pass these through properly. */ 141 /* ICU specific process attach could go here */ 142 break; 143 144 case DLL_PROCESS_DETACH: 145 /* Here is the one we actually care about. */ 146 147 UCLN_CLEAN_ME_UP; 148 149 break; 150 151 case DLL_THREAD_ATTACH: 152 /* ICU does not trap thread attach, but must pass these through properly. */ 153 /* ICU specific thread attach could go here */ 154 break; 155 156 case DLL_THREAD_DETACH: 157 /* ICU does not trap thread detach, but must pass these through properly. */ 158 /* ICU specific thread detach could go here */ 159 break; 160 161 } 162 return status; 163} 164 165#elif defined(__GNUC__) 166/* GCC - use __attribute((destructor)) */ 167static void ucln_destructor() __attribute__((destructor)) ; 168 169static void ucln_destructor() 170{ 171 UCLN_CLEAN_ME_UP; 172} 173 174#endif 175 176#endif /* UCLN_NO_AUTO_CLEANUP */ 177 178#else 179#error This file can only be included once. 180#endif 181