简体   繁体   English

在C中实现缓存

[英]Implementing Caches in C

I am confused and not sure if what i am doing is correct... 我很困惑,不确定我在做什么是否正确...

typedef struct Node {
   size_t tag;                
   struct Node *next;   
   int valid;
} Node;

typedef struct caches {
   struct Node *tag; 
   int hits;
   int misses;
   int Coldmisses;
} caches;

These are my structs..... 这些是我的结构.....

I created a method... 我创建了一个方法

caches* L1Cache() {
    caches *l1 = malloc(numberofsets1*sizeof(caches));
    if (numberofsets1 != 1) {
        numberofsets1 = L1Size/(blocksize*L1assoc);
    }
    l1->hits = 0;
    l1->misses = 0;
    l1->tag->valid = 0;
    l1->Coldmisses = 0;
    return l1;
}

in my main I called it like caches* L1 = L1Cache() ; 在我的caches* L1 = L1Cache()我将其称为caches* L1 = L1Cache() ; then can i use the L1 something like this???? 那我可以用L1这样的东西吗???? Also I am getting SEGEMENTATION FAULT "l1->tag->valid = 0;" 我也得到SEGEMENTATION FAULT“ l1-> tag-> valid = 0;”。 here IDK WHY?? 在这里IDK为什么? Help please 请帮助

if (L1[indexoffset].tag->valid = 0) {
    L1[indexoffset].tag->tag = tag;
    L1[indexoffset].tag->valid = 1;
}

What if I add l1->tag=malloc(sizeof(Node)); 如果我添加l1->tag=malloc(sizeof(Node));怎样? in caches* L1Cache() right after l1->misses = 0; l1->misses = 0;之后l1->misses = 0;caches* L1Cache() l1->misses = 0; – user3100209 – user3100209

It should work. 它应该工作。 Do not forget to free it at the end or if you change the pointer ( l1->tag=othernode ). 不要忘记在最后释放它,或者如果您更改了指针( l1->tag=othernode )。 – francis –弗朗西斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM