sl@0: /* sl@0: ****************************************************************************** sl@0: * * sl@0: * Copyright (C) 2001-2005, International Business Machines * sl@0: * Corporation and others. All Rights Reserved. * sl@0: * * sl@0: ****************************************************************************** sl@0: * file name: uclean.h sl@0: * encoding: US-ASCII sl@0: * tab size: 8 (not used) sl@0: * indentation:4 sl@0: * sl@0: * created on: 2001July05 sl@0: * created by: George Rhoten sl@0: */ sl@0: sl@0: #ifndef __UCLEAN_H__ sl@0: #define __UCLEAN_H__ sl@0: sl@0: #include "unicode/utypes.h" sl@0: /** sl@0: * \file sl@0: * \brief C API: Initialize and clean up ICU sl@0: */ sl@0: sl@0: /** sl@0: * Initialize ICU. The description further below applies to ICU 2.6 to ICU 3.4. sl@0: * Starting with ICU 3.4, u_init() needs not be called any more for sl@0: * ensuring thread safety, but it can give an indication for whether ICU sl@0: * can load its data. In ICU 3.4, it will try to load the converter alias table sl@0: * (cnvalias.icu) and give an error code if that fails. sl@0: * This may change in the future. sl@0: *
sl@0: * For ensuring the availability of necessary data, an application should sl@0: * open the service objects (converters, collators, etc.) that it will use sl@0: * and check for error codes there. sl@0: *
sl@0: * Documentation for ICU 2.6 to ICU 3.4: sl@0: *
sl@0: * This function loads and initializes data items sl@0: * that are required internally by various ICU functions. Use of this explicit sl@0: * initialization is required in multi-threaded applications; in sl@0: * single threaded apps, use is optional, but incurs little additional sl@0: * cost, and is thus recommended. sl@0: *
sl@0: * In multi-threaded applications, u_init() should be called in the sl@0: * main thread before starting additional threads, or, alternatively sl@0: * it can be called in each individual thread once, before other ICU sl@0: * functions are called in that thread. In this second scenario, the sl@0: * application must guarantee that the first call to u_init() happen sl@0: * without contention, in a single thread only. sl@0: *
sl@0: * If u_setMemoryFunctions()
or
sl@0: * u_setMutexFunctions
are needed (uncommon), they must be
sl@0: * called _before_ u_init()
.
sl@0: *
sl@0: * Extra, repeated, or otherwise unneeded calls to u_init() do no harm,
sl@0: * other than taking a small amount of time.
sl@0: *
sl@0: * @param status An ICU UErrorCode parameter. It must not be NULL
.
sl@0: * An Error will be returned if some required part of ICU data can not
sl@0: * be loaded or initialized.
sl@0: * The function returns immediately if the input error code indicates a
sl@0: * failure, as usual.
sl@0: *
sl@0: * @stable ICU 2.6
sl@0: */
sl@0: U_STABLE void U_EXPORT2
sl@0: u_init(UErrorCode *status);
sl@0:
sl@0: /**
sl@0: * Clean up the system resources, such as allocated memory or open files,
sl@0: * used in all ICU libraries. This will free/delete all memory owned by the
sl@0: * ICU libraries, and return them to their original load state. All open ICU
sl@0: * items (collators, resource bundles, converters, etc.) must be closed before
sl@0: * calling this function, otherwise ICU may not free its allocated memory
sl@0: * (e.g. close your converters and resource bundles before calling this
sl@0: * function). Generally, this function should be called once just before
sl@0: * an application exits. For applications that dynamically load and unload
sl@0: * the ICU libraries (relatively uncommon), u_cleanup() should be called
sl@0: * just before the library unload.
sl@0: *
sl@0: * u_cleanup() also clears any ICU heap functions, mutex functions or sl@0: * trace functions that may have been set for the process. sl@0: * This has the effect of restoring ICU to its initial condition, before sl@0: * any of these override functions were installed. Refer to sl@0: * u_setMemoryFunctions(), u_setMutexFunctions and sl@0: * utrace_setFunctions(). If ICU is to be reinitialized after after sl@0: * calling u_cleanup(), these runtime override functions will need to sl@0: * be set up again if they are still required. sl@0: *
sl@0: * u_cleanup() is not thread safe. All other threads should stop using ICU sl@0: * before calling this function. sl@0: *
sl@0: * Any open ICU items will be left in an undefined state by u_cleanup(), sl@0: * and any subsequent attempt to use such an item will give unpredictable sl@0: * results. sl@0: *
sl@0: * After calling u_cleanup(), an application may continue to use ICU by sl@0: * calling u_init(). An application must invoke u_init() first from one single sl@0: * thread before allowing other threads call u_init(). All threads existing sl@0: * at the time of the first thread's call to u_init() must also call sl@0: * u_init() themselves before continuing with other ICU operations. sl@0: *
sl@0: * The use of u_cleanup() just before an application terminates is optional, sl@0: * but it should be called only once for performance reasons. The primary sl@0: * benefit is to eliminate reports of memory or resource leaks originating sl@0: * in ICU code from the results generated by heap analysis tools. sl@0: *
sl@0: * Use this function with great care! sl@0: *
sl@0: * sl@0: * @stable ICU 2.0 sl@0: * @system sl@0: */ sl@0: U_STABLE void U_EXPORT2 sl@0: u_cleanup(void); sl@0: sl@0: sl@0: sl@0: sl@0: /** sl@0: * An opaque pointer type that represents an ICU mutex. sl@0: * For user-implemented mutexes, the value will typically point to a sl@0: * struct or object that implements the mutex. sl@0: * @stable ICU 2.8 sl@0: * @system sl@0: */ sl@0: typedef void *UMTX; sl@0: sl@0: /** sl@0: * Function Pointer type for a user supplied mutex initialization function. sl@0: * The user-supplied function will be called by ICU whenever ICU needs to create a sl@0: * new mutex. The function implementation should create a mutex, and store a pointer sl@0: * to something that uniquely identifies the mutex into the UMTX that is supplied sl@0: * as a paramter. sl@0: * @param context user supplied value, obtained from from u_setMutexFunctions(). sl@0: * @param mutex Receives a pointer that identifies the new mutex. sl@0: * The mutex init function must set the UMTX to a non-null value. sl@0: * Subsequent calls by ICU to lock, unlock, or destroy a mutex will sl@0: * identify the mutex by the UMTX value. sl@0: * @param status Error status. Report errors back to ICU by setting this variable sl@0: * with an error code. sl@0: * @stable ICU 2.8 sl@0: * @system sl@0: */ sl@0: typedef void U_CALLCONV UMtxInitFn (const void *context, UMTX *mutex, UErrorCode* status); sl@0: sl@0: sl@0: /** sl@0: * Function Pointer type for a user supplied mutex functions. sl@0: * One of the user-supplied functions with this signature will be called by ICU sl@0: * whenever ICU needs to lock, unlock, or destroy a mutex. sl@0: * @param context user supplied value, obtained from from u_setMutexFunctions(). sl@0: * @param mutex specify the mutex on which to operate. sl@0: * @stable ICU 2.8 sl@0: * @system sl@0: */ sl@0: typedef void U_CALLCONV UMtxFn (const void *context, UMTX *mutex); sl@0: sl@0: sl@0: /** sl@0: * Set the functions that ICU will use for mutex operations sl@0: * Use of this function is optional; by default (without this function), ICU will sl@0: * directly access system functions for mutex operations sl@0: * This function can only be used when ICU is in an initial, unused state, before sl@0: * u_init() has been called. sl@0: * This function may be used even when ICU has been built without multi-threaded sl@0: * support (see ICU_USE_THREADS pre-processor variable, umutex.h) sl@0: * @param context This pointer value will be saved, and then (later) passed as sl@0: * a parameter to the user-supplied mutex functions each time they sl@0: * are called. sl@0: * @param init Pointer to a mutex initialization function. Must be non-null. sl@0: * @param destroy Pointer to the mutex destroy function. Must be non-null. sl@0: * @param lock pointer to the mutex lock function. Must be non-null. sl@0: * @param unlock Pointer to the mutex unlock function. Must be non-null. sl@0: * @param status Receives error values. sl@0: * @stable ICU 2.8 sl@0: * @system sl@0: */ sl@0: U_STABLE void U_EXPORT2 sl@0: u_setMutexFunctions(const void *context, UMtxInitFn *init, UMtxFn *destroy, UMtxFn *lock, UMtxFn *unlock, sl@0: UErrorCode *status); sl@0: sl@0: sl@0: /** sl@0: * Pointer type for a user supplied atomic increment or decrement function. sl@0: * @param context user supplied value, obtained from from u_setAtomicIncDecFunctions(). sl@0: * @param p Pointer to a 32 bit int to be incremented or decremented sl@0: * @return The value of the variable after the inc or dec operation. sl@0: * @stable ICU 2.8 sl@0: * @system sl@0: */ sl@0: typedef int32_t U_CALLCONV UMtxAtomicFn(const void *context, int32_t *p); sl@0: sl@0: /** sl@0: * Set the functions that ICU will use for atomic increment and decrement of int32_t values. sl@0: * Use of this function is optional; by default (without this function), ICU will sl@0: * use its own internal implementation of atomic increment/decrement. sl@0: * This function can only be used when ICU is in an initial, unused state, before sl@0: * u_init() has been called. sl@0: * @param context This pointer value will be saved, and then (later) passed as sl@0: * a parameter to the increment and decrement functions each time they sl@0: * are called. This function can only be called sl@0: * @param inc Pointer to a function to do an atomic increment operation. Must be non-null. sl@0: * @param dec Pointer to a function to do an atomic decrement operation. Must be non-null. sl@0: * @param status Receives error values. sl@0: * @stable ICU 2.8 sl@0: * @system sl@0: */ sl@0: U_STABLE void U_EXPORT2 sl@0: u_setAtomicIncDecFunctions(const void *context, UMtxAtomicFn *inc, UMtxAtomicFn *dec, sl@0: UErrorCode *status); sl@0: sl@0: sl@0: sl@0: /** sl@0: * Pointer type for a user supplied memory allocation function. sl@0: * @param context user supplied value, obtained from from u_setMemoryFunctions(). sl@0: * @param size The number of bytes to be allocated sl@0: * @return Pointer to the newly allocated memory, or NULL if the allocation failed. sl@0: * @stable ICU 2.8 sl@0: * @system sl@0: */ sl@0: typedef void *U_CALLCONV UMemAllocFn(const void *context, size_t size); sl@0: /** sl@0: * Pointer type for a user supplied memory re-allocation function. sl@0: * @param context user supplied value, obtained from from u_setMemoryFunctions(). sl@0: * @param size The number of bytes to be allocated sl@0: * @return Pointer to the newly allocated memory, or NULL if the allocation failed. sl@0: * @stable ICU 2.8 sl@0: * @system sl@0: */ sl@0: typedef void *U_CALLCONV UMemReallocFn(const void *context, void *mem, size_t size); sl@0: /** sl@0: * Pointer type for a user supplied memory free function. Behavior should be sl@0: * similar the standard C library free(). sl@0: * @param context user supplied value, obtained from from u_setMemoryFunctions(). sl@0: * @param mem Pointer to the memory block to be resized sl@0: * @param size The new size for the block sl@0: * @return Pointer to the resized memory block, or NULL if the resizing failed. sl@0: * @stable ICU 2.8 sl@0: * @system sl@0: */ sl@0: typedef void U_CALLCONV UMemFreeFn (const void *context, void *mem); sl@0: sl@0: /** sl@0: * Set the functions that ICU will use for memory allocation. sl@0: * Use of this function is optional; by default (without this function), ICU will sl@0: * use the standard C library malloc() and free() functions. sl@0: * This function can only be used when ICU is in an initial, unused state, before sl@0: * u_init() has been called. sl@0: * @param context This pointer value will be saved, and then (later) passed as sl@0: * a parameter to the memory functions each time they sl@0: * are called. sl@0: * @param a Pointer to a user-supplied malloc function. sl@0: * @param r Pointer to a user-supplied realloc function. sl@0: * @param f Pointer to a user-supplied free function. sl@0: * @param status Receives error values. sl@0: * @stable ICU 2.8 sl@0: * @system sl@0: */ sl@0: U_STABLE void U_EXPORT2 sl@0: u_setMemoryFunctions(const void *context, UMemAllocFn *a, UMemReallocFn *r, UMemFreeFn *f, sl@0: UErrorCode *status); sl@0: sl@0: #endif