1 /*
2  * Copyright (C) 2009 Aliaksey Kandratsenka
3  *
4  * This program is free software: you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 3 of the
7  * License, or (at your option) any later version.
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  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see
16  * `http://www.gnu.org/licenses/'.
17  */
18 #include <string.h>
19 #include <stdlib.h>
20 #include <glib.h>
21 #include "xmalloc.h"
22 
23 #include "refcounted_str.h"
24 
refcounted_str_dup(char * str)25 struct refcounted_str *refcounted_str_dup(char *str)
26 {
27 	struct refcounted_str *rv = xmalloc(sizeof(struct refcounted_str));
28 	str = xstrdup(str);
29 
30 	rv->refcnt = 1;
31 	rv->str = str;
32 	return rv;
33 }
34 
refcounted_str_get(struct refcounted_str ** ptr,struct refcounted_str * src)35 void refcounted_str_get(struct refcounted_str **ptr, struct refcounted_str *src) {
36 	if (src)
37 		src->refcnt++;
38 	*ptr = src;
39 }
40 
refcounted_str_put(struct refcounted_str ** ptr)41 void refcounted_str_put(struct refcounted_str **ptr) {
42 	struct refcounted_str *rstr = *ptr;
43 	*ptr = 0;
44 	if (rstr)
45 		if (--rstr->refcnt == 0) {
46 			free(rstr->str);
47 			free(rstr);
48 		}
49 }
50