]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lscpu: add find_implementer() for ARM implementer lookup
authorKarel Zak <kzak@redhat.com>
Thu, 28 May 2026 10:01:01 +0000 (12:01 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 28 May 2026 10:01:01 +0000 (12:01 +0200)
Refactor ARM implementer lookup into a dedicated find_implementer()
function and use it in is_arm() and arm_ids_decode() to simplify
the code and avoid open-coded linear searches.

Addresses: https://github.com/util-linux/util-linux/pull/4362
Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/lscpu-arm.c

index ed255a3c791a14b14eec07d6fa6e4c733b498a73..ca12893a152245593b60ea6eae981551d53bb291 100644 (file)
@@ -368,6 +368,16 @@ static inline int parse_implementer_id(struct lscpu_cputype *ct)
        return ct->vendor_id;
 }
 
+static const struct hw_impl *find_implementer(int id)
+{
+       int j;
+
+       for (j = 0; hw_implementer[j].id != -1; j++)
+               if (hw_implementer[j].id == id)
+                       return &hw_implementer[j];
+       return NULL;
+}
+
 int is_arm(struct lscpu_cxt *cxt)
 {
        size_t i;
@@ -378,13 +388,9 @@ int is_arm(struct lscpu_cxt *cxt)
        /* dump; assume ARM if vendor ID is known */
        for (i = 0; i < cxt->ncputypes; i++) {
 
-               int j, id = get_implementer_id(cxt->cputypes[i]);
-               if (id <= 0)
-                       continue;
-               for (j = 0; hw_implementer[j].id != -1; j++) {
-                       if (hw_implementer[j].id == id)
-                               return 1;
-               }
+               int id = get_implementer_id(cxt->cputypes[i]);
+               if (id > 0 && find_implementer(id))
+                       return 1;
        }
 
        return 0;
@@ -396,38 +402,33 @@ int is_arm(struct lscpu_cxt *cxt)
 static int arm_ids_decode(struct lscpu_cputype *ct)
 {
        int impl, part, j;
-       const struct id_part *parts = NULL;
+       const struct hw_impl *hw;
 
        impl = parse_implementer_id(ct);
        if (impl <= 0)
                return -EINVAL; /* no ARM or missing ID */
 
+       hw = find_implementer(impl);
+       if (!hw)
+               return 0;
+
        /* decode vendor */
-       for (j = 0; hw_implementer[j].id != -1; j++) {
-               if (hw_implementer[j].id == impl) {
-                       parts = hw_implementer[j].parts;
-                       free(ct->vendor);
-                       ct->vendor = xstrdup(hw_implementer[j].name);
-                       break;
-               }
-       }
+       free(ct->vendor);
+       ct->vendor = xstrdup(hw->name);
 
        /* decode model */
-       if (!parts)
-               goto done;
-
        part = parse_model_id(ct);
        if (part <= 0)
-               goto done;
+               return 0;
 
-       for (j = 0; parts[j].id != -1; j++) {
-               if (parts[j].id == part) {
+       for (j = 0; hw->parts[j].id != -1; j++) {
+               if (hw->parts[j].id == part) {
                        free(ct->modelname);
-                       ct->modelname = xstrdup(parts[j].name);
+                       ct->modelname = xstrdup(hw->parts[j].name);
                        break;
                }
        }
-done:
+
        return 0;
 }