summaryrefslogtreecommitdiff
path: root/command_line/CmdLine/Src/CLIncludeFileCache.c
blob: d479f36ae436cbf08d133337bee25156853ce236 (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "cmdline.h"

typedef struct CacheEntry {
    struct CacheEntry *next;
    struct CacheEntry *prev;
    int locked;
    int precompiled;
    Handle text;
    OSSpec spec;
    UInt32 size;
    UInt32 when;
} CacheEntry;

enum {
    CACHE_SIZE = 0x800000
};

static CacheEntry *cachelist;
static CacheEntry *freelist;
static UInt32 availablecache;

static CacheEntry *makecacheentry() {
    CacheEntry *c = freelist;
    if (c) {
        freelist = c->next;
        return c;
    } else {
        return xmalloc("include file cache", sizeof(CacheEntry));
    }
}

static void insertcacheentry(CacheEntry *c) {
    if (cachelist)
        cachelist->prev = c;
    c->next = cachelist;
    c->prev = NULL;
    cachelist = c;
}

static void deletecacheentry(CacheEntry *c) {
    if (c->next)
        c->next->prev = c->prev;
    if (c->prev)
        c->prev->next = c->next;
    else
        cachelist = c->next;
}

void InitializeIncludeCache() {
    freelist = NULL;
    cachelist = NULL;
    availablecache = CACHE_SIZE;
}

void CleanupIncludeCache() {
    CacheEntry *c;
    CacheEntry *c1;

    for (c = cachelist; c; c = c1) {
        c1 = c->next;
        if (c->text)
            DisposeHandle(c->text);
        xfree(c);
    }

    for (c = freelist; c; c = c1) {
        c1 = c->next;
        xfree(c);
    }

    InitializeIncludeCache();
}

void CacheIncludeFile(OSSpec *spec, Handle text, Boolean precompiled) {
    UInt32 size;
    CacheEntry *c;
    CacheEntry *lru;

    size = GetHandleSize(text);
    if (precompiled) {
        lru = NULL;
        for (c = cachelist; c; c = c->next) {
            if (c->precompiled) {
                lru = c;
                break;
            }
        }
        if (lru) {
            DisposeHandle(lru->text);
            lru->text = NULL;
            deletecacheentry(lru);
            lru->next = freelist;
            freelist = lru;
        }
    } else {
        if ((SInt32) size > (SInt32) CACHE_SIZE) {
            return;
        } else {
            while (size > availablecache) {
                lru = NULL;
                for (c = cachelist; c; c = c->next) {
                    if (!c->locked && !c->precompiled && (lru == NULL || c->when < lru->when))
                        lru = c;
                }

                if (lru) {
                    DisposeHandle(lru->text);
                    lru->text = NULL;
                    availablecache += lru->size;
                    deletecacheentry(lru);
                    lru->next = freelist;
                    freelist = lru;
                } else {
                    return;
                }
            }
        }
    }

    c = makecacheentry();
    c->locked = 1;
    c->precompiled = precompiled;
    c->spec = *spec;
    c->text = text;
    c->size = size;
    c->when = OS_GetMilliseconds();
    insertcacheentry(c);

    if (!precompiled)
        availablecache -= size;
}

Handle CachedIncludeFile(OSSpec *spec, Boolean *precompiled) {
    CacheEntry *c;

    for (c = cachelist; c; c = c->next) {
        if (OS_EqualSpec(&c->spec, spec)) {
            c->when = OS_GetMilliseconds();
            *precompiled = c->precompiled;
            c->locked++;
            return c->text;
        }
    }

    return NULL;
}

void FreeIncludeFile(Handle text) {
    CacheEntry *c;

    for (c = cachelist; c; c = c->next) {
        if (c->text == text) {
            c->locked--;
            return;
        }
    }

    DisposeHandle(text);
}