~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/util/fncache.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /* Manage a cache of file names' existence */
  3 #include <stdlib.h>
  4 #include <unistd.h>
  5 #include <string.h>
  6 #include <linux/list.h>
  7 #include "fncache.h"
  8 
  9 struct fncache {
 10         struct hlist_node nd;
 11         bool res;
 12         char name[];
 13 };
 14 
 15 #define FNHSIZE 61
 16 
 17 static struct hlist_head fncache_hash[FNHSIZE];
 18 
 19 unsigned shash(const unsigned char *s)
 20 {
 21         unsigned h = 0;
 22         while (*s)
 23                 h = 65599 * h + *s++;
 24         return h ^ (h >> 16);
 25 }
 26 
 27 static bool lookup_fncache(const char *name, bool *res)
 28 {
 29         int h = shash((const unsigned char *)name) % FNHSIZE;
 30         struct fncache *n;
 31 
 32         hlist_for_each_entry(n, &fncache_hash[h], nd) {
 33                 if (!strcmp(n->name, name)) {
 34                         *res = n->res;
 35                         return true;
 36                 }
 37         }
 38         return false;
 39 }
 40 
 41 static void update_fncache(const char *name, bool res)
 42 {
 43         struct fncache *n = malloc(sizeof(struct fncache) + strlen(name) + 1);
 44         int h = shash((const unsigned char *)name) % FNHSIZE;
 45 
 46         if (!n)
 47                 return;
 48         strcpy(n->name, name);
 49         n->res = res;
 50         hlist_add_head(&n->nd, &fncache_hash[h]);
 51 }
 52 
 53 /* No LRU, only use when bounded in some other way. */
 54 bool file_available(const char *name)
 55 {
 56         bool res;
 57 
 58         if (lookup_fncache(name, &res))
 59                 return res;
 60         res = access(name, R_OK) == 0;
 61         update_fncache(name, res);
 62         return res;
 63 }
 64 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php