cache kernelVersion(), make isAtLeastKernelVersion() cheaper

not perfect due to this being in a header file, so multiple copies
potentially exist, but it's really simple, and works nearly as well.

Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Id90c6933d57bc12f4dd640c8918fd0885c7474cf
diff --git a/staticlibs/native/bpf_headers/include/bpf/BpfUtils.h b/staticlibs/native/bpf_headers/include/bpf/BpfUtils.h
index 8f1b9a2..7801c3e 100644
--- a/staticlibs/native/bpf_headers/include/bpf/BpfUtils.h
+++ b/staticlibs/native/bpf_headers/include/bpf/BpfUtils.h
@@ -92,7 +92,7 @@
 
 #define KVER(a, b, c) (((a) << 24) + ((b) << 16) + (c))
 
-static inline unsigned kernelVersion() {
+static inline unsigned uncachedKernelVersion() {
     struct utsname buf;
     int ret = uname(&buf);
     if (ret) return 0;
@@ -108,6 +108,11 @@
     return KVER(kver_major, kver_minor, kver_sub);
 }
 
+static inline unsigned kernelVersion() {
+    static unsigned kver = uncachedKernelVersion();
+    return kver;
+}
+
 static inline bool isAtLeastKernelVersion(unsigned major, unsigned minor, unsigned sub) {
     return kernelVersion() >= KVER(major, minor, sub);
 }
diff --git a/staticlibs/native/tcutils/kernelversion.h b/staticlibs/native/tcutils/kernelversion.h
index 3be1ad2..492444a 100644
--- a/staticlibs/native/tcutils/kernelversion.h
+++ b/staticlibs/native/tcutils/kernelversion.h
@@ -32,7 +32,7 @@
 
 namespace android {
 
-static inline unsigned kernelVersion() {
+static inline unsigned uncachedKernelVersion() {
   struct utsname buf;
   int ret = uname(&buf);
   if (ret)
@@ -51,6 +51,11 @@
   return KVER(kver_major, kver_minor, kver_sub);
 }
 
+static unsigned kernelVersion() {
+  static unsigned kver = uncachedKernelVersion();
+  return kver;
+}
+
 static inline bool isAtLeastKernelVersion(unsigned major, unsigned minor,
                                           unsigned sub) {
   return kernelVersion() >= KVER(major, minor, sub);