evolution-3.6.4/e-util/e-poolv.c

No issues found

  1 /*
  2  * e-poolv.c
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) version 3.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 16  *
 17  */
 18 
 19 #ifdef HAVE_CONFIG_H
 20 #include <config.h>
 21 #endif
 22 
 23 #include "e-poolv.h"
 24 
 25 #include <string.h>
 26 #include <camel/camel.h>
 27 
 28 struct _EPoolv {
 29 	guchar length;
 30 	gchar *s[1];
 31 };
 32 
 33 static GHashTable *poolv_pool;
 34 static CamelMemPool *poolv_mempool;
 35 
 36 G_LOCK_DEFINE_STATIC (poolv);
 37 
 38 /**
 39  * e_poolv_new:
 40  * @size: The number of elements in the poolv, maximum of 254 elements.
 41  *
 42  * Create a new #EPoolv: a string vector which shares a global string
 43  * pool.  An #EPoolv can be used to work with arrays of strings which
 44  * save memory by eliminating duplicated allocations of the same string.
 45  *
 46  * This is useful when you have a log of read-only strings that do not
 47  * go away and are duplicated a lot, such as email headers.
 48  *
 49  * Returns: a new #EPoolv
 50  **/
 51 EPoolv *
 52 e_poolv_new (guint size)
 53 {
 54 	EPoolv *poolv;
 55 
 56 	g_return_val_if_fail (size < 255, NULL);
 57 
 58 	poolv = g_malloc0 (sizeof (*poolv) + (size - 1) * sizeof (gchar *));
 59 	poolv->length = size;
 60 
 61 	G_LOCK (poolv);
 62 
 63 	if (!poolv_pool)
 64 		poolv_pool = g_hash_table_new (g_str_hash, g_str_equal);
 65 
 66 	if (!poolv_mempool)
 67 		poolv_mempool = camel_mempool_new (
 68 			32 * 1024, 512, CAMEL_MEMPOOL_ALIGN_BYTE);
 69 
 70 	G_UNLOCK (poolv);
 71 
 72 	return poolv;
 73 }
 74 
 75 /**
 76  * e_poolv_set:
 77  * @poolv: pooled string vector
 78  * @index: index in vector of string
 79  * @str: string to set
 80  * @freeit: whether the caller is releasing its reference to the
 81  * string
 82  *
 83  * Set a string vector reference.  If the caller will no longer be
 84  * referencing the string, freeit should be TRUE.  Otherwise, this
 85  * will duplicate the string if it is not found in the pool.
 86  *
 87  * Returns: @poolv
 88  **/
 89 EPoolv *
 90 e_poolv_set (EPoolv *poolv,
 91              gint index,
 92              gchar *str,
 93              gint freeit)
 94 {
 95 	g_return_val_if_fail (poolv != NULL, NULL);
 96 	g_return_val_if_fail (index >= 0 && index < poolv->length, NULL);
 97 
 98 	if (!str) {
 99 		poolv->s[index] = NULL;
100 		return poolv;
101 	}
102 
103 	G_LOCK (poolv);
104 
105 	if ((poolv->s[index] = g_hash_table_lookup (poolv_pool, str)) != NULL) {
106 	} else {
107 		poolv->s[index] = camel_mempool_strdup (poolv_mempool, str);
108 		g_hash_table_insert (poolv_pool, poolv->s[index], poolv->s[index]);
109 	}
110 
111 	G_UNLOCK (poolv);
112 
113 	if (freeit)
114 		g_free (str);
115 
116 	return poolv;
117 }
118 
119 /**
120  * e_poolv_get:
121  * @poolv: pooled string vector
122  * @index: index in vector of string
123  *
124  * Retrieve a string by index.  This could possibly just be a macro.
125  *
126  * Since the pool is never freed, this string does not need to be
127  * duplicated, but should not be modified.
128  *
129  * Returns: string at that index.
130  **/
131 const gchar *
132 e_poolv_get (EPoolv *poolv,
133              gint index)
134 {
135 	g_return_val_if_fail (poolv != NULL, NULL);
136 	g_return_val_if_fail (index >= 0 && index < poolv->length, NULL);
137 
138 	return poolv->s[index] ? poolv->s[index] : "";
139 }
140 
141 /**
142  * e_poolv_destroy:
143  * @poolv: pooled string vector to free
144  *
145  * Free a pooled string vector.  This doesn't free the strings from
146  * the vector, however.
147  **/
148 void
149 e_poolv_destroy (EPoolv *poolv)
150 {
151 	g_return_if_fail (poolv != NULL);
152 
153 	g_free (poolv);
154 }