aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-poolv.c
blob: ca95286f69eef36b07e7b400cdfb973b5fffac21 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * e-poolv.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-poolv.h"

#include <string.h>
#include <camel/camel.h>

struct _EPoolv {
    guchar length;
    gchar *s[1];
};

static GHashTable *poolv_pool;
static CamelMemPool *poolv_mempool;

G_LOCK_DEFINE_STATIC (poolv);

/**
 * e_poolv_new:
 * @size: The number of elements in the poolv, maximum of 254 elements.
 *
 * Create a new #EPoolv: a string vector which shares a global string
 * pool.  An #EPoolv can be used to work with arrays of strings which
 * save memory by eliminating duplicated allocations of the same string.
 *
 * This is useful when you have a log of read-only strings that do not
 * go away and are duplicated a lot, such as email headers.
 *
 * Returns: a new #EPoolv
 **/
EPoolv *
e_poolv_new (guint size)
{
    EPoolv *poolv;

    g_return_val_if_fail (size < 255, NULL);

    poolv = g_malloc0 (sizeof (*poolv) + (size - 1) * sizeof (gchar *));
    poolv->length = size;

    G_LOCK (poolv);

    if (!poolv_pool)
        poolv_pool = g_hash_table_new (g_str_hash, g_str_equal);

    if (!poolv_mempool)
        poolv_mempool = camel_mempool_new (
            32 * 1024, 512, CAMEL_MEMPOOL_ALIGN_BYTE);

    G_UNLOCK (poolv);

    return poolv;
}

/**
 * e_poolv_set:
 * @poolv: pooled string vector
 * @index: index in vector of string
 * @str: string to set
 * @freeit: whether the caller is releasing its reference to the
 * string
 *
 * Set a string vector reference.  If the caller will no longer be
 * referencing the string, freeit should be TRUE.  Otherwise, this
 * will duplicate the string if it is not found in the pool.
 *
 * Returns: @poolv
 **/
EPoolv *
e_poolv_set (EPoolv *poolv,
             gint index,
             gchar *str,
             gint freeit)
{
    g_return_val_if_fail (poolv != NULL, NULL);
    g_return_val_if_fail (index >= 0 && index < poolv->length, NULL);

    if (!str) {
        poolv->s[index] = NULL;
        return poolv;
    }

    G_LOCK (poolv);

    if ((poolv->s[index] = g_hash_table_lookup (poolv_pool, str)) != NULL) {
    } else {
        poolv->s[index] = camel_mempool_strdup (poolv_mempool, str);
        g_hash_table_insert (poolv_pool, poolv->s[index], poolv->s[index]);
    }

    G_UNLOCK (poolv);

    if (freeit)
        g_free (str);

    return poolv;
}

/**
 * e_poolv_get:
 * @poolv: pooled string vector
 * @index: index in vector of string
 *
 * Retrieve a string by index.  This could possibly just be a macro.
 *
 * Since the pool is never freed, this string does not need to be
 * duplicated, but should not be modified.
 *
 * Returns: string at that index.
 **/
const gchar *
e_poolv_get (EPoolv *poolv,
             gint index)
{
    g_return_val_if_fail (poolv != NULL, NULL);
    g_return_val_if_fail (index >= 0 && index < poolv->length, NULL);

    return poolv->s[index] ? poolv->s[index] : "";
}

/**
 * e_poolv_destroy:
 * @poolv: pooled string vector to free
 *
 * Free a pooled string vector.  This doesn't free the strings from
 * the vector, however.
 **/
void
e_poolv_destroy (EPoolv *poolv)
{
    g_return_if_fail (poolv != NULL);

    g_free (poolv);
}