tools/perf/util/srccode.c
Source file repositories/reference/linux-study-clean/tools/perf/util/srccode.c
File Facts
- System
- Linux kernel
- Corpus path
tools/perf/util/srccode.c- Extension
.c- Size
- 3493 bytes
- Lines
- 172
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/list.hlinux/zalloc.hstdlib.hsys/mman.hsys/stat.hfcntl.hunistd.hassert.hstring.hsrccode.hdebug.hinternal/lib.hhashmap.h
Detected Declarations
struct srcfilefunction countlinesfunction fill_linesfunction free_srcfilefunction hlist_for_each_entry
Annotated Snippet
struct srcfile {
struct hlist_node hash_nd;
struct list_head nd;
char *fn;
char **lines;
char *map;
unsigned numlines;
size_t maplen;
};
static struct hlist_head srcfile_htab[SRC_HTAB_SZ];
static LIST_HEAD(srcfile_list);
static long map_total_sz;
static int num_srcfiles;
static int countlines(char *map, int maplen)
{
int numl;
char *end = map + maplen;
char *p = map;
if (maplen == 0)
return 0;
numl = 0;
while (p < end && (p = memchr(p, '\n', end - p)) != NULL) {
numl++;
p++;
}
if (p < end)
numl++;
return numl;
}
static void fill_lines(char **lines, int maxline, char *map, int maplen)
{
int l;
char *end = map + maplen;
char *p = map;
if (maplen == 0 || maxline == 0)
return;
l = 0;
lines[l++] = map;
while (p < end && (p = memchr(p, '\n', end - p)) != NULL) {
if (l >= maxline)
return;
lines[l++] = ++p;
}
if (p < end)
lines[l] = p;
}
static void free_srcfile(struct srcfile *sf)
{
list_del_init(&sf->nd);
hlist_del(&sf->hash_nd);
map_total_sz -= sf->maplen;
munmap(sf->map, sf->maplen);
zfree(&sf->lines);
zfree(&sf->fn);
free(sf);
num_srcfiles--;
}
static struct srcfile *find_srcfile(char *fn)
{
struct stat st;
struct srcfile *h;
int fd;
unsigned long sz;
size_t hval = str_hash(fn) % SRC_HTAB_SZ;
hlist_for_each_entry (h, &srcfile_htab[hval], hash_nd) {
if (!strcmp(fn, h->fn)) {
/* Move to front */
list_move(&h->nd, &srcfile_list);
return h;
}
}
/* Only prune if there is more than one entry */
while ((num_srcfiles > MAXSRCFILES || map_total_sz > MAXSRCCACHE) &&
srcfile_list.next != &srcfile_list) {
assert(!list_empty(&srcfile_list));
h = list_entry(srcfile_list.prev, struct srcfile, nd);
free_srcfile(h);
}
fd = open(fn, O_RDONLY);
if (fd < 0 || fstat(fd, &st) < 0) {
Annotation
- Immediate include surface: `linux/list.h`, `linux/zalloc.h`, `stdlib.h`, `sys/mman.h`, `sys/stat.h`, `fcntl.h`, `unistd.h`, `assert.h`.
- Detected declarations: `struct srcfile`, `function countlines`, `function fill_lines`, `function free_srcfile`, `function hlist_for_each_entry`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.