1 /* Miscellaneous utilities.
2 Copyright (C) 2019 Free Software Foundation, Inc.
4 This file is part of libctf.
6 libctf is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not see
18 <http://www.gnu.org/licenses/>. */
23 /* Simple doubly-linked list append routine. This implementation assumes that
24 each list element contains an embedded ctf_list_t as the first member.
25 An additional ctf_list_t is used to store the head (l_next) and tail
26 (l_prev) pointers. The current head and tail list elements have their
27 previous and next pointers set to NULL, respectively. */
30 ctf_list_append (ctf_list_t *lp, void *newp)
32 ctf_list_t *p = lp->l_prev; /* p = tail list element. */
33 ctf_list_t *q = newp; /* q = new list element. */
45 /* Prepend the specified existing element to the given ctf_list_t. The
46 existing pointer should be pointing at a struct with embedded ctf_list_t. */
49 ctf_list_prepend (ctf_list_t * lp, void *newp)
51 ctf_list_t *p = newp; /* p = new list element. */
52 ctf_list_t *q = lp->l_next; /* q = head list element. */
64 /* Delete the specified existing element from the given ctf_list_t. The
65 existing pointer should be pointing at a struct with embedded ctf_list_t. */
68 ctf_list_delete (ctf_list_t *lp, void *existing)
70 ctf_list_t *p = existing;
72 if (p->l_prev != NULL)
73 p->l_prev->l_next = p->l_next;
75 lp->l_next = p->l_next;
77 if (p->l_next != NULL)
78 p->l_next->l_prev = p->l_prev;
80 lp->l_prev = p->l_prev;
83 /* Convert a 32-bit ELF symbol into Elf64 and return a pointer to it. */
86 ctf_sym_to_elf64 (const Elf32_Sym *src, Elf64_Sym *dst)
88 dst->st_name = src->st_name;
89 dst->st_value = src->st_value;
90 dst->st_size = src->st_size;
91 dst->st_info = src->st_info;
92 dst->st_other = src->st_other;
93 dst->st_shndx = src->st_shndx;
98 /* Same as strdup(3C), but use ctf_alloc() to do the memory allocation. */
100 _libctf_malloc_ char *
101 ctf_strdup (const char *s1)
103 char *s2 = ctf_alloc (strlen (s1) + 1);
106 (void) strcpy (s2, s1);
111 /* A string appender working on dynamic strings. */
114 ctf_str_append (char *s, const char *append)
124 size_t append_len = strlen (append);
126 if ((s = realloc (s, s_len + append_len + 1)) == NULL)
129 memcpy (s + s_len, append, append_len);
130 s[s_len + append_len] = '\0';
135 /* A realloc() that fails noisily if called with any ctf_str_num_users. */
137 ctf_realloc (ctf_file_t *fp, void *ptr, size_t size)
139 if (fp->ctf_str_num_refs > 0)
141 ctf_dprintf ("%p: attempt to realloc() string table with %lu active refs\n",
142 (void *) fp, (unsigned long) fp->ctf_str_num_refs);
145 return realloc (ptr, size);
148 /* Store the specified error code into errp if it is non-NULL, and then
149 return NULL for the benefit of the caller. */
152 ctf_set_open_errno (int *errp, int error)
159 /* Store the specified error code into the CTF container, and then return
160 CTF_ERR / -1 for the benefit of the caller. */
163 ctf_set_errno (ctf_file_t * fp, int err)