1 /* 2 ****************************************************************************** 3 * * 4 * Copyright (C) 2001-2005, International Business Machines * 5 * Corporation and others. All Rights Reserved. * 6 * * 7 ****************************************************************************** 8 * file name: uclean.h 9 * encoding: US-ASCII 10 * tab size: 8 (not used) 11 * indentation:4 12 * 13 * created on: 2001July05 14 * created by: George Rhoten 15 */ 16 17 #ifndef __UCLEAN_H__ 18 #define __UCLEAN_H__ 19 20 #include "unicode/utypes.h" 21 /** 22 * \file 23 * \brief C API: Initialize and clean up ICU 24 */ 25 26 /** 27 * Initialize ICU. The description further below applies to ICU 2.6 to ICU 3.4. 28 * Starting with ICU 3.4, u_init() needs not be called any more for 29 * ensuring thread safety, but it can give an indication for whether ICU 30 * can load its data. In ICU 3.4, it will try to load the converter alias table 31 * (cnvalias.icu) and give an error code if that fails. 32 * This may change in the future. 33 * <p> 34 * For ensuring the availability of necessary data, an application should 35 * open the service objects (converters, collators, etc.) that it will use 36 * and check for error codes there. 37 * <p> 38 * Documentation for ICU 2.6 to ICU 3.4: 39 * <p> 40 * This function loads and initializes data items 41 * that are required internally by various ICU functions. Use of this explicit 42 * initialization is required in multi-threaded applications; in 43 * single threaded apps, use is optional, but incurs little additional 44 * cost, and is thus recommended. 45 * <p> 46 * In multi-threaded applications, u_init() should be called in the 47 * main thread before starting additional threads, or, alternatively 48 * it can be called in each individual thread once, before other ICU 49 * functions are called in that thread. In this second scenario, the 50 * application must guarantee that the first call to u_init() happen 51 * without contention, in a single thread only. 52 * <p> 53 * If <code>u_setMemoryFunctions()</code> or 54 * <code>u_setMutexFunctions</code> are needed (uncommon), they must be 55 * called _before_ <code>u_init()</code>. 56 * <p> 57 * Extra, repeated, or otherwise unneeded calls to u_init() do no harm, 58 * other than taking a small amount of time. 59 * 60 * @param status An ICU UErrorCode parameter. It must not be <code>NULL</code>. 61 * An Error will be returned if some required part of ICU data can not 62 * be loaded or initialized. 63 * The function returns immediately if the input error code indicates a 64 * failure, as usual. 65 * 66 * @stable ICU 2.6 67 */ 68 U_STABLE void U_EXPORT2 69 u_init(UErrorCode *status); 70 71 /** 72 * Clean up the system resources, such as allocated memory or open files, 73 * used in all ICU libraries. This will free/delete all memory owned by the 74 * ICU libraries, and return them to their original load state. All open ICU 75 * items (collators, resource bundles, converters, etc.) must be closed before 76 * calling this function, otherwise ICU may not free its allocated memory 77 * (e.g. close your converters and resource bundles before calling this 78 * function). Generally, this function should be called once just before 79 * an application exits. For applications that dynamically load and unload 80 * the ICU libraries (relatively uncommon), u_cleanup() should be called 81 * just before the library unload. 82 * <p> 83 * u_cleanup() also clears any ICU heap functions, mutex functions or 84 * trace functions that may have been set for the process. 85 * This has the effect of restoring ICU to its initial condition, before 86 * any of these override functions were installed. Refer to 87 * u_setMemoryFunctions(), u_setMutexFunctions and 88 * utrace_setFunctions(). If ICU is to be reinitialized after after 89 * calling u_cleanup(), these runtime override functions will need to 90 * be set up again if they are still required. 91 * <p> 92 * u_cleanup() is not thread safe. All other threads should stop using ICU 93 * before calling this function. 94 * <p> 95 * Any open ICU items will be left in an undefined state by u_cleanup(), 96 * and any subsequent attempt to use such an item will give unpredictable 97 * results. 98 * <p> 99 * After calling u_cleanup(), an application may continue to use ICU by 100 * calling u_init(). An application must invoke u_init() first from one single 101 * thread before allowing other threads call u_init(). All threads existing 102 * at the time of the first thread's call to u_init() must also call 103 * u_init() themselves before continuing with other ICU operations. 104 * <p> 105 * The use of u_cleanup() just before an application terminates is optional, 106 * but it should be called only once for performance reasons. The primary 107 * benefit is to eliminate reports of memory or resource leaks originating 108 * in ICU code from the results generated by heap analysis tools. 109 * <p> 110 * <strong>Use this function with great care!</strong> 111 * </p> 112 * 113 * @stable ICU 2.0 114 * @system 115 */ 116 U_STABLE void U_EXPORT2 117 u_cleanup(void); 118 119 120 121 122 /** 123 * An opaque pointer type that represents an ICU mutex. 124 * For user-implemented mutexes, the value will typically point to a 125 * struct or object that implements the mutex. 126 * @stable ICU 2.8 127 * @system 128 */ 129 typedef void *UMTX; 130 131 /** 132 * Function Pointer type for a user supplied mutex initialization function. 133 * The user-supplied function will be called by ICU whenever ICU needs to create a 134 * new mutex. The function implementation should create a mutex, and store a pointer 135 * to something that uniquely identifies the mutex into the UMTX that is supplied 136 * as a paramter. 137 * @param context user supplied value, obtained from from u_setMutexFunctions(). 138 * @param mutex Receives a pointer that identifies the new mutex. 139 * The mutex init function must set the UMTX to a non-null value. 140 * Subsequent calls by ICU to lock, unlock, or destroy a mutex will 141 * identify the mutex by the UMTX value. 142 * @param status Error status. Report errors back to ICU by setting this variable 143 * with an error code. 144 * @stable ICU 2.8 145 * @system 146 */ 147 typedef void U_CALLCONV UMtxInitFn (const void *context, UMTX *mutex, UErrorCode* status); 148 149 150 /** 151 * Function Pointer type for a user supplied mutex functions. 152 * One of the user-supplied functions with this signature will be called by ICU 153 * whenever ICU needs to lock, unlock, or destroy a mutex. 154 * @param context user supplied value, obtained from from u_setMutexFunctions(). 155 * @param mutex specify the mutex on which to operate. 156 * @stable ICU 2.8 157 * @system 158 */ 159 typedef void U_CALLCONV UMtxFn (const void *context, UMTX *mutex); 160 161 162 /** 163 * Set the functions that ICU will use for mutex operations 164 * Use of this function is optional; by default (without this function), ICU will 165 * directly access system functions for mutex operations 166 * This function can only be used when ICU is in an initial, unused state, before 167 * u_init() has been called. 168 * This function may be used even when ICU has been built without multi-threaded 169 * support (see ICU_USE_THREADS pre-processor variable, umutex.h) 170 * @param context This pointer value will be saved, and then (later) passed as 171 * a parameter to the user-supplied mutex functions each time they 172 * are called. 173 * @param init Pointer to a mutex initialization function. Must be non-null. 174 * @param destroy Pointer to the mutex destroy function. Must be non-null. 175 * @param lock pointer to the mutex lock function. Must be non-null. 176 * @param unlock Pointer to the mutex unlock function. Must be non-null. 177 * @param status Receives error values. 178 * @stable ICU 2.8 179 * @system 180 */ 181 U_STABLE void U_EXPORT2 182 u_setMutexFunctions(const void *context, UMtxInitFn *init, UMtxFn *destroy, UMtxFn *lock, UMtxFn *unlock, 183 UErrorCode *status); 184 185 186 /** 187 * Pointer type for a user supplied atomic increment or decrement function. 188 * @param context user supplied value, obtained from from u_setAtomicIncDecFunctions(). 189 * @param p Pointer to a 32 bit int to be incremented or decremented 190 * @return The value of the variable after the inc or dec operation. 191 * @stable ICU 2.8 192 * @system 193 */ 194 typedef int32_t U_CALLCONV UMtxAtomicFn(const void *context, int32_t *p); 195 196 /** 197 * Set the functions that ICU will use for atomic increment and decrement of int32_t values. 198 * Use of this function is optional; by default (without this function), ICU will 199 * use its own internal implementation of atomic increment/decrement. 200 * This function can only be used when ICU is in an initial, unused state, before 201 * u_init() has been called. 202 * @param context This pointer value will be saved, and then (later) passed as 203 * a parameter to the increment and decrement functions each time they 204 * are called. This function can only be called 205 * @param inc Pointer to a function to do an atomic increment operation. Must be non-null. 206 * @param dec Pointer to a function to do an atomic decrement operation. Must be non-null. 207 * @param status Receives error values. 208 * @stable ICU 2.8 209 * @system 210 */ 211 U_STABLE void U_EXPORT2 212 u_setAtomicIncDecFunctions(const void *context, UMtxAtomicFn *inc, UMtxAtomicFn *dec, 213 UErrorCode *status); 214 215 216 217 /** 218 * Pointer type for a user supplied memory allocation function. 219 * @param context user supplied value, obtained from from u_setMemoryFunctions(). 220 * @param size The number of bytes to be allocated 221 * @return Pointer to the newly allocated memory, or NULL if the allocation failed. 222 * @stable ICU 2.8 223 * @system 224 */ 225 typedef void *U_CALLCONV UMemAllocFn(const void *context, size_t size); 226 /** 227 * Pointer type for a user supplied memory re-allocation function. 228 * @param context user supplied value, obtained from from u_setMemoryFunctions(). 229 * @param size The number of bytes to be allocated 230 * @return Pointer to the newly allocated memory, or NULL if the allocation failed. 231 * @stable ICU 2.8 232 * @system 233 */ 234 typedef void *U_CALLCONV UMemReallocFn(const void *context, void *mem, size_t size); 235 /** 236 * Pointer type for a user supplied memory free function. Behavior should be 237 * similar the standard C library free(). 238 * @param context user supplied value, obtained from from u_setMemoryFunctions(). 239 * @param mem Pointer to the memory block to be resized 240 * @param size The new size for the block 241 * @return Pointer to the resized memory block, or NULL if the resizing failed. 242 * @stable ICU 2.8 243 * @system 244 */ 245 typedef void U_CALLCONV UMemFreeFn (const void *context, void *mem); 246 247 /** 248 * Set the functions that ICU will use for memory allocation. 249 * Use of this function is optional; by default (without this function), ICU will 250 * use the standard C library malloc() and free() functions. 251 * This function can only be used when ICU is in an initial, unused state, before 252 * u_init() has been called. 253 * @param context This pointer value will be saved, and then (later) passed as 254 * a parameter to the memory functions each time they 255 * are called. 256 * @param a Pointer to a user-supplied malloc function. 257 * @param r Pointer to a user-supplied realloc function. 258 * @param f Pointer to a user-supplied free function. 259 * @param status Receives error values. 260 * @stable ICU 2.8 261 * @system 262 */ 263 U_STABLE void U_EXPORT2 264 u_setMemoryFunctions(const void *context, UMemAllocFn *a, UMemReallocFn *r, UMemFreeFn *f, 265 UErrorCode *status); 266 267 #endif 268