ucln_imp.h 5.1 KB

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