| 42 | */ |
| 43 | |
| 44 | char * /* O - String pointer */ |
| 45 | _cupsStrAlloc(const char *s) /* I - String */ |
| 46 | { |
| 47 | size_t slen; /* Length of string */ |
| 48 | _cups_sp_item_t *item, /* String pool item */ |
| 49 | *key; /* Search key */ |
| 50 | |
| 51 | |
| 52 | /* |
| 53 | * Range check input... |
| 54 | */ |
| 55 | |
| 56 | if (!s) |
| 57 | return (NULL); |
| 58 | |
| 59 | /* |
| 60 | * Get the string pool... |
| 61 | */ |
| 62 | |
| 63 | _cupsMutexLock(&sp_mutex); |
| 64 | |
| 65 | if (!stringpool) |
| 66 | stringpool = cupsArrayNew((cups_array_func_t)compare_sp_items, NULL); |
| 67 | |
| 68 | if (!stringpool) |
| 69 | { |
| 70 | _cupsMutexUnlock(&sp_mutex); |
| 71 | |
| 72 | return (NULL); |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * See if the string is already in the pool... |
| 77 | */ |
| 78 | |
| 79 | key = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str)); |
| 80 | |
| 81 | if ((item = (_cups_sp_item_t *)cupsArrayFind(stringpool, key)) != NULL) |
| 82 | { |
| 83 | /* |
| 84 | * Found it, return the cached string... |
| 85 | */ |
| 86 | |
| 87 | item->ref_count ++; |
| 88 | |
| 89 | #ifdef DEBUG_GUARDS |
| 90 | DEBUG_printf(("5_cupsStrAlloc: Using string %p(%s) for \"%s\", guard=%08x, " |
| 91 | "ref_count=%d", item, item->str, s, item->guard, |
| 92 | item->ref_count)); |
| 93 | |
| 94 | if (item->guard != _CUPS_STR_GUARD) |
| 95 | abort(); |
| 96 | #endif /* DEBUG_GUARDS */ |
| 97 | |
| 98 | _cupsMutexUnlock(&sp_mutex); |
| 99 | |
| 100 | return (item->str); |
| 101 | } |
no test coverage detected