From ca9d06db2ca893b6fd582973f59c3013d8e0a91f Mon Sep 17 00:00:00 2001 From: Alexandra Ellwood Date: Fri, 24 Oct 2008 00:58:38 +0000 Subject: [PATCH] Use CFStringGetCStringPtr if possible to avoid wasting memory since CFStringGetMaximumSizeForEncoding is wasteful for UTF8. ticket: new git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@20914 dc483132-0cff-0310-8789-dd5450dbe970 --- src/kim/lib/mac/kim_os_string.c | 37 ++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/kim/lib/mac/kim_os_string.c b/src/kim/lib/mac/kim_os_string.c index 5999021d7..96573eec9 100644 --- a/src/kim/lib/mac/kim_os_string.c +++ b/src/kim/lib/mac/kim_os_string.c @@ -101,21 +101,34 @@ kim_error kim_os_string_create_from_cfstring (kim_string *out_string, if (!err && !in_cfstring) { err = check_error (KIM_NULL_PARAMETER_ERR); } if (!err) { - length = CFStringGetMaximumSizeForEncoding (CFStringGetLength (in_cfstring), - kCFStringEncodingUTF8) + 1; + char *ptr = NULL; - string = (char *) calloc (length, sizeof (char)); - if (!string) { err = check_error (KIM_OUT_OF_MEMORY_ERR); } + /* check if in_cfstring is a C string internally so we can + * avoid using CFStringGetMaximumSizeForEncoding which is wasteful */ + ptr = (char *) CFStringGetCStringPtr(in_cfstring, + kCFStringEncodingUTF8); + if (ptr) { + string = strdup (ptr); + if (!string) { err = check_error (KIM_OUT_OF_MEMORY_ERR); } + + } else { + length = CFStringGetMaximumSizeForEncoding (CFStringGetLength (in_cfstring), + kCFStringEncodingUTF8) + 1; + + string = (char *) calloc (length, sizeof (char)); + if (!string) { err = check_error (KIM_OUT_OF_MEMORY_ERR); } + + if (!err) { + if (!CFStringGetCString (in_cfstring, + (char *) string, + length, + kCFStringEncodingUTF8)) { + err = KIM_OUT_OF_MEMORY_ERR; + } + } + } } - if (!err) { - if (!CFStringGetCString (in_cfstring, - (char *) string, - length, - kCFStringEncodingUTF8)) { - err = KIM_OUT_OF_MEMORY_ERR; - } - } if (!err) { *out_string = string; -- 2.26.2