libbpf: Refactor btf__add_enum() for future code sharing
authorYonghong Song <yhs@fb.com>
Tue, 7 Jun 2022 06:26:15 +0000 (23:26 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 7 Jun 2022 17:20:42 +0000 (10:20 -0700)
Refactor btf__add_enum() function to create a separate
function btf_add_enum_common() so later the common function
can be used to add enum64 btf type. There is no functionality
change for this patch.

Acked-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/r/20220607062615.3718063-1-yhs@fb.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/lib/bpf/btf.c

index 2e9c23bcdc677dba808940ebedfb474cb094b968..1972d0c65e0c59d5a50e85fdd153c1bbeda0f292 100644 (file)
@@ -2129,20 +2129,8 @@ int btf__add_field(struct btf *btf, const char *name, int type_id,
        return 0;
 }
 
-/*
- * Append new BTF_KIND_ENUM type with:
- *   - *name* - name of the enum, can be NULL or empty for anonymous enums;
- *   - *byte_sz* - size of the enum, in bytes.
- *
- * Enum initially has no enum values in it (and corresponds to enum forward
- * declaration). Enumerator values can be added by btf__add_enum_value()
- * immediately after btf__add_enum() succeeds.
- *
- * Returns:
- *   - >0, type ID of newly added BTF type;
- *   - <0, on error.
- */
-int btf__add_enum(struct btf *btf, const char *name, __u32 byte_sz)
+static int btf_add_enum_common(struct btf *btf, const char *name, __u32 byte_sz,
+                              bool is_signed, __u8 kind)
 {
        struct btf_type *t;
        int sz, name_off = 0;
@@ -2167,12 +2155,30 @@ int btf__add_enum(struct btf *btf, const char *name, __u32 byte_sz)
 
        /* start out with vlen=0; it will be adjusted when adding enum values */
        t->name_off = name_off;
-       t->info = btf_type_info(BTF_KIND_ENUM, 0, 0);
+       t->info = btf_type_info(kind, 0, is_signed);
        t->size = byte_sz;
 
        return btf_commit_type(btf, sz);
 }
 
+/*
+ * Append new BTF_KIND_ENUM type with:
+ *   - *name* - name of the enum, can be NULL or empty for anonymous enums;
+ *   - *byte_sz* - size of the enum, in bytes.
+ *
+ * Enum initially has no enum values in it (and corresponds to enum forward
+ * declaration). Enumerator values can be added by btf__add_enum_value()
+ * immediately after btf__add_enum() succeeds.
+ *
+ * Returns:
+ *   - >0, type ID of newly added BTF type;
+ *   - <0, on error.
+ */
+int btf__add_enum(struct btf *btf, const char *name, __u32 byte_sz)
+{
+       return btf_add_enum_common(btf, name, byte_sz, false, BTF_KIND_ENUM);
+}
+
 /*
  * Append new enum value for the current ENUM type with:
  *   - *name* - name of the enumerator value, can't be NULL or empty;