Submenu support: development snapshot
[syslinux.git] / com32 / menu / refstr.c
blob745bc9227e541405acf5b54dfd4c37d451361704
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 2008 H. Peter Anvin - All Rights Reserved
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, Inc., 51 Franklin St, Fifth Floor,
8 * Boston MA 02110-1301, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
14 * refstr.c
16 * Simple reference-counted strings
19 #include <stdlib.h>
20 #include <string.h>
21 #include "refstr.h"
23 const char *refstr_mkn(const char *str, size_t len)
25 char *r;
27 len = strnlen(str, len);
28 r = malloc(sizeof(unsigned int)+len+1);
29 *(unsigned int *)r = 1;
30 r += sizeof(unsigned int);
31 memcpy(r, str, len);
32 r[len] = '\0';
33 return r;
36 const char *refstr_mk(const char *str)
38 refstr *r;
39 size_t len;
41 len = strlen(str);
42 r = malloc(sizeof(unsigned int)+len+1);
43 *(unsigned int *)r = 1;
44 r += sizeof(unsigned int);
45 memcpy(r, str, len);
46 r[len] = '\0';
47 return r;
50 void refstr_put(const char *r)
52 unsigned int *ref = (unsigned int *)r - 1;
54 if (!--*ref)
55 free(ref);