NetSurf
font_cache.c
Go to the documentation of this file.
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__
39static struct SkipList *ami_font_cache_list = NULL;
40static struct Hook ami_font_cache_hook;
41#else
42static struct MinList *ami_font_cache_list = NULL;
43#endif
44
45
46
47#ifdef __amigaos4__
48static 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__
57static 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);
77 RemoveSkipNode(skiplist, node->skip_node.sn_Key);
78 }
79 } while((node = nnode));
80
81 /* reschedule to run in five minutes */
83}
84#else
85static 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
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 */
114}
115#endif
116
117#ifdef __amigaos4__
118static 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);
129
130 } while((node = nnode));
131
132 DeleteSkipList(skiplist);
133}
134#endif
135
136
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
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
175void ami_font_cache_insert(struct ami_font_cache_node *nodedata, const char *font)
176{
177#ifndef __amigaos4__
179 if(node) {
181 node->objstruct = nodedata;
182 node->dtz_Node.ln_Name = strdup(font);
183 }
184#endif
185}
186
188{
189 NSLOG(netsurf, INFO, "Cleaning up font cache");
191#ifdef __amigaos4__
192 ami_font_cache_del_skiplist(ami_font_cache_list);
193#else
195#endif
196 ami_font_cache_list = NULL;
197}
198
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
207#endif
208
209 /* run first cleanup in ten minutes */
211}
212
nserror ami_schedule(int t, void(*callback)(void *p), void *p)
Schedule a callback.
Definition: schedule.c:331
void ami_font_bullet_close(void *nso)
Definition: font_bullet.c:872
static struct MinList * ami_font_cache_list
Definition: font_cache.c:42
void ami_font_cache_fini(void)
Definition: font_cache.c:187
struct ami_font_cache_node * ami_font_cache_alloc_entry(const char *font)
Definition: font_cache.c:159
void ami_font_cache_insert(struct ami_font_cache_node *nodedata, const char *font)
Definition: font_cache.c:175
void ami_font_cache_init(void)
Definition: font_cache.c:199
static void ami_font_cache_cleanup(struct MinList *ami_font_cache_list)
Definition: font_cache.c:85
struct ami_font_cache_node * ami_font_cache_locate(const char *font)
Definition: font_cache.c:137
struct MinList * NewObjList(void)
Definition: object.c:71
void FreeObjList(struct MinList *objlist)
Definition: object.c:117
void ObjectCallback(struct nsObject *dtzo, void(*callback)(void *nso))
Definition: object.c:92
void DelObject(struct nsObject *dtzo)
Definition: object.c:107
struct nsObject * AddObject(struct MinList *objlist, ULONG otype)
Definition: object.c:77
@ AMINS_FONT
Definition: object.h:37
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
struct Node * GetHead(struct List *list)
Definition: os3support.c:364
struct Node * GetSucc(struct Node *node)
Definition: os3support.c:381
Minimal compatibility header for AmigaOS 3.
#define IsMinListEmpty(L)
Definition: os3support.h:54
uint32_t uint32
Definition: os3support.h:184
#define FindIName
Definition: os3support.h:165
Interface to utility string handling.
uint32 Seconds
Definition: os3support.h:190
struct OutlineFont * font
Definition: font_cache.h:29
struct TimeVal lastused
Definition: font_cache.h:33
struct Node dtz_Node
Definition: object.h:44
void * objstruct
Definition: object.h:46