1 /*
2 * Copyright 2015 Chris Young <chris@unsatisfactorysoftware.co.uk>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "amiga/os3support.h"
20 #include <string.h>
21
22 #include <proto/timer.h>
23 #include <proto/utility.h>
24
25 #include "utils/log.h"
26
27 #include "amiga/font.h"
28 #include "amiga/font_bullet.h"
29 #include "amiga/font_cache.h"
30 #include "amiga/schedule.h"
31
32 #ifdef __amigaos4__
33 #include "amiga/hash/xxhash.h"
34 #else
35 #include "amiga/object.h"
36 #endif
37
38 #ifdef __amigaos4__
39 static struct SkipList *ami_font_cache_list = NULL;
40 static struct Hook ami_font_cache_hook;
41 #else
42 static struct MinList *ami_font_cache_list = NULL;
43 #endif
44
45
46
47 #ifdef __amigaos4__
ami_font_cache_sort(struct Hook * hook,APTR key1,APTR key2)48 static LONG ami_font_cache_sort(struct Hook *hook, APTR key1, APTR key2)
49 {
50 if(key1 == key2) return 0;
51 if(key1 < key2) return -1;
52 return 1;
53 }
54 #endif
55
56 #ifdef __amigaos4__
ami_font_cache_cleanup(struct SkipList * skiplist)57 static void ami_font_cache_cleanup(struct SkipList *skiplist)
58 {
59 struct ami_font_cache_node *node;
60 struct ami_font_cache_node *nnode;
61 struct TimeVal curtime;
62
63 node = (struct ami_font_cache_node *)GetFirstSkipNode(skiplist);
64 if(node == NULL) return;
65
66 do {
67 nnode = (struct ami_font_cache_node *)GetNextSkipNode(skiplist, (struct SkipNode *)node);
68 GetSysTime(&curtime);
69 SubTime(&curtime, &node->lastused);
70 if(curtime.Seconds > 300)
71 {
72 NSLOG(netsurf, INFO,
73 "Freeing font %p not used for %ld seconds",
74 node->skip_node.sn_Key,
75 curtime.Seconds);
76 ami_font_bullet_close(node);
77 RemoveSkipNode(skiplist, node->skip_node.sn_Key);
78 }
79 } while((node = nnode));
80
81 /* reschedule to run in five minutes */
82 ami_schedule(300000, (void *)ami_font_cache_cleanup, ami_font_cache_list);
83 }
84 #else
ami_font_cache_cleanup(struct MinList * ami_font_cache_list)85 static void ami_font_cache_cleanup(struct MinList *ami_font_cache_list)
86 {
87 struct nsObject *node;
88 struct nsObject *nnode;
89 struct ami_font_cache_node *fnode;
90 struct TimeVal curtime;
91
92 if(IsMinListEmpty(ami_font_cache_list)) return;
93
94 node = (struct nsObject *)GetHead((struct List *)ami_font_cache_list);
95
96 do
97 {
98 nnode=(struct nsObject *)GetSucc((struct Node *)node);
99 fnode = node->objstruct;
100 GetSysTime(&curtime);
101 SubTime(&curtime, &fnode->lastused);
102 if(curtime.Seconds > 300)
103 {
104 NSLOG(netsurf, INFO,
105 "Freeing %s not used for %ld seconds",
106 node->dtz_Node.ln_Name,
107 curtime.Seconds);
108 DelObject(node);
109 }
110 } while((node=nnode));
111
112 /* reschedule to run in five minutes */
113 ami_schedule(300000, (void *)ami_font_cache_cleanup, ami_font_cache_list);
114 }
115 #endif
116
117 #ifdef __amigaos4__
ami_font_cache_del_skiplist(struct SkipList * skiplist)118 static void ami_font_cache_del_skiplist(struct SkipList *skiplist)
119 {
120 struct SkipNode *node;
121 struct SkipNode *nnode;
122
123 node = GetFirstSkipNode(skiplist);
124 if(node == NULL) return;
125
126 do {
127 nnode = GetNextSkipNode(skiplist, node);
128 ami_font_bullet_close((struct ami_font_cache_node *)node);
129
130 } while((node = nnode));
131
132 DeleteSkipList(skiplist);
133 }
134 #endif
135
136
ami_font_cache_locate(const char * font)137 struct ami_font_cache_node *ami_font_cache_locate(const char *font)
138 {
139 struct ami_font_cache_node *nodedata = NULL;
140 uint32 hash = 0;
141
142 #ifdef __amigaos4__
143 hash = XXH32(font, strlen(font), 0);
144 nodedata = (struct ami_font_cache_node *)FindSkipNode(ami_font_cache_list, (APTR)hash);
145 #else
146 struct nsObject *node = (struct nsObject *)FindIName((struct List *)ami_font_cache_list, font);
147 if(node) nodedata = node->objstruct;
148 #endif
149
150 if(nodedata) {
151 GetSysTime(&nodedata->lastused);
152 return nodedata;
153 }
154
155 NSLOG(netsurf, INFO, "Font cache miss: %s (%lx)", font, hash);
156 return NULL;
157 }
158
ami_font_cache_alloc_entry(const char * font)159 struct ami_font_cache_node *ami_font_cache_alloc_entry(const char *font)
160 {
161 struct ami_font_cache_node *nodedata;
162
163 #ifdef __amigaos4__
164 uint32 hash = XXH32(font, strlen(font), 0);
165 nodedata = (struct ami_font_cache_node *)InsertSkipNode(ami_font_cache_list, (APTR)hash, sizeof(struct ami_font_cache_node));
166 #else
167 nodedata = malloc(sizeof(struct ami_font_cache_node));
168 #endif
169
170 GetSysTime(&nodedata->lastused);
171
172 return nodedata;
173 }
174
ami_font_cache_insert(struct ami_font_cache_node * nodedata,const char * font)175 void ami_font_cache_insert(struct ami_font_cache_node *nodedata, const char *font)
176 {
177 #ifndef __amigaos4__
178 struct nsObject *node = AddObject(ami_font_cache_list, AMINS_FONT);
179 if(node) {
180 ObjectCallback(node, ami_font_bullet_close);
181 node->objstruct = nodedata;
182 node->dtz_Node.ln_Name = strdup(font);
183 }
184 #endif
185 }
186
ami_font_cache_fini(void)187 void ami_font_cache_fini(void)
188 {
189 NSLOG(netsurf, INFO, "Cleaning up font cache");
190 ami_schedule(-1, (void *)ami_font_cache_cleanup, ami_font_cache_list);
191 #ifdef __amigaos4__
192 ami_font_cache_del_skiplist(ami_font_cache_list);
193 #else
194 FreeObjList(ami_font_cache_list);
195 #endif
196 ami_font_cache_list = NULL;
197 }
198
ami_font_cache_init(void)199 void ami_font_cache_init(void)
200 {
201 #ifdef __amigaos4__
202 ami_font_cache_hook.h_Entry = (HOOKFUNC)ami_font_cache_sort;
203 ami_font_cache_hook.h_Data = 0;
204 ami_font_cache_list = CreateSkipList(&ami_font_cache_hook, 8);
205 #else
206 ami_font_cache_list = NewObjList();
207 #endif
208
209 /* run first cleanup in ten minutes */
210 ami_schedule(600000, (void *)ami_font_cache_cleanup, ami_font_cache_list);
211 }
212
213