-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathfd_cache.c
63 lines (61 loc) · 1.31 KB
/
fd_cache.c
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "fd_cache.h"
CFDCache *FDCacheNew() {
CFDCache *r=calloc(1,sizeof(CFDCache));
r->size=0;
r->values=NULL;
return r;
};
char *fd_cache_none="";
static size_t RoundUp(int fd) {
return (fd&~127)+128*2;
}
void FDCacheSet(CFDCache *c,int fd,char *v) {
if(fd<=0) return;
if(fd>=1024) return;
char **new;
if(fd>=c->size) {
new=calloc(1,sizeof(char*)*RoundUp(fd));
if(c->values) memcpy(new,c->values,sizeof(char*)*c->size);
c->size=RoundUp(fd);
free(c->values);
c->values=new;
}
if(FD_CACHE_NOT_FILE!=c->values[fd]) free(c->values[fd]);
if(v==FD_CACHE_NOT_FILE) c->values[fd]=FD_CACHE_NOT_FILE;
else c->values[fd]=strdup(v);
}
void FDCacheRem(CFDCache *c,int fd) {
if(fd<=0) return;
char *have;
if(fd<c->size) {
have=c->values[fd];
if(have!=FD_CACHE_NOT_FILE&&have) {
free(have);
}
c->values[fd]=NULL;
}
};
void FDCacheDel(CFDCache *c) {
long i=c->size;
char *have;
while(--i>=0) {
if(have=c->values[i])
if(have!=FD_CACHE_NOT_FILE)
free(have);
}
free(c->values);
free(c);
}
char *FDCacheGet(CFDCache *c,int fd) {
if(fd<=0) return NULL;
if(fd>=1024) return NULL;
if(fd<c->size) {
if(c->values[fd]==FD_CACHE_NOT_FILE)
return NULL;
return c->values[fd];
}
return NULL;
}
void FDCacheInvalidate(CFDCache *c,int fd) {
FDCacheSet(c,fd,FD_CACHE_NOT_FILE);
}