1 /* 2 * Pool for shared strings. 3 * 4 * Copyright (c) 2010 Marko Kreen, Skype Technologies OÜ 5 * 6 * Permission to use, copy, modify, and/or distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /** 20 * @file 21 * 22 * Storage for shared strings. 23 * 24 * This provides refcounted searchable string pool for cases 25 * where lot of objects reference same strings. 26 */ 27 28 #ifndef _USUAL_STRPOOL_H_ 29 #define _USUAL_STRPOOL_H_ 30 31 #include <usual/cxalloc.h> 32 33 /** Handle for the pool */ 34 struct StrPool; 35 36 /** Pooled String */ 37 struct PStr { 38 /** Parent pool */ 39 struct StrPool *pool; 40 /** String length */ 41 size_t len; 42 /** Reference count */ 43 int refcnt; 44 /** Zero-terminated value */ 45 char str[FLEX_ARRAY]; 46 }; 47 48 /** Create new pool */ 49 struct StrPool *strpool_create(CxMem *ca); 50 51 /** Release pool */ 52 void strpool_free(struct StrPool *sp); 53 54 /** Return either existing or new PStr for given value */ 55 struct PStr *strpool_get(struct StrPool *sp, const char *str, ssize_t len); 56 57 /** Increase reference count for existing PStr */ 58 void strpool_incref(struct PStr *str); 59 60 /** Decrease reference count for existing PStr */ 61 void strpool_decref(struct PStr *str); 62 63 /** Return count of strings in the pool */ 64 int strpool_total(struct StrPool *sp); 65 66 #endif 67