8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / common / ctf / ctf_util.c
blob740d403e8c523c4d74e7786f01122f29939c3da6
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <ctf_impl.h>
32 * Simple doubly-linked list append routine. This implementation assumes that
33 * each list element contains an embedded ctf_list_t as the first member.
34 * An additional ctf_list_t is used to store the head (l_next) and tail
35 * (l_prev) pointers. The current head and tail list elements have their
36 * previous and next pointers set to NULL, respectively.
38 void
39 ctf_list_append(ctf_list_t *lp, void *new)
41 ctf_list_t *p = lp->l_prev; /* p = tail list element */
42 ctf_list_t *q = new; /* q = new list element */
44 lp->l_prev = q;
45 q->l_prev = p;
46 q->l_next = NULL;
48 if (p != NULL)
49 p->l_next = q;
50 else
51 lp->l_next = q;
55 * Prepend the specified existing element to the given ctf_list_t. The
56 * existing pointer should be pointing at a struct with embedded ctf_list_t.
58 void
59 ctf_list_prepend(ctf_list_t *lp, void *new)
61 ctf_list_t *p = new; /* p = new list element */
62 ctf_list_t *q = lp->l_next; /* q = head list element */
64 lp->l_next = p;
65 p->l_prev = NULL;
66 p->l_next = q;
68 if (q != NULL)
69 q->l_prev = p;
70 else
71 lp->l_prev = p;
75 * Delete the specified existing element from the given ctf_list_t. The
76 * existing pointer should be pointing at a struct with embedded ctf_list_t.
78 void
79 ctf_list_delete(ctf_list_t *lp, void *existing)
81 ctf_list_t *p = existing;
83 if (p->l_prev != NULL)
84 p->l_prev->l_next = p->l_next;
85 else
86 lp->l_next = p->l_next;
88 if (p->l_next != NULL)
89 p->l_next->l_prev = p->l_prev;
90 else
91 lp->l_prev = p->l_prev;
95 * Convert an encoded CTF string name into a pointer to a C string by looking
96 * up the appropriate string table buffer and then adding the offset.
98 const char *
99 ctf_strraw(ctf_file_t *fp, uint_t name)
101 ctf_strs_t *ctsp = &fp->ctf_str[CTF_NAME_STID(name)];
103 if (ctsp->cts_strs != NULL && CTF_NAME_OFFSET(name) < ctsp->cts_len)
104 return (ctsp->cts_strs + CTF_NAME_OFFSET(name));
106 /* string table not loaded or corrupt offset */
107 return (NULL);
110 const char *
111 ctf_strptr(ctf_file_t *fp, uint_t name)
113 const char *s = ctf_strraw(fp, name);
114 return (s != NULL ? s : "(?)");
118 * Same strdup(3C), but use ctf_alloc() to do the memory allocation.
120 char *
121 ctf_strdup(const char *s1)
123 char *s2 = ctf_alloc(strlen(s1) + 1);
125 if (s2 != NULL)
126 (void) strcpy(s2, s1);
128 return (s2);
132 * Store the specified error code into errp if it is non-NULL, and then
133 * return NULL for the benefit of the caller.
135 ctf_file_t *
136 ctf_set_open_errno(int *errp, int error)
138 if (errp != NULL)
139 *errp = error;
140 return (NULL);
144 * Store the specified error code into the CTF container, and then return
145 * CTF_ERR for the benefit of the caller.
147 long
148 ctf_set_errno(ctf_file_t *fp, int err)
150 fp->ctf_errno = err;
151 return (CTF_ERR);