From 534444b1ee161f342d303fd6549310963ce233c9 Mon Sep 17 00:00:00 2001 From: Nick Alcock Date: Thu, 18 Mar 2021 12:37:52 +0000 Subject: [PATCH] libctf: eliminate dtd_u, part 2: arrays This is even simpler than ints, floats and slices, with the only extra complication being the need to manually transfer the array parameter in the rarely-used function ctf_set_array. (Arrays are unique in libctf in that they can be modified post facto, not just created and appended to. I'm not sure why they got this exemption, but it's easy to maintain.) libctf/ChangeLog 2021-03-18 Nick Alcock * ctf-impl.h (ctf_dtdef_t) : Remove. * ctf-create.c (ctf_add_array): Use the dtd_vlen, not dtu_arr. (ctf_set_array): Likewise. * ctf-serialize.c (ctf_emit_type_sect): Just copy the dtd_vlen. * ctf-types.c (ctf_array_info): Just use the vlen. --- libctf/ChangeLog | 8 ++++++++ libctf/ctf-create.c | 16 +++++++++++++--- libctf/ctf-impl.h | 1 - libctf/ctf-serialize.c | 8 ++------ libctf/ctf-types.c | 10 ++++------ 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/libctf/ChangeLog b/libctf/ChangeLog index bf138399a39..25a32d2a658 100644 --- a/libctf/ChangeLog +++ b/libctf/ChangeLog @@ -1,3 +1,11 @@ +2021-03-18 Nick Alcock + + * ctf-impl.h (ctf_dtdef_t) : Remove. + * ctf-create.c (ctf_add_array): Use the dtd_vlen, not dtu_arr. + (ctf_set_array): Likewise. + * ctf-serialize.c (ctf_emit_type_sect): Just copy the dtd_vlen. + * ctf-types.c (ctf_array_info): Just use the vlen. + 2021-03-18 Nick Alcock * ctf-impl.h (ctf_dtdef_t) : Remove. diff --git a/libctf/ctf-create.c b/libctf/ctf-create.c index 90db7121af6..bc46cfa6ca8 100644 --- a/libctf/ctf-create.c +++ b/libctf/ctf-create.c @@ -635,6 +635,7 @@ ctf_id_t ctf_add_array (ctf_dict_t *fp, uint32_t flag, const ctf_arinfo_t *arp) { ctf_dtdef_t *dtd; + ctf_array_t cta; ctf_id_t type; ctf_dict_t *tmp = fp; @@ -658,12 +659,17 @@ ctf_add_array (ctf_dict_t *fp, uint32_t flag, const ctf_arinfo_t *arp) } if ((type = ctf_add_generic (fp, flag, NULL, CTF_K_ARRAY, - 0, &dtd)) == CTF_ERR) + sizeof (ctf_array_t), &dtd)) == CTF_ERR) return CTF_ERR; /* errno is set for us. */ + memset (&cta, 0, sizeof (ctf_array_t)); + dtd->dtd_data.ctt_info = CTF_TYPE_INFO (CTF_K_ARRAY, flag, 0); dtd->dtd_data.ctt_size = 0; - dtd->dtd_u.dtu_arr = *arp; + cta.cta_contents = (uint32_t) arp->ctr_contents; + cta.cta_index = (uint32_t) arp->ctr_index; + cta.cta_nelems = arp->ctr_nelems; + memcpy (dtd->dtd_vlen, &cta, sizeof (ctf_array_t)); return type; } @@ -672,6 +678,7 @@ int ctf_set_array (ctf_dict_t *fp, ctf_id_t type, const ctf_arinfo_t *arp) { ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, type); + ctf_array_t *vlen; if (!(fp->ctf_flags & LCTF_RDWR)) return (ctf_set_errno (fp, ECTF_RDONLY)); @@ -680,8 +687,11 @@ ctf_set_array (ctf_dict_t *fp, ctf_id_t type, const ctf_arinfo_t *arp) || LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info) != CTF_K_ARRAY) return (ctf_set_errno (fp, ECTF_BADID)); + vlen = (ctf_array_t *) dtd->dtd_vlen; fp->ctf_flags |= LCTF_DIRTY; - dtd->dtd_u.dtu_arr = *arp; + vlen->cta_contents = (uint32_t) arp->ctr_contents; + vlen->cta_index = (uint32_t) arp->ctr_index; + vlen->cta_nelems = arp->ctr_nelems; return 0; } diff --git a/libctf/ctf-impl.h b/libctf/ctf-impl.h index 742b4b37aff..c1ce50bc3b9 100644 --- a/libctf/ctf-impl.h +++ b/libctf/ctf-impl.h @@ -196,7 +196,6 @@ typedef struct ctf_dtdef union { ctf_list_t dtu_members; /* struct, union, or enum */ - ctf_arinfo_t dtu_arr; /* array */ uint32_t *dtu_argv; /* function */ } dtd_u; } ctf_dtdef_t; diff --git a/libctf/ctf-serialize.c b/libctf/ctf-serialize.c index f07cb61c42a..d8e78f361f4 100644 --- a/libctf/ctf-serialize.c +++ b/libctf/ctf-serialize.c @@ -857,7 +857,6 @@ ctf_emit_type_sect (ctf_dict_t *fp, unsigned char **tptr) uint32_t kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info); uint32_t vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info); - ctf_array_t cta; size_t len; ctf_stype_t *copied; const char *name; @@ -888,11 +887,8 @@ ctf_emit_type_sect (ctf_dict_t *fp, unsigned char **tptr) break; case CTF_K_ARRAY: - cta.cta_contents = (uint32_t) dtd->dtd_u.dtu_arr.ctr_contents; - cta.cta_index = (uint32_t) dtd->dtd_u.dtu_arr.ctr_index; - cta.cta_nelems = dtd->dtd_u.dtu_arr.ctr_nelems; - memcpy (t, &cta, sizeof (cta)); - t += sizeof (cta); + memcpy (t, dtd->dtd_vlen, sizeof (struct ctf_array)); + t += sizeof (struct ctf_array); break; case CTF_K_FUNCTION: diff --git a/libctf/ctf-types.c b/libctf/ctf-types.c index ae243817713..8c983d5542f 100644 --- a/libctf/ctf-types.c +++ b/libctf/ctf-types.c @@ -1520,14 +1520,12 @@ ctf_array_info (ctf_dict_t *fp, ctf_id_t type, ctf_arinfo_t *arp) return (ctf_set_errno (ofp, ECTF_NOTARRAY)); if ((dtd = ctf_dynamic_type (ofp, type)) != NULL) + ap = (const ctf_array_t *) dtd->dtd_vlen; + else { - *arp = dtd->dtd_u.dtu_arr; - return 0; + ctf_get_ctt_size (fp, tp, NULL, &increment); + ap = (const ctf_array_t *) ((uintptr_t) tp + increment); } - - (void) ctf_get_ctt_size (fp, tp, NULL, &increment); - - ap = (const ctf_array_t *) ((uintptr_t) tp + increment); arp->ctr_contents = ap->cta_contents; arp->ctr_index = ap->cta_index; arp->ctr_nelems = ap->cta_nelems; -- 2.30.2