From 18fa32ded03b62ae123b84a1d140733bbbf7742d Mon Sep 17 00:00:00 2001 From: "Steven R. Loomis" Date: Fri, 19 Feb 2010 17:27:33 +0000 Subject: [PATCH] ICU-6633 warnings fix X-SVN-Rev: 27600 --- icu4c/source/common/icuplug.c | 976 ++++++++++++------------- icu4c/source/common/icuplugimp.h | 2 +- icu4c/source/common/unicode/icuplug.h | 3 +- icu4c/source/tools/icuinfo/icuinfo.cpp | 16 +- icu4c/source/tools/icuinfo/testplug.c | 30 +- 5 files changed, 526 insertions(+), 501 deletions(-) diff --git a/icu4c/source/common/icuplug.c b/icu4c/source/common/icuplug.c index 708d93243f5..ff76d3bc3e9 100644 --- a/icu4c/source/common/icuplug.c +++ b/icu4c/source/common/icuplug.c @@ -27,9 +27,7 @@ #if UPLUG_TRACE #include -#define DBG(x) printf("%s:%d: ",__FILE__,__LINE__); printf x; -#else -#define DBG(x) +#define DBG(x) fprintf(stderr, "%s:%d: ",__FILE__,__LINE__); fprintf x #endif /** @@ -37,19 +35,19 @@ */ struct UPlugData { - UPlugEntrypoint *entrypoint; /**< plugin entrypoint */ - uint32_t structSize; /**< initialized to the size of this structure */ - uint32_t token; /**< must be U_PLUG_TOKEN */ - void *lib; /**< plugin library, or NULL */ - char libName[UPLUG_NAME_MAX]; /**< library name */ - char sym[UPLUG_NAME_MAX]; /**< plugin symbol, or NULL */ - char config[UPLUG_NAME_MAX]; /**< configuration data */ - void *context; /**< user context data */ - char name[UPLUG_NAME_MAX]; /**< name of plugin */ - UPlugLevel level; /**< level of plugin */ - UBool awaitingLoad; /**< TRUE if the plugin is awaiting a load call */ - UBool dontUnload; /**< TRUE if plugin must stay resident (leak plugin and lib) */ - UErrorCode pluginStatus; /**< status code of plugin */ + UPlugEntrypoint *entrypoint; /**< plugin entrypoint */ + uint32_t structSize; /**< initialized to the size of this structure */ + uint32_t token; /**< must be U_PLUG_TOKEN */ + void *lib; /**< plugin library, or NULL */ + char libName[UPLUG_NAME_MAX]; /**< library name */ + char sym[UPLUG_NAME_MAX]; /**< plugin symbol, or NULL */ + char config[UPLUG_NAME_MAX]; /**< configuration data */ + void *context; /**< user context data */ + char name[UPLUG_NAME_MAX]; /**< name of plugin */ + UPlugLevel level; /**< level of plugin */ + UBool awaitingLoad; /**< TRUE if the plugin is awaiting a load call */ + UBool dontUnload; /**< TRUE if plugin must stay resident (leak plugin and lib) */ + UErrorCode pluginStatus; /**< status code of plugin */ }; @@ -57,17 +55,6 @@ struct UPlugData { #define UPLUG_LIBRARY_INITIAL_COUNT 8 #define UPLUG_PLUGIN_INITIAL_COUNT 12 - -/* - - have a callback to determine HIGH or LOW level - - automatically run all LOW level then HIGH level plugins in order -- have icuinfo give diagnostics and info - - have icuinfo give sample config file and location. - - */ - - - /** * Remove an item * @param list the full list @@ -77,19 +64,19 @@ struct UPlugData { * @return the new listsize */ static int32_t uplug_removeEntryAt(void *list, int32_t listSize, int32_t memberSize, int32_t itemToRemove) { - uint8_t *bytePtr = (uint8_t *)list; + uint8_t *bytePtr = (uint8_t *)list; - /* get rid of some bad cases first */ - if(listSize<1) { - return listSize; - } + /* get rid of some bad cases first */ + if(listSize<1) { + return listSize; + } - /* is there anything to move? */ - if(listSize > itemToRemove+1) { - memmove(bytePtr+(itemToRemove*memberSize), bytePtr+((itemToRemove+1)*memberSize), memberSize); - } + /* is there anything to move? */ + if(listSize > itemToRemove+1) { + memmove(bytePtr+(itemToRemove*memberSize), bytePtr+((itemToRemove+1)*memberSize), memberSize); + } - return listSize-1; + return listSize-1; } @@ -107,9 +94,9 @@ struct UPlugLibrary; * @internal */ typedef struct UPlugLibrary { - void *lib; /**< library ptr */ - char name[UPLUG_NAME_MAX]; /**< library name */ - uint32_t ref; /**< reference count */ + void *lib; /**< library ptr */ + char name[UPLUG_NAME_MAX]; /**< library name */ + uint32_t ref; /**< reference count */ } UPlugLibrary; static UPlugLibrary staticLibraryList[UPLUG_LIBRARY_INITIAL_COUNT]; @@ -123,107 +110,110 @@ static int32_t libraryMax = UPLUG_LIBRARY_INITIAL_COUNT; * @return the library's struct */ static int32_t searchForLibraryName(const char *libName) { - int32_t i; + int32_t i; - for(i=0;i= libraryMax) { + /* Ran out of library slots. Statically allocated because we can't depend on allocating memory.. */ + *status = U_MEMORY_ALLOCATION_ERROR; +#if UPLUG_TRACE + DBG((stderr, "uplug_openLibrary() - out of library slots (max %d)\n", libraryMax)); +#endif + return NULL; } - DBG(("uplug_openLibrary(%s,%s) libEntry %d, lib %p, ref %d\n", libName, u_errorName(*status), libEntry, lib, libEntry?libraryList[libEntry].ref:-1)) - return lib; + /* Some operating systems don't want + DL operations from multiple threads. */ + libraryList[libEntry].lib = uprv_dl_open(libName, status); +#if UPLUG_TRACE + DBG((stderr, "uplug_openLibrary(%s,%s) libEntry %d, lib %p\n", libName, u_errorName(*status), libEntry, lib)); +#endif + + if(libraryList[libEntry].lib == NULL || U_FAILURE(*status)) { + /* cleanup. */ + libraryList[libEntry].lib = NULL; /* failure with open */ + libraryList[libEntry].name[0] = 0; +#if UPLUG_TRACE + DBG((stderr, "uplug_openLibrary(%s,%s) libEntry %d, lib %p\n", libName, u_errorName(*status), libEntry, lib)); +#endif + /* no need to free - just won't increase the count. */ + libraryCount--; + } else { /* is it still there? */ + /* link it in */ + strncpy(libraryList[libEntry].name,libName,UPLUG_NAME_MAX); + libraryList[libEntry].ref=1; + lib = libraryList[libEntry].lib; + } + + } else { + lib = libraryList[libEntry].lib; + libraryList[libEntry].ref++; + } + return lib; } U_INTERNAL void U_EXPORT2 uplug_closeLibrary(void *lib, UErrorCode *status) { - int32_t i; + int32_t i; - DBG(("uplug_closeLibrary(%p,%s) list %p\n", lib, u_errorName(*status), (void*)libraryList)) - if(U_FAILURE(*status)) return; +#if UPLUG_TRACE + DBG((stderr, "uplug_closeLibrary(%p,%s) list %p\n", lib, u_errorName(*status), (void*)libraryList)); +#endif + if(U_FAILURE(*status)) return; - DBG(("uplug_closeLibrary(%p,%s) list %p\n", lib, u_errorName(*status), (void*)libraryList)) - for(i=0;i=pastPlug) { - return pluginCount; - } else { - return (d-pluginList)/sizeof(pluginList[0]); - } + UPlugData *pastPlug = &pluginList[pluginCount]; + if(d<=pluginList) { + return 0; + } else if(d>=pastPlug) { + return pluginCount; + } else { + return (d-pluginList)/sizeof(pluginList[0]); + } } U_CAPI UPlugData * U_EXPORT2 uplug_nextPlug(UPlugData *prior) { - if(prior==NULL) { - return pluginList; - } else { - UPlugData *nextPlug = &prior[1]; - UPlugData *pastPlug = &pluginList[pluginCount]; + if(prior==NULL) { + return pluginList; + } else { + UPlugData *nextPlug = &prior[1]; + UPlugData *pastPlug = &pluginList[pluginCount]; - if(nextPlug>=pastPlug) { - return NULL; - } else { - return nextPlug; - } + if(nextPlug>=pastPlug) { + return NULL; + } else { + return nextPlug; } + } } @@ -268,165 +258,165 @@ uplug_nextPlug(UPlugData *prior) { * Call the plugin with some params */ static void uplug_callPlug(UPlugData *plug, UPlugReason reason, UErrorCode *status) { - UPlugTokenReturn token; - if(plug==NULL||U_FAILURE(*status)) { - return; - } - token = (*(plug->entrypoint))(plug, reason, status); - if(token!=UPLUG_TOKEN) { - *status = U_INTERNAL_PROGRAM_ERROR; - } + UPlugTokenReturn token; + if(plug==NULL||U_FAILURE(*status)) { + return; + } + token = (*(plug->entrypoint))(plug, reason, status); + if(token!=UPLUG_TOKEN) { + *status = U_INTERNAL_PROGRAM_ERROR; + } } static void uplug_unloadPlug(UPlugData *plug, UErrorCode *status) { - if(plug->awaitingLoad) { /* shouldn't happen. Plugin hasn'tbeen loaded yet.*/ - *status = U_INTERNAL_PROGRAM_ERROR; - return; - } - if(U_SUCCESS(plug->pluginStatus)) { - /* Don't unload a plug which has a failing load status - means it didn't actually load. */ - uplug_callPlug(plug, UPLUG_REASON_UNLOAD, status); - } + if(plug->awaitingLoad) { /* shouldn't happen. Plugin hasn'tbeen loaded yet.*/ + *status = U_INTERNAL_PROGRAM_ERROR; + return; + } + if(U_SUCCESS(plug->pluginStatus)) { + /* Don't unload a plug which has a failing load status - means it didn't actually load. */ + uplug_callPlug(plug, UPLUG_REASON_UNLOAD, status); + } } static void uplug_queryPlug(UPlugData *plug, UErrorCode *status) { - if(!plug->awaitingLoad || !(plug->level == UPLUG_LEVEL_UNKNOWN) ) { /* shouldn't happen. Plugin hasn'tbeen loaded yet.*/ - *status = U_INTERNAL_PROGRAM_ERROR; - return; - } - plug->level = UPLUG_LEVEL_INVALID; - uplug_callPlug(plug, UPLUG_REASON_QUERY, status); - if(U_SUCCESS(*status)) { - if(plug->level == UPLUG_LEVEL_INVALID) { - plug->pluginStatus = U_PLUGIN_DIDNT_SET_LEVEL; - plug->awaitingLoad = FALSE; - } - } else { - plug->pluginStatus = U_INTERNAL_PROGRAM_ERROR; - plug->awaitingLoad = FALSE; + if(!plug->awaitingLoad || !(plug->level == UPLUG_LEVEL_UNKNOWN) ) { /* shouldn't happen. Plugin hasn'tbeen loaded yet.*/ + *status = U_INTERNAL_PROGRAM_ERROR; + return; + } + plug->level = UPLUG_LEVEL_INVALID; + uplug_callPlug(plug, UPLUG_REASON_QUERY, status); + if(U_SUCCESS(*status)) { + if(plug->level == UPLUG_LEVEL_INVALID) { + plug->pluginStatus = U_PLUGIN_DIDNT_SET_LEVEL; + plug->awaitingLoad = FALSE; } + } else { + plug->pluginStatus = U_INTERNAL_PROGRAM_ERROR; + plug->awaitingLoad = FALSE; + } } static void uplug_loadPlug(UPlugData *plug, UErrorCode *status) { - if(!plug->awaitingLoad || (plug->level < UPLUG_LEVEL_LOW) ) { /* shouldn't happen. Plugin hasn'tbeen loaded yet.*/ - *status = U_INTERNAL_PROGRAM_ERROR; - return; - } - uplug_callPlug(plug, UPLUG_REASON_LOAD, status); - plug->awaitingLoad = FALSE; - if(!U_SUCCESS(*status)) { - plug->pluginStatus = U_INTERNAL_PROGRAM_ERROR; - } + if(!plug->awaitingLoad || (plug->level < UPLUG_LEVEL_LOW) ) { /* shouldn't happen. Plugin hasn'tbeen loaded yet.*/ + *status = U_INTERNAL_PROGRAM_ERROR; + return; + } + uplug_callPlug(plug, UPLUG_REASON_LOAD, status); + plug->awaitingLoad = FALSE; + if(!U_SUCCESS(*status)) { + plug->pluginStatus = U_INTERNAL_PROGRAM_ERROR; + } } static UPlugData *uplug_allocateEmptyPlug(UErrorCode *status) { - UPlugData *plug = NULL; + UPlugData *plug = NULL; - if(U_FAILURE(*status)) { - return NULL; - } + if(U_FAILURE(*status)) { + return NULL; + } - if(pluginCount == UPLUG_PLUGIN_INITIAL_COUNT) { - *status = U_MEMORY_ALLOCATION_ERROR; - return NULL; - } + if(pluginCount == UPLUG_PLUGIN_INITIAL_COUNT) { + *status = U_MEMORY_ALLOCATION_ERROR; + return NULL; + } - plug = &pluginList[pluginCount++]; + plug = &pluginList[pluginCount++]; - plug->token = UPLUG_TOKEN; - plug->structSize = sizeof(UPlugData); - plug->name[0]=0; - plug->level = UPLUG_LEVEL_UNKNOWN; /* initialize to null state */ - plug->awaitingLoad = TRUE; - plug->dontUnload = FALSE; - plug->pluginStatus = U_ZERO_ERROR; - plug->libName[0] = 0; - plug->config[0]=0; - plug->sym[0]=0; - plug->lib=NULL; - plug->entrypoint=NULL; + plug->token = UPLUG_TOKEN; + plug->structSize = sizeof(UPlugData); + plug->name[0]=0; + plug->level = UPLUG_LEVEL_UNKNOWN; /* initialize to null state */ + plug->awaitingLoad = TRUE; + plug->dontUnload = FALSE; + plug->pluginStatus = U_ZERO_ERROR; + plug->libName[0] = 0; + plug->config[0]=0; + plug->sym[0]=0; + plug->lib=NULL; + plug->entrypoint=NULL; - return plug; + return plug; } static UPlugData *uplug_allocatePlug(UPlugEntrypoint *entrypoint, const char *config, void *lib, const char *symName, - UErrorCode *status) { - UPlugData *plug; + UErrorCode *status) { + UPlugData *plug; - if(U_FAILURE(*status)) { - return NULL; - } + if(U_FAILURE(*status)) { + return NULL; + } - plug = uplug_allocateEmptyPlug(status); - if(config!=NULL) { - strncpy(plug->config, config, UPLUG_NAME_MAX); - } else { - plug->config[0] = 0; - } + plug = uplug_allocateEmptyPlug(status); + if(config!=NULL) { + strncpy(plug->config, config, UPLUG_NAME_MAX); + } else { + plug->config[0] = 0; + } - if(symName!=NULL) { - strncpy(plug->sym, symName, UPLUG_NAME_MAX); - } else { - plug->sym[0] = 0; - } + if(symName!=NULL) { + strncpy(plug->sym, symName, UPLUG_NAME_MAX); + } else { + plug->sym[0] = 0; + } - plug->entrypoint = entrypoint; - plug->lib = lib; - uplug_queryPlug(plug, status); + plug->entrypoint = entrypoint; + plug->lib = lib; + uplug_queryPlug(plug, status); - return plug; + return plug; } static void uplug_deallocatePlug(UPlugData *plug, UErrorCode *status) { - UErrorCode subStatus = U_ZERO_ERROR; - if(!plug->dontUnload) { + UErrorCode subStatus = U_ZERO_ERROR; + if(!plug->dontUnload) { #if U_ENABLE_DYLOAD - uplug_closeLibrary(plug->lib, &subStatus); + uplug_closeLibrary(plug->lib, &subStatus); #endif - } - plug->lib = NULL; - if(U_SUCCESS(*status) && U_FAILURE(subStatus)) { - *status = subStatus; - } - /* shift plugins up and decrement count. */ - if(U_SUCCESS(*status)) { - /* all ok- remove. */ - pluginCount = uplug_removeEntryAt(pluginList, pluginCount, sizeof(plug[0]), uplug_pluginNumber(plug)); - } else { - /* not ok- leave as a message. */ - plug->awaitingLoad=FALSE; - plug->entrypoint=0; - plug->dontUnload=TRUE; - } + } + plug->lib = NULL; + if(U_SUCCESS(*status) && U_FAILURE(subStatus)) { + *status = subStatus; + } + /* shift plugins up and decrement count. */ + if(U_SUCCESS(*status)) { + /* all ok- remove. */ + pluginCount = uplug_removeEntryAt(pluginList, pluginCount, sizeof(plug[0]), uplug_pluginNumber(plug)); + } else { + /* not ok- leave as a message. */ + plug->awaitingLoad=FALSE; + plug->entrypoint=0; + plug->dontUnload=TRUE; + } } static void uplug_doUnloadPlug(UPlugData *plugToRemove, UErrorCode *status) { - if(plugToRemove != NULL) { - uplug_unloadPlug(plugToRemove, status); - uplug_deallocatePlug(plugToRemove, status); - } + if(plugToRemove != NULL) { + uplug_unloadPlug(plugToRemove, status); + uplug_deallocatePlug(plugToRemove, status); + } } U_CAPI void U_EXPORT2 uplug_removePlug(UPlugData *plug, UErrorCode *status) { - UPlugData *cursor = NULL; - UPlugData *plugToRemove = NULL; - if(U_FAILURE(*status)) return; + UPlugData *cursor = NULL; + UPlugData *plugToRemove = NULL; + if(U_FAILURE(*status)) return; - for(cursor=pluginList;cursor!=NULL;) { - if(cursor==plug) { - plugToRemove = plug; - cursor=NULL; - } else { - cursor = uplug_nextPlug(cursor); - } + for(cursor=pluginList;cursor!=NULL;) { + if(cursor==plug) { + plugToRemove = plug; + cursor=NULL; + } else { + cursor = uplug_nextPlug(cursor); } + } - uplug_doUnloadPlug(plugToRemove, status); + uplug_doUnloadPlug(plugToRemove, status); } @@ -435,86 +425,86 @@ uplug_removePlug(UPlugData *plug, UErrorCode *status) { U_CAPI void U_EXPORT2 uplug_setPlugNoUnload(UPlugData *data, UBool dontUnload) { - data->dontUnload = dontUnload; + data->dontUnload = dontUnload; } U_CAPI void U_EXPORT2 uplug_setPlugLevel(UPlugData *data, UPlugLevel level) { - data->level = level; + data->level = level; } U_CAPI UPlugLevel U_EXPORT2 uplug_getPlugLevel(UPlugData *data) { - return data->level; + return data->level; } U_CAPI void U_EXPORT2 uplug_setPlugName(UPlugData *data, const char *name) { - strncpy(data->name, name, UPLUG_NAME_MAX); + strncpy(data->name, name, UPLUG_NAME_MAX); } U_CAPI const char * U_EXPORT2 uplug_getPlugName(UPlugData *data) { - return data->name; + return data->name; } U_CAPI const char * U_EXPORT2 uplug_getSymbolName(UPlugData *data) { - return data->sym; + return data->sym; } U_CAPI const char * U_EXPORT2 uplug_getLibraryName(UPlugData *data, UErrorCode *status) { - if(data->libName[0]) { - return data->libName; - } else { + if(data->libName[0]) { + return data->libName; + } else { #if U_ENABLE_DYLOAD - return uplug_findLibrary(data->lib, status); + return uplug_findLibrary(data->lib, status); #else - return NULL; + return NULL; #endif - } + } } U_CAPI void * U_EXPORT2 uplug_getLibrary(UPlugData *data) { - return data->lib; + return data->lib; } U_CAPI void * U_EXPORT2 uplug_getContext(UPlugData *data) { - return data->context; + return data->context; } U_CAPI void U_EXPORT2 uplug_setContext(UPlugData *data, void *context) { - data->context = context; + data->context = context; } U_CAPI const char* U_EXPORT2 uplug_getConfiguration(UPlugData *data) { - return data->config; + return data->config; } U_INTERNAL UPlugData* U_EXPORT2 uplug_getPlugInternal(int32_t n) { - if(n <0 || n >= pluginCount) { - return NULL; - } else { - return &(pluginList[n]); - } + if(n <0 || n >= pluginCount) { + return NULL; + } else { + return &(pluginList[n]); + } } U_CAPI UErrorCode U_EXPORT2 uplug_getPlugLoadStatus(UPlugData *plug) { - return plug->pluginStatus; + return plug->pluginStatus; } @@ -524,54 +514,54 @@ uplug_getPlugLoadStatus(UPlugData *plug) { * Initialize a plugin fron an entrypoint and library - but don't load it. */ static UPlugData* uplug_initPlugFromEntrypointAndLibrary(UPlugEntrypoint *entrypoint, const char *config, void *lib, const char *sym, - UErrorCode *status) { - UPlugData *plug = NULL; + UErrorCode *status) { + UPlugData *plug = NULL; - plug = uplug_allocatePlug(entrypoint, config, lib, sym, status); + plug = uplug_allocatePlug(entrypoint, config, lib, sym, status); - if(U_SUCCESS(*status)) { - return plug; - } else { - uplug_deallocatePlug(plug, status); - return NULL; - } + if(U_SUCCESS(*status)) { + return plug; + } else { + uplug_deallocatePlug(plug, status); + return NULL; + } } U_CAPI UPlugData* U_EXPORT2 uplug_loadPlugFromEntrypoint(UPlugEntrypoint *entrypoint, const char *config, UErrorCode *status) { - UPlugData* plug = uplug_initPlugFromEntrypointAndLibrary(entrypoint, config, NULL, NULL, status); - uplug_loadPlug(plug, status); - return plug; + UPlugData* plug = uplug_initPlugFromEntrypointAndLibrary(entrypoint, config, NULL, NULL, status); + uplug_loadPlug(plug, status); + return plug; } static UPlugData* uplug_initErrorPlug(const char *libName, const char *sym, const char *config, const char *nameOrError, UErrorCode loadStatus, UErrorCode *status) { - UPlugData *plug = uplug_allocateEmptyPlug(status); - if(U_FAILURE(*status)) return NULL; + UPlugData *plug = uplug_allocateEmptyPlug(status); + if(U_FAILURE(*status)) return NULL; - plug->pluginStatus = loadStatus; - plug->awaitingLoad = FALSE; /* Won't load. */ - plug->dontUnload = TRUE; /* cannot unload. */ + plug->pluginStatus = loadStatus; + plug->awaitingLoad = FALSE; /* Won't load. */ + plug->dontUnload = TRUE; /* cannot unload. */ - if(sym!=NULL) { - strncpy(plug->sym, sym, UPLUG_NAME_MAX); - } + if(sym!=NULL) { + strncpy(plug->sym, sym, UPLUG_NAME_MAX); + } - if(libName!=NULL) { - strncpy(plug->libName, libName, UPLUG_NAME_MAX); - } + if(libName!=NULL) { + strncpy(plug->libName, libName, UPLUG_NAME_MAX); + } - if(nameOrError!=NULL) { - strncpy(plug->name, nameOrError, UPLUG_NAME_MAX); - } + if(nameOrError!=NULL) { + strncpy(plug->name, nameOrError, UPLUG_NAME_MAX); + } - if(config!=NULL) { - strncpy(plug->config, config, UPLUG_NAME_MAX); - } + if(config!=NULL) { + strncpy(plug->config, config, UPLUG_NAME_MAX); + } - return plug; + return plug; } /** @@ -582,250 +572,254 @@ uplug_initErrorPlug(const char *libName, const char *sym, const char *config, co static UPlugData* uplug_initPlugFromLibrary(const char *libName, const char *sym, const char *config, UErrorCode *status) { - void *lib = NULL; - UPlugData *plug = NULL; - void *entrypoint = NULL; - if(U_FAILURE(*status)) { return NULL; } - lib = uplug_openLibrary(libName, status); - if(lib!=NULL && U_SUCCESS(*status)) { - entrypoint = uprv_dl_sym(lib, sym, status); - - if(entrypoint!=NULL&&U_SUCCESS(*status)) { - /* TODO: warning on next line needs to be corrected by complex C syntax. */ - plug = uplug_initPlugFromEntrypointAndLibrary((UPlugEntrypoint*)entrypoint, config, lib, sym, status); - if(plug!=NULL&&U_SUCCESS(*status)) { - plug->lib = lib; /* plug takes ownership of library */ - lib = NULL; /* library is now owned by plugin. */ - } - } else { - UErrorCode subStatus = U_ZERO_ERROR; - plug = uplug_initErrorPlug(libName,sym,config,"ERROR: Could not load entrypoint",(lib==NULL)?U_MISSING_RESOURCE_ERROR:*status,&subStatus); - } - if(lib!=NULL) { /* still need to close the lib */ - UErrorCode subStatus = U_ZERO_ERROR; - uplug_closeLibrary(lib, &subStatus); /* don't care here */ - } - } else { - UErrorCode subStatus = U_ZERO_ERROR; - plug = uplug_initErrorPlug(libName,sym,config,"ERROR: could not load library",(lib==NULL)?U_MISSING_RESOURCE_ERROR:*status,&subStatus); + void *lib = NULL; + UPlugData *plug = NULL; + if(U_FAILURE(*status)) { return NULL; } + lib = uplug_openLibrary(libName, status); + if(lib!=NULL && U_SUCCESS(*status)) { + UPlugEntrypoint *entrypoint = NULL; + /* + * ISO forbids the following cast. + * See: http://www.trilithium.com/johan/2004/12/problem-with-dlsym/ + */ + entrypoint = (UPlugEntrypoint*)uprv_dl_sym(lib, sym, status); + + if(entrypoint!=NULL&&U_SUCCESS(*status)) { + plug = uplug_initPlugFromEntrypointAndLibrary(entrypoint, config, lib, sym, status); + if(plug!=NULL&&U_SUCCESS(*status)) { + plug->lib = lib; /* plug takes ownership of library */ + lib = NULL; /* library is now owned by plugin. */ + } + } else { + UErrorCode subStatus = U_ZERO_ERROR; + plug = uplug_initErrorPlug(libName,sym,config,"ERROR: Could not load entrypoint",(lib==NULL)?U_MISSING_RESOURCE_ERROR:*status,&subStatus); } - return plug; + if(lib!=NULL) { /* still need to close the lib */ + UErrorCode subStatus = U_ZERO_ERROR; + uplug_closeLibrary(lib, &subStatus); /* don't care here */ + } + } else { + UErrorCode subStatus = U_ZERO_ERROR; + plug = uplug_initErrorPlug(libName,sym,config,"ERROR: could not load library",(lib==NULL)?U_MISSING_RESOURCE_ERROR:*status,&subStatus); + } + return plug; } U_CAPI UPlugData* U_EXPORT2 uplug_loadPlugFromLibrary(const char *libName, const char *sym, const char *config, UErrorCode *status) { - UPlugData *plug = NULL; - if(U_FAILURE(*status)) { return NULL; } - plug = uplug_initPlugFromLibrary(libName, sym, config, status); - uplug_loadPlug(plug, status); + UPlugData *plug = NULL; + if(U_FAILURE(*status)) { return NULL; } + plug = uplug_initPlugFromLibrary(libName, sym, config, status); + uplug_loadPlug(plug, status); - return plug; + return plug; } #endif - - - U_CAPI UPlugLevel U_EXPORT2 uplug_getCurrentLevel() { - if(cmemory_inUse()) { - return UPLUG_LEVEL_HIGH; - } else { - return UPLUG_LEVEL_LOW; - } + if(cmemory_inUse()) { + return UPLUG_LEVEL_HIGH; + } else { + return UPLUG_LEVEL_LOW; + } } static UBool U_CALLCONV uplug_cleanup(void) { - int32_t i; + int32_t i; - UPlugData *pluginToRemove; - /* cleanup plugs */ - for(i=0;iawaitingLoad) { - if(pluginToLoad->level == UPLUG_LEVEL_LOW) { - DBG(( "Low #%d: Awaiting load: %p. Level: %d\n", i, (void*)pluginToLoad, - (int32_t)currentLevel)) - if(currentLevel > UPLUG_LEVEL_LOW) { - pluginToLoad->pluginStatus = U_PLUGIN_TOO_HIGH; - } else { - UPlugLevel newLevel; - uplug_loadPlug(pluginToLoad, &subStatus); - newLevel = uplug_getCurrentLevel(); - if(newLevel > currentLevel) { - pluginToLoad->pluginStatus = U_PLUGIN_CHANGED_LEVEL_WARNING; - currentLevel = newLevel; - DBG(( "Low #%d: Level change! %p\n", i, (void*)pluginToLoad)) - } - } - pluginToLoad->awaitingLoad = FALSE; - DBG(( " %p -> %s, %s. Level: %d\n", (void*)pluginToLoad, u_errorName(subStatus), u_errorName(pluginToLoad->pluginStatus), - (int32_t)currentLevel)); - } + if(U_FAILURE(*status)) { + return; + } +#if UPLUG_TRACE + DBG((stderr, "uplug_loadWaitingPlugs() Level: %d\n", currentLevel)); +#endif + /* pass #1: low level plugs */ + for(i=0;iawaitingLoad) { + if(pluginToLoad->level == UPLUG_LEVEL_LOW) { + if(currentLevel > UPLUG_LEVEL_LOW) { + pluginToLoad->pluginStatus = U_PLUGIN_TOO_HIGH; + } else { + UPlugLevel newLevel; + uplug_loadPlug(pluginToLoad, &subStatus); + newLevel = uplug_getCurrentLevel(); + if(newLevel > currentLevel) { + pluginToLoad->pluginStatus = U_PLUGIN_CHANGED_LEVEL_WARNING; + currentLevel = newLevel; + } } + pluginToLoad->awaitingLoad = FALSE; + } } - currentLevel = uplug_getCurrentLevel(); + } + currentLevel = uplug_getCurrentLevel(); - for(i=0;iawaitingLoad) { - DBG(( "High #%d: Awaiting load: %p. Level: %d\n", i, (void*)pluginToLoad, - (int32_t)currentLevel)) - if(pluginToLoad->level == UPLUG_LEVEL_INVALID) { - pluginToLoad->pluginStatus = U_PLUGIN_DIDNT_SET_LEVEL; - } else if(pluginToLoad->level == UPLUG_LEVEL_UNKNOWN) { - pluginToLoad->pluginStatus = U_INTERNAL_PROGRAM_ERROR; - } else { - uplug_loadPlug(pluginToLoad, &subStatus); - } - pluginToLoad->awaitingLoad = FALSE; - DBG(( " %p -> %s, %s. Level: %d\n", (void*)pluginToLoad, u_errorName(subStatus), u_errorName(pluginToLoad->pluginStatus), - (int32_t)currentLevel)); - - } + if(pluginToLoad->awaitingLoad) { + if(pluginToLoad->level == UPLUG_LEVEL_INVALID) { + pluginToLoad->pluginStatus = U_PLUGIN_DIDNT_SET_LEVEL; + } else if(pluginToLoad->level == UPLUG_LEVEL_UNKNOWN) { + pluginToLoad->pluginStatus = U_INTERNAL_PROGRAM_ERROR; + } else { + uplug_loadPlug(pluginToLoad, &subStatus); + } + pluginToLoad->awaitingLoad = FALSE; } + } - DBG(( " Done Loading Plugs. Level: Level: %d\n", (int32_t)uplug_getCurrentLevel())); +#if UPLUG_TRACE + DBG((stderr, " Done Loading Plugs. Level: %d\n", (int32_t)uplug_getCurrentLevel())); +#endif; } #if U_ENABLE_DYLOAD +/* Name of the plugin config file */ static char plugin_file[2048] = ""; #endif U_INTERNAL const char* U_EXPORT2 uplug_getPluginFile() { #if U_ENABLE_DYLOAD - return plugin_file; + return plugin_file; #else - return NULL; + return NULL; #endif } U_CAPI void U_EXPORT2 uplug_init(UErrorCode *status) { -#if U_ENABLE_DYLOAD - const char *plugin_dir; - - plugin_dir = getenv("ICU_PLUGINS"); - DBG((">> ICU_PLUGINS=%s\n", plugin_dir)) +#if !U_ENABLE_DYLOAD + (void)status; /* unused */ +#else + const char *plugin_dir; + + if(U_FAILURE(*status)) return; + plugin_dir = getenv("ICU_PLUGINS"); #if defined(DEFAULT_ICU_PLUGINS) - if(plugin_dir == NULL || !*plugin_dir) { - plugin_dir = DEFAULT_ICU_PLUGINS; - DBG((" (using DEFAULT_ICU_PLUGINS)\n")) - } + if(plugin_dir == NULL || !*plugin_dir) { + plugin_dir = DEFAULT_ICU_PLUGINS; + } #endif - if(plugin_dir != NULL && *plugin_dir) { - FILE *f; - - - strncpy(plugin_file, plugin_dir, 2047); - strncat(plugin_file, U_FILE_SEP_STRING,2047); - strncat(plugin_file, "icuplugins",2047); - strncat(plugin_file, U_ICU_VERSION_SHORT ,2047); - strncat(plugin_file, ".txt" ,2047); - - DBG((">> pluginfile [%s]\n", plugin_file)) - - f = fopen(plugin_file, "r"); +#if UPLUG_TRACE + DBG((stderr, "ICU_PLUGINS=%s\n", plugin_dir)); +#endif - if(f != NULL) { - char linebuf[1024]; - char *p, *libName=NULL, *symName=NULL, *config=NULL; - int32_t line = 0; + if(plugin_dir != NULL && *plugin_dir) { + FILE *f; + + + strncpy(plugin_file, plugin_dir, 2047); + strncat(plugin_file, U_FILE_SEP_STRING,2047); + strncat(plugin_file, "icuplugins",2047); + strncat(plugin_file, U_ICU_VERSION_SHORT ,2047); + strncat(plugin_file, ".txt" ,2047); + +#if UPLUG_TRACE + DBG((stderr, "pluginfile= %s\n", plugin_file)); +#endif + + f = fopen(plugin_file, "r"); + + if(f != NULL) { + char linebuf[1024]; + char *p, *libName=NULL, *symName=NULL, *config=NULL; + int32_t line = 0; - while(fgets(linebuf,1023,f)) { - line++; - DBG(("#%d: %s",line,linebuf)) + while(fgets(linebuf,1023,f)) { + line++; - if(!*linebuf || *linebuf=='#') { - continue; - } else { - p = linebuf; - while(*p&&isspace(*p)) - p++; - if(!*p || *p=='#') continue; - libName = p; - while(*p&&!isspace(*p)) { - p++; - } - if(!*p || *p=='#') continue; /* no tab after libname */ - *p=0; /* end of libname */ - p++; - while(*p&&isspace(*p)) { - p++; - } - if(!*p||*p=='#') continue; /* no symname after libname +tab */ - symName = p; - while(*p&&!isspace(*p)) { - p++; - } + if(!*linebuf || *linebuf=='#') { + continue; + } else { + p = linebuf; + while(*p&&isspace(*p)) + p++; + if(!*p || *p=='#') continue; + libName = p; + while(*p&&!isspace(*p)) { + p++; + } + if(!*p || *p=='#') continue; /* no tab after libname */ + *p=0; /* end of libname */ + p++; + while(*p&&isspace(*p)) { + p++; + } + if(!*p||*p=='#') continue; /* no symname after libname +tab */ + symName = p; + while(*p&&!isspace(*p)) { + p++; + } - if(*p) { /* has config */ - *p=0; - ++p; - while(*p&&isspace(*p)) { - p++; - } - if(*p) { - config = p; - } - } - - /* chop whitespace at the end of the config */ - if(config!=NULL&&*config!=0) { - p = config+strlen(config); - while(p>config&&isspace(*(--p))) { - *p=0; - } - } - - /* OK, we're good. */ - DBG(("PLUGIN libName=[%s], sym=[%s], config=[%s]\n", libName, symName, config)) - - { - UErrorCode subStatus = U_ZERO_ERROR; - UPlugData *plug = uplug_initPlugFromLibrary(libName, symName, config, &subStatus); - DBG((" -> %p, %s\n", (void*)plug, u_errorName(subStatus))) - } - } + if(*p) { /* has config */ + *p=0; + ++p; + while(*p&&isspace(*p)) { + p++; } - } else { - DBG(("Can't open plugin file %s\n", plugin_file)) + if(*p) { + config = p; + } + } + + /* chop whitespace at the end of the config */ + if(config!=NULL&&*config!=0) { + p = config+strlen(config); + while(p>config&&isspace(*(--p))) { + *p=0; + } + } + + /* OK, we're good. */ + { + UErrorCode subStatus = U_ZERO_ERROR; + UPlugData *plug = uplug_initPlugFromLibrary(libName, symName, config, &subStatus); + if(U_FAILURE(subStatus) && U_SUCCESS(*status)) { + *status = subStatus; + } +#if UPLUG_TRACE + DBG((stderr, "PLUGIN libName=[%s], sym=[%s], config=[%s]\n", libName, symName, config)); + DBG((stderr, " -> %p, %s\n", (void*)plug, u_errorName(subStatus))); +#else + (void)plug; /* unused */ +#endif + } } + } + } else { +#if UPLUG_TRACE + DBG((stderr, "Can't open plugin file %s\n", plugin_file)); +#endif } - uplug_loadWaitingPlugs(status); + } + uplug_loadWaitingPlugs(status); #endif /* U_ENABLE_DYLOAD */ - ucln_registerCleanup(UCLN_UPLUG, uplug_cleanup); + ucln_registerCleanup(UCLN_UPLUG, uplug_cleanup); } - - diff --git a/icu4c/source/common/icuplugimp.h b/icu4c/source/common/icuplugimp.h index 72a197da16a..53b9c0cd6f0 100644 --- a/icu4c/source/common/icuplugimp.h +++ b/icu4c/source/common/icuplugimp.h @@ -80,7 +80,7 @@ uplug_getPlugInternal(int32_t n); * @internal - Internal use only. */ U_INTERNAL const char* U_EXPORT2 -uplug_getPluginFile(); +uplug_getPluginFile(void); /** @} */ diff --git a/icu4c/source/common/unicode/icuplug.h b/icu4c/source/common/unicode/icuplug.h index be886f430ae..ac413b937ba 100644 --- a/icu4c/source/common/unicode/icuplug.h +++ b/icu4c/source/common/unicode/icuplug.h @@ -221,7 +221,8 @@ uplug_getPlugLevel(UPlugData *plug); * @return the lowest level of plug which can currently load * @internal ICU 4.4 Technology Preview */ -U_CAPI UPlugLevel U_EXPORT2 uplug_getCurrentLevel(); +U_CAPI UPlugLevel U_EXPORT2 +uplug_getCurrentLevel(void); /** diff --git a/icu4c/source/tools/icuinfo/icuinfo.cpp b/icu4c/source/tools/icuinfo/icuinfo.cpp index 82f913f16ca..9e52a26c6c4 100644 --- a/icu4c/source/tools/icuinfo/icuinfo.cpp +++ b/icu4c/source/tools/icuinfo/icuinfo.cpp @@ -45,6 +45,15 @@ static UOption options[]={ /*6*/ UOPTION_DEF("cleanup", 'K', UOPT_NO_ARG), }; +static UErrorCode initStatus = U_ZERO_ERROR; +static UBool icuInitted = FALSE; + +static void do_init() { + if(!icuInitted) { + u_init(&initStatus); + icuInitted = TRUE; + } +} /** * Print the current platform @@ -104,9 +113,9 @@ void cmd_version(UBool noLoad) printf("Cygwin: CYGWINMSVC\n"); #endif printf("ICUDATA: %s\n", U_ICUDATA_NAME); - u_init(&status); + do_init(); printf("Data Directory: %s\n", u_getDataDirectory()); - printf("ICU Initialization returned: %s\n", u_errorName(status)); + printf("ICU Initialization returned: %s\n", u_errorName(initStatus)); printf( "Default locale: %s\n", uloc_getDefault()); { UErrorCode subStatus = U_ZERO_ERROR; @@ -169,6 +178,9 @@ void cmd_cleanup() void cmd_listplugins() { int32_t i; UPlugData *plug; + + do_init(); + printf("ICU Initialized: u_init() returned %s\n", u_errorName(initStatus)); printf("Plugins: \n"); printf( "# %6s %s \n", diff --git a/icu4c/source/tools/icuinfo/testplug.c b/icu4c/source/tools/icuinfo/testplug.c index 2d60b9c0a19..e97bb69e0b6 100644 --- a/icu4c/source/tools/icuinfo/testplug.c +++ b/icu4c/source/tools/icuinfo/testplug.c @@ -24,6 +24,19 @@ #include "unicode/uclean.h" #include "cmemory.h" +/** + * Prototypes + */ +#define DECLARE_PLUGIN(x) U_CAPI UPlugTokenReturn U_EXPORT2 x (UPlugData *data, UPlugReason reason, UErrorCode *status) + +DECLARE_PLUGIN(myPlugin); +DECLARE_PLUGIN(myPluginLow); +DECLARE_PLUGIN(myPluginFailQuery); +DECLARE_PLUGIN(myPluginFailToken); +DECLARE_PLUGIN(myPluginBad); +DECLARE_PLUGIN(myPluginHigh); +DECLARE_PLUGIN(debugMemoryPlugin); + /** * A simple, trivial plugin. */ @@ -141,25 +154,30 @@ UPlugTokenReturn U_EXPORT2 myPluginHigh ( /* Debug Memory Plugin (see hpmufn.c) */ static void * U_CALLCONV myMemAlloc(const void *context, size_t size) { - void *retPtr = (void *)malloc(size); - fprintf(stderr, "MEM: malloc(%d) = %p\n", size, retPtr); - return retPtr; + void *retPtr = (void *)malloc(size); + (void)context; /* unused */ + fprintf(stderr, "MEM: malloc(%d) = %p\n", (int32_t)size, retPtr); + return retPtr; } static void U_CALLCONV myMemFree(const void *context, void *mem) { - free(mem); - fprintf(stderr, "MEM: free(%p)\n", mem); + (void)context; /* unused */ + + free(mem); + fprintf(stderr, "MEM: free(%p)\n", mem); } static void * U_CALLCONV myMemRealloc(const void *context, void *mem, size_t size) { void *retPtr; + (void)context; /* unused */ + if(mem==NULL) { retPtr = NULL; } else { retPtr = realloc(mem, size); } - fprintf(stderr, "MEM: realloc(%p, %d) = %p\n", mem, size, retPtr); + fprintf(stderr, "MEM: realloc(%p, %d) = %p\n", mem, (int32_t)size, retPtr); return retPtr; }