summaryrefslogtreecommitdiffstats
path: root/src/detection/cpucache/cpucache.h
blob: 287154bbc65c23261f7bcabf5c05cd612bbc93e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#pragma once

#include "fastfetch.h"
#include "modules/cpucache/option.h"

typedef enum FF_A_PACKED FFCPUCacheType {
    FF_CPU_CACHE_TYPE_UNIFIED = 0,
    FF_CPU_CACHE_TYPE_INSTRUCTION = 1,
    FF_CPU_CACHE_TYPE_DATA = 2,
    FF_CPU_CACHE_TYPE_TRACE = 3,
} FFCPUCacheType;

typedef struct FFCPUCache {
    uint32_t size;
    uint32_t num;
    uint32_t lineSize;
    FFCPUCacheType type;
} FFCPUCache;

typedef struct FFCPUCacheResult {
    FFlist caches[4]; // L1, L2, L3, L4(?)
} FFCPUCacheResult;

const char* ffDetectCPUCache(FFCPUCacheResult* result);

static inline FFCPUCache* ffCPUCacheAddItem(FFCPUCacheResult* result, uint32_t level, uint32_t size, uint32_t lineSize, FFCPUCacheType type) {
    FFlist* cacheLevel = &result->caches[level - 1];

    FF_LIST_FOR_EACH (FFCPUCache, item, *cacheLevel) {
        if (item->type == type && item->size == size && item->lineSize == lineSize) {
            item->num++;
            return item;
        }
    }

    FFCPUCache* item = FF_LIST_ADD(FFCPUCache, *cacheLevel);
    *item = (FFCPUCache) {
        .size = size,
        .num = 1,
        .lineSize = lineSize,
        .type = type,
    };
    return item;
}