| 1 |
/* sysprof-strings.c |
| 2 |
* |
| 3 |
* Copyright 2023 Christian Hergert <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include "sysprof-strings-private.h" |
| 24 |
|
| 25 |
struct _SysprofStrings |
| 26 |
{ |
| 27 |
GMutex mutex; |
| 28 |
GHashTable *hashtable; |
| 29 |
}; |
| 30 |
|
| 31 |
SysprofStrings * |
| 32 |
sysprof_strings_new (void) |
| 33 |
{ |
| 34 |
SysprofStrings *self; |
| 35 |
|
| 36 |
self = g_atomic_rc_box_new0 (SysprofStrings); |
| 37 |
g_mutex_init (&self->mutex); |
| 38 |
self->hashtable = g_hash_table_new_full (g_str_hash, |
| 39 |
g_str_equal, |
| 40 |
(GDestroyNotify)g_ref_string_release, |
| 41 |
NULL); |
| 42 |
|
| 43 |
return self; |
| 44 |
} |
| 45 |
|
| 46 |
SysprofStrings * |
| 47 |
sysprof_strings_ref (SysprofStrings *self) |
| 48 |
{ |
| 49 |
return g_atomic_rc_box_acquire (self); |
| 50 |
} |
| 51 |
|
| 52 |
static void |
| 53 |
sysprof_strings_finalize (gpointer data) |
| 54 |
{ |
| 55 |
SysprofStrings *self = data; |
| 56 |
|
| 57 |
g_mutex_clear (&self->mutex); |
| 58 |
g_clear_pointer (&self->hashtable, g_hash_table_unref); |
| 59 |
} |
| 60 |
|
| 61 |
void |
| 62 |
sysprof_strings_unref (SysprofStrings *self) |
| 63 |
{ |
| 64 |
g_atomic_rc_box_release_full (self, sysprof_strings_finalize); |
| 65 |
} |
| 66 |
|
| 67 |
GRefString * |
| 68 |
sysprof_strings_get (SysprofStrings *self, |
| 69 |
const char *string) |
| 70 |
{ |
| 71 |
GRefString *ret; |
| 72 |
|
| 73 |
if (string == NULL) |
| 74 |
return NULL; |
| 75 |
|
| 76 |
g_mutex_lock (&self->mutex); |
| 77 |
if (!(ret = g_hash_table_lookup (self->hashtable, string))) |
| 78 |
{ |
| 79 |
ret = g_ref_string_new (string); |
| 80 |
g_hash_table_insert (self->hashtable, ret, ret); |
| 81 |
} |
| 82 |
g_ref_string_acquire (ret); |
| 83 |
g_mutex_unlock (&self->mutex); |
| 84 |
|
| 85 |
return ret; |
| 86 |
} |