Fri, 05 Dec 2003 16:56:03 +0000
[gaim-migrate @ 8408]
This is an implementation of immutable, reference-counted strings. It
is exceedingly trivial, I have no idea why I didn't do this long
ago... PLEASE use these anywhere you pass around strings that may be
stuck in multiple places; I'm not sure that it will save us too much
heap holistically, but it should prevent having to make a hojillion
twenty-byte allocations and free them immediately.
| 7763 | 1 | /** |
| 2 | * @file stringref.c Reference-counted strings | |
| 3 | * @ingroup core | |
| 4 | * | |
| 5 | * gaim | |
| 6 | * | |
| 7 | * Copyright (C) 2003 Ethan Blanton <elb@elitists.net> | |
| 8 | * | |
| 9 | * This program is free software; you can redistribute it and/or modify | |
| 10 | * it under the terms of the GNU General Public License as published by | |
| 11 | * the Free Software Foundation; either version 2 of the License, or | |
| 12 | * (at your option) any later version. | |
| 13 | * | |
| 14 | * This program is distributed in the hope that it will be useful, | |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | * GNU General Public License for more details. | |
| 18 | * | |
| 19 | * You should have received a copy of the GNU General Public License | |
| 20 | * along with this program; if not, write to the Free Software | |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 22 | * | |
| 23 | */ | |
| 24 | ||
| 25 | #include "internal.h" | |
| 26 | ||
| 27 | #include <string.h> | |
| 28 | ||
| 29 | #include "stringref.h" | |
| 30 | ||
| 31 | GaimStringref *gaim_stringref_new(const char *value) | |
| 32 | { | |
| 33 | GaimStringref *newref; | |
| 34 | ||
| 35 | newref = g_malloc(sizeof(GaimStringref) + strlen(value) + 1); | |
| 36 | strcpy(newref->value, value); | |
| 37 | newref->ref = 1; | |
| 38 | ||
| 39 | return newref; | |
| 40 | } | |
| 41 | ||
| 42 | GaimStringref *gaim_stringref_ref(GaimStringref *stringref) | |
| 43 | { | |
| 44 | if (stringref == NULL) | |
| 45 | return NULL; | |
| 46 | stringref->ref++; | |
| 47 | return stringref; | |
| 48 | } | |
| 49 | ||
| 50 | void gaim_stringref_unref(GaimStringref *stringref) | |
| 51 | { | |
| 52 | g_return_if_fail(stringref != NULL); | |
| 53 | if (--stringref->ref == 0) | |
| 54 | g_free(stringref); | |
| 55 | } | |
| 56 | ||
| 57 | const char *gaim_stringref_value(GaimStringref *stringref) | |
| 58 | { | |
| 59 | return (stringref == NULL ? NULL : stringref->value); | |
| 60 | } |