fp = ctf_get_dict (fp, type);
- idx = LCTF_TYPE_TO_INDEX(fp, type);
+ idx = ctf_type_to_index (fp, type);
if ((unsigned long) idx > fp->ctf_stypes)
return ctf_dtd_lookup (fp, type);
fp = ctf_get_dict (fp, type);
- idx = LCTF_TYPE_TO_INDEX(fp, type);
+ idx = ctf_type_to_index (fp, type);
return ((unsigned long) idx <= fp->ctf_stypes);
}
ntd = ctf_list_next (dtd);
- if (LCTF_TYPE_TO_INDEX (fp, dtd->dtd_type) <= id.dtd_id)
+ if (ctf_type_to_index (fp, dtd->dtd_type) <= id.dtd_id)
continue;
kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
if (flag != CTF_ADD_NONROOT && flag != CTF_ADD_ROOT)
return (ctf_set_typed_errno (fp, EINVAL));
- if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) >= CTF_MAX_TYPE)
+ if (ctf_index_to_type (fp, fp->ctf_typemax) >= CTF_MAX_TYPE)
return (ctf_set_typed_errno (fp, ECTF_FULL));
- if (LCTF_INDEX_TO_TYPE (fp, fp->ctf_typemax, 1) == (CTF_MAX_PTYPE - 1))
+ if (ctf_index_to_type (fp, fp->ctf_typemax) == (CTF_MAX_PTYPE - 1))
return (ctf_set_typed_errno (fp, ECTF_FULL));
if (fp->ctf_flags & LCTF_NO_STR)
dtd->dtd_vlen = NULL;
type = ++fp->ctf_typemax;
- type = LCTF_INDEX_TO_TYPE (fp, type, (fp->ctf_flags & LCTF_CHILD));
+ type = ctf_index_to_type (fp, type);
dtd->dtd_data.ctt_name = ctf_str_add_ref (fp, name, &dtd->dtd_data.ctt_name);
dtd->dtd_type = type;
addition of this type. The pptrtab is lazily-updated as needed, so is not
touched here. */
- uint32_t type_idx = LCTF_TYPE_TO_INDEX (fp, type);
- uint32_t ref_idx = LCTF_TYPE_TO_INDEX (fp, ref);
+ uint32_t type_idx = ctf_type_to_index (fp, type);
+ uint32_t ref_idx = ctf_type_to_index (fp, ref);
if (ctf_type_ischild (fp, ref) == child
&& ref_idx < fp->ctf_typemax)
if (src_fp == NULL || dst_fp == NULL)
return;
- src_type = LCTF_TYPE_TO_INDEX(src_fp, src_type);
+ src_type = ctf_type_to_index (src_fp, src_type);
- dst_type = LCTF_TYPE_TO_INDEX(dst_fp, dst_type);
+ dst_type = ctf_type_to_index (dst_fp, dst_type);
if (dst_fp->ctf_link_type_mapping == NULL)
{
if (src_fp == NULL)
return 0;
- src_type = LCTF_TYPE_TO_INDEX(src_fp, src_type);
+ src_type = ctf_type_to_index (src_fp, src_type);
key.cltk_fp = src_fp;
key.cltk_idx = src_type;
if (dst_type != 0)
{
- dst_type = LCTF_INDEX_TO_TYPE (target_fp, dst_type,
- target_fp->ctf_parent != NULL);
+ dst_type = ctf_index_to_type (target_fp, dst_type);
*dst_fp = target_fp;
return dst_type;
}
&key);
if (dst_type)
- dst_type = LCTF_INDEX_TO_TYPE (target_fp, dst_type,
- target_fp->ctf_parent != NULL);
+ dst_type = ctf_index_to_type (target_fp, dst_type);
*dst_fp = target_fp;
return dst_type;
} cu;
};
+extern uint32_t ctf_type_to_index (const ctf_dict_t *, ctf_id_t);
+extern ctf_id_t ctf_index_to_type (const ctf_dict_t *, uint32_t);
+
/* Return x rounded up to an alignment boundary.
eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align)
eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align) */
/* * If an offs is not aligned already then round it up and align it. */
#define LCTF_ALIGN_OFFS(offs, align) ((offs + (align - 1)) & ~(align - 1))
-#define LCTF_TYPE_TO_INDEX(fp, id) ((id) & (fp->ctf_parmax))
-#define LCTF_INDEX_TO_TYPE(fp, id, child) (child ? ((id) | (fp->ctf_parmax+1)) : \
- (id))
-
-#define LCTF_INDEX_TO_TYPEPTR(fp, i) \
- ((i > fp->ctf_stypes) ? \
- &(ctf_dtd_lookup (fp, LCTF_INDEX_TO_TYPE \
- (fp, i, fp->ctf_flags & LCTF_CHILD))->dtd_data) : \
- (ctf_type_t *)((uintptr_t)(fp)->ctf_buf + (fp)->ctf_txlate[(i)]))
+#define LCTF_INDEX_TO_TYPEPTR(fp, i) \
+ ((i > fp->ctf_stypes) ? \
+ &(ctf_dtd_lookup (fp, ctf_index_to_type (fp, i))->dtd_data) : \
+ (ctf_type_t *)((uintptr_t)(fp)->ctf_buf + (fp)->ctf_txlate[(i)]))
#define LCTF_INFO_KIND(fp, info) ((fp)->ctf_dictops->ctfo_get_kind(info))
#define LCTF_INFO_ISROOT(fp, info) ((fp)->ctf_dictops->ctfo_get_root(info))
uint32_t i;
for (i = fp->ctf_pptrtab_typemax; i <= fp->ctf_typemax; i++)
{
- ctf_id_t type = LCTF_INDEX_TO_TYPE (fp, i, 1);
+ ctf_id_t type = ctf_index_to_type (fp, i);
ctf_id_t reffed_type;
if (ctf_type_kind (fp, type) != CTF_K_POINTER)
if (ctf_type_isparent (fp, reffed_type))
{
- uint32_t idx = LCTF_TYPE_TO_INDEX (fp, reffed_type);
+ uint32_t idx = ctf_type_to_index (fp, reffed_type);
/* Guard against references to invalid types. No need to consider
the CTF dict corrupt in this case: this pointer just can't be a
ctf_lookup_by_name. (Pointers to types are never of type 0, so
this is unambiguous, just fiddly to deal with.) */
- uint32_t idx = LCTF_TYPE_TO_INDEX (fp, type);
+ uint32_t idx = ctf_type_to_index (fp, type);
int in_child = 0;
ntype = CTF_ERR;
if (ntype == CTF_ERR)
goto notype;
- idx = LCTF_TYPE_TO_INDEX (fp, ntype);
+ idx = ctf_type_to_index (fp, ntype);
ntype = CTF_ERR;
if (child && idx < child->ctf_pptrtab_len)
goto notype;
}
- type = LCTF_INDEX_TO_TYPE (fp, ntype, (fp->ctf_flags & LCTF_CHILD)
- || in_child);
+ if (in_child)
+ type = ctf_index_to_type (child, ntype);
+ else
+ type = ctf_index_to_type (fp, ntype);
/* We are looking up a type in the parent, but the pointed-to type is
in the child. Switch to looking in the child: if we need to go
return NULL;
}
- idx = LCTF_TYPE_TO_INDEX (fp, type);
+ idx = ctf_type_to_index (fp, type);
if (idx > 0 && (unsigned long) idx <= fp->ctf_typemax)
{
*fpp = fp; /* Possibly the parent CTF dict. */
if (((existing = ctf_dynhash_lookup_type (fp->ctf_names, name)) == 0)
|| ctf_type_encoding (fp, existing, &existing_en) != 0
- || (ctf_type_encoding (fp, LCTF_INDEX_TO_TYPE (fp, id, child), &this_en) == 0
+ || (ctf_type_encoding (fp, ctf_index_to_type (fp, id), &this_en) == 0
&& this_en.cte_offset == 0
&& (existing_en.cte_offset != 0
|| existing_en.cte_bits < this_en.cte_bits)))
{
err = ctf_dynhash_insert_type (fp, fp->ctf_names,
- LCTF_INDEX_TO_TYPE (fp, id, child),
+ ctf_index_to_type (fp, id),
tp->ctt_name);
if (err != 0)
return err * -1;
break;
err = ctf_dynhash_insert_type (fp, fp->ctf_names,
- LCTF_INDEX_TO_TYPE (fp, id, child),
+ ctf_index_to_type (fp, id),
tp->ctt_name);
if (err != 0)
return err * -1;
break;
err = ctf_dynhash_insert_type (fp, fp->ctf_structs,
- LCTF_INDEX_TO_TYPE (fp, id, child),
+ ctf_index_to_type (fp, id),
tp->ctt_name);
if (err != 0)
break;
err = ctf_dynhash_insert_type (fp, fp->ctf_unions,
- LCTF_INDEX_TO_TYPE (fp, id, child),
+ ctf_index_to_type (fp, id),
tp->ctt_name);
if (err != 0)
break;
err = ctf_dynhash_insert_type (fp, fp->ctf_enums,
- LCTF_INDEX_TO_TYPE (fp, id, child),
+ ctf_index_to_type (fp, id),
tp->ctt_name);
if (err != 0)
/* Remember all enums for later rescanning. */
err = ctf_dynset_insert (all_enums, (void *) (ptrdiff_t)
- LCTF_INDEX_TO_TYPE (fp, id, child));
+ ctf_index_to_type (fp, id));
if (err != 0)
return err * -1;
break;
break;
err = ctf_dynhash_insert_type (fp, fp->ctf_names,
- LCTF_INDEX_TO_TYPE (fp, id, child),
+ ctf_index_to_type (fp, id),
tp->ctt_name);
if (err != 0)
return err * -1;
name is not already present. */
if (ctf_dynhash_lookup_type (h, name) == 0)
{
- err = ctf_dynhash_insert_type (fp, h, LCTF_INDEX_TO_TYPE (fp, id, child),
+ err = ctf_dynhash_insert_type (fp, h, ctf_index_to_type (fp, id),
tp->ctt_name);
if (err != 0)
return err * -1;
referenced type ]. */
if (ctf_type_ischild (fp, tp->ctt_type) == child
- && LCTF_TYPE_TO_INDEX (fp, tp->ctt_type) <= fp->ctf_typemax)
- fp->ctf_ptrtab[LCTF_TYPE_TO_INDEX (fp, tp->ctt_type)] = id;
+ && ctf_type_to_index (fp, tp->ctt_type) <= fp->ctf_typemax)
+ fp->ctf_ptrtab[ctf_type_to_index (fp, tp->ctt_type)] = id;
/*FALLTHRU*/
case CTF_K_VOLATILE:
break;
err = ctf_dynhash_insert_type (fp, fp->ctf_names,
- LCTF_INDEX_TO_TYPE (fp, id, child),
+ ctf_index_to_type (fp, id),
tp->ctt_name);
if (err != 0)
return err * -1;
return (!ctf_type_isparent (fp, id));
}
+/* Get the index in the internal type array (or otherwise) for a given type ID.
+ Only ever called on the right dictionary for the type and can fail otherwise.
+ If called on an invalid type, may return an index that does not correspond to
+ any type (such as -1). */
+
+uint32_t
+ctf_type_to_index (const ctf_dict_t *fp, ctf_id_t type)
+{
+ return type & fp->ctf_parmax;
+}
+
+/* The inverse of ctf_type_to_index. */
+ctf_id_t
+ctf_index_to_type (const ctf_dict_t *fp, uint32_t idx)
+{
+ return (fp->ctf_flags & LCTF_CHILD) ? ((idx) | (fp->ctf_parmax+1)) : idx;
+}
+
/* Expand a structure element into the passed-in ctf_lmember_t. */
static int
if (flag)
*flag = LCTF_INFO_ISROOT (fp, tp->ctt_info);
- return LCTF_INDEX_TO_TYPE (fp, i->ctn_type++, fp->ctf_flags & LCTF_CHILD);
+ return ctf_index_to_type (fp, i->ctn_type++);
}
ctf_next_destroy (i);
*it = NULL;
if (ctf_lookup_by_id (&fp, type) == NULL)
return CTF_ERR; /* errno is set for us. */
- if ((ntype = fp->ctf_ptrtab[LCTF_TYPE_TO_INDEX (fp, type)]) != 0)
- return (LCTF_INDEX_TO_TYPE (fp, ntype, (fp->ctf_flags & LCTF_CHILD)));
+ if ((ntype = fp->ctf_ptrtab[ctf_type_to_index (fp, type)]) != 0)
+ return (ctf_index_to_type (fp, ntype));
if ((type = ctf_type_resolve (fp, type)) == CTF_ERR)
return (ctf_set_typed_errno (ofp, ECTF_NOTYPE));
if (ctf_lookup_by_id (&fp, type) == NULL)
return (ctf_set_typed_errno (ofp, ECTF_NOTYPE));
- if ((ntype = fp->ctf_ptrtab[LCTF_TYPE_TO_INDEX (fp, type)]) != 0)
- return (LCTF_INDEX_TO_TYPE (fp, ntype, (fp->ctf_flags & LCTF_CHILD)));
+ if ((ntype = fp->ctf_ptrtab[ctf_type_to_index (fp, type)]) != 0)
+ return (ctf_index_to_type (fp, ntype));
return (ctf_set_typed_errno (ofp, ECTF_NOTYPE));
}