U_CAPI UPlugTokenReturn U_EXPORT2 debugMemoryPlugin ( UPlugData *data, UPlugReason reason, UErrorCode *status) { fprintf(stderr,"debugMemoryPlugin: data=%p, reason=%s, status=%s\n", (void*)data, udbg_enumName(UDBG_UPlugReason,(int32_t)reason), u_errorName(*status)); if(reason==UPLUG_REASON_QUERY) { uplug_setPlugLevel(data, UPLUG_LEVEL_LOW); uplug_setPlugName(data, "Memory Plugin"); } else if(reason==UPLUG_REASON_LOAD) { u_setMemoryFunctions(uplug_getContext(data), &myMemAlloc, &myMemRealloc, &myMemFree, status); fprintf(stderr, "MEM: status now %s\n", u_errorName(*status)); } else if(reason==UPLUG_REASON_UNLOAD) { fprintf(stderr, "MEM: not possible to unload this plugin (no way to reset memory functions)...\n"); uplug_setPlugNoUnload(data, TRUE); } return UPLUG_TOKEN; }
static void TestHeapFunctions() { UErrorCode status = U_ZERO_ERROR; UResourceBundle *rb = NULL; char *icuDataDir; UVersionInfo unicodeVersion = {0,0,0,0}; icuDataDir = safeGetICUDataDirectory(); /* save icu data dir, so we can put it back * after doing u_cleanup(). */ /* Verify that ICU can be cleaned up and reinitialized successfully. * Failure here usually means that some ICU service didn't clean up successfully, * probably because some earlier test accidently left something open. */ ctest_resetICU(); /* Can not set memory functions if ICU is already initialized */ u_setMemoryFunctions(&gContext, myMemAlloc, myMemRealloc, myMemFree, &status); TEST_STATUS(status, U_INVALID_STATE_ERROR); /* Un-initialize ICU */ u_cleanup(); /* Can not set memory functions with NULL values */ status = U_ZERO_ERROR; u_setMemoryFunctions(&gContext, NULL, myMemRealloc, myMemFree, &status); TEST_STATUS(status, U_ILLEGAL_ARGUMENT_ERROR); status = U_ZERO_ERROR; u_setMemoryFunctions(&gContext, myMemAlloc, NULL, myMemFree, &status); TEST_STATUS(status, U_ILLEGAL_ARGUMENT_ERROR); status = U_ZERO_ERROR; u_setMemoryFunctions(&gContext, myMemAlloc, myMemRealloc, NULL, &status); TEST_STATUS(status, U_ILLEGAL_ARGUMENT_ERROR); /* u_setMemoryFunctions() should work with null or non-null context pointer */ status = U_ZERO_ERROR; u_setMemoryFunctions(NULL, myMemAlloc, myMemRealloc, myMemFree, &status); TEST_STATUS(status, U_ZERO_ERROR); u_setMemoryFunctions(&gContext, myMemAlloc, myMemRealloc, myMemFree, &status); TEST_STATUS(status, U_ZERO_ERROR); /* After reinitializing ICU, we should not be able to set the memory funcs again. */ status = U_ZERO_ERROR; u_setDataDirectory(icuDataDir); u_init(&status); TEST_STATUS(status, U_ZERO_ERROR); u_setMemoryFunctions(NULL, myMemAlloc, myMemRealloc, myMemFree, &status); TEST_STATUS(status, U_INVALID_STATE_ERROR); /* Doing ICU operations should cause allocations to come through our test heap */ gBlockCount = 0; status = U_ZERO_ERROR; rb = ures_open(NULL, "es", &status); TEST_STATUS(status, U_ZERO_ERROR); if (gBlockCount == 0) { log_err("Heap functions are not being called from ICU.\n"); } ures_close(rb); /* Cleanup should put the heap back to its default implementation. */ ctest_resetICU(); u_getUnicodeVersion(unicodeVersion); if (unicodeVersion[0] <= 0) { log_err("Properties doesn't reinitialize without u_init.\n"); } status = U_ZERO_ERROR; u_init(&status); TEST_STATUS(status, U_ZERO_ERROR); /* ICU operations should no longer cause allocations to come through our test heap */ gBlockCount = 0; status = U_ZERO_ERROR; rb = ures_open(NULL, "fr", &status); TEST_STATUS(status, U_ZERO_ERROR); if (gBlockCount != 0) { log_err("Heap functions did not reset after u_cleanup.\n"); } ures_close(rb); free(icuDataDir); ctest_resetICU(); }
int T_CTEST_EXPORT2 initArgs( int argc, const char* const argv[], ArgHandlerPtr argHandler, void *context) { int i; int argSkip = 0; VERBOSITY = FALSE; ERR_MSG = TRUE; ARGV_0=argv[0]; for( i=1; i<argc; i++) { if ( argv[i][0] == '/' ) { /* We don't run the tests here. */ continue; } else if ((strcmp( argv[i], "-a") == 0) || (strcmp(argv[i],"-all") == 0)) { /* We don't run the tests here. */ continue; } else if (strcmp( argv[i], "-v" )==0 || strcmp( argv[i], "-verbose")==0) { VERBOSITY = TRUE; } else if (strcmp( argv[i], "-l" )==0 ) { /* doList = TRUE; */ } else if (strcmp( argv[i], "-e1") == 0) { QUICK = -1; } else if (strcmp( argv[i], "-e") ==0) { QUICK = 0; } else if (strcmp( argv[i], "-w") ==0) { WARN_ON_MISSING_DATA = TRUE; } else if (strcmp( argv[i], "-m") ==0) { UErrorCode errorCode = U_ZERO_ERROR; if (i+1 < argc) { char *endPtr = NULL; i++; MINIMUM_MEMORY_SIZE_FAILURE = (size_t)strtol(argv[i], &endPtr, 10); if (endPtr == argv[i]) { printf("Can't parse %s\n", argv[i]); help(argv[0]); return 0; } if (*endPtr == '-') { char *maxPtr = endPtr+1; endPtr = NULL; MAXIMUM_MEMORY_SIZE_FAILURE = (size_t)strtol(maxPtr, &endPtr, 10); if (endPtr == argv[i]) { printf("Can't parse %s\n", argv[i]); help(argv[0]); return 0; } } } /* Use the default value */ u_setMemoryFunctions(NULL, ctest_libMalloc, ctest_libRealloc, ctest_libFree, &errorCode); if (U_FAILURE(errorCode)) { printf("u_setMemoryFunctions returned %s\n", u_errorName(errorCode)); return 0; } } else if(strcmp( argv[i], "-n") == 0 || strcmp( argv[i], "-no_err_msg") == 0) { ERR_MSG = FALSE; } else if (strcmp( argv[i], "-r") == 0) { if (!REPEAT_TESTS_INIT) { REPEAT_TESTS++; } } else if (strcmp( argv[i], "-x") == 0) { if(++i>=argc) { printf("* Error: '-x' option requires an argument. usage: '-x outfile.xml'.\n"); return 0; } if(ctest_xml_setFileName(argv[i])) { /* set the name */ return 0; } } else if (strcmp( argv[i], "-t_info") == 0) { ICU_TRACE = UTRACE_INFO; } else if (strcmp( argv[i], "-t_error") == 0) { ICU_TRACE = UTRACE_ERROR; } else if (strcmp( argv[i], "-t_warn") == 0) { ICU_TRACE = UTRACE_WARNING; } else if (strcmp( argv[i], "-t_verbose") == 0) { ICU_TRACE = UTRACE_VERBOSE; } else if (strcmp( argv[i], "-t_oc") == 0) { ICU_TRACE = UTRACE_OPEN_CLOSE; } else if (strcmp( argv[i], "-h" )==0 || strcmp( argv[i], "--help" )==0) { help( argv[0] ); return 0; } else if (argHandler != NULL && (argSkip = argHandler(i, argc, argv, context)) > 0) { i += argSkip - 1; } else { printf("* unknown option: %s\n", argv[i]); help( argv[0] ); return 0; } } if (ICU_TRACE != UTRACE_OFF) { utrace_setFunctions(NULL, TraceEntry, TraceExit, TraceData); utrace_setLevel(ICU_TRACE); } return 1; /* total error count */ }