|
使用文件系统的汉字库显示汉字的时候,系统用到了64个汉字的缓存作为当前显示的汉字点阵区,当有新的汉字要显示时,则会向系统先查询在当前的缓存区是否已经存在该汉字,如果没有,则会向申请一个汉字的缓存区,同时把该汉字的图形读入到申请的缓存中,
static rt_uint8_t* _font_cache_get(struct rtgui_hz_file_font* font, rt_uint16_t hz_id)
{
rt_uint32_t seek;
struct hz_cache *cache, search;
search.hz_id = hz_id;
cache = SPLAY_FIND(cache_tree, &(font->cache_root), &search);
if (cache != RT_NULL)
{
/* find it */
return (rt_uint8_t*)(cache + 1);
}
/* can not find it, load to cache */
cache = (struct hz_cache*) rtgui_malloc(sizeof(struct hz_cache) + font->font_data_size);
if (cache == RT_NULL) return RT_NULL; /* no memory yet */
cache->hz_id = hz_id;
seek = 94 * (((hz_id & 0xff) - 0xA0) - 1) + ((hz_id >> 8) - 0xA0) - 1;
seek *= font->font_data_size;
/* read hz font data */
if ((lseek(font->fd, seek, SEEK_SET) < 0) ||
read(font->fd, (char*)(cache + 1), font->font_data_size) !=
font->font_data_size)
{
rtgui_free(cache);
return RT_NULL;
}
/* insert to cache */
SPLAY_INSERT(cache_tree, &(font->cache_root), cache);
font->cache_size ++;
if (font->cache_size > HZ_CACHE_MAX)
{
/* remove a cache */
struct hz_cache* left;
left = font->cache_root.sph_root;
while (SPLAY_LEFT(left, hz_node) != RT_NULL) left = SPLAY_LEFT(left, hz_node);
/* remove the left node */
SPLAY_REMOVE(cache_tree, &(font->cache_root), left);
font->cache_size --;
}
return (rt_uint8_t*)(cache + 1);
}
如果缓存区的大小超过了HZ_CACHE_MAX,则在最后插入新加的汉字,同时移走一个汉字,问题就是向系统申请了新的内存,却没有把移走的汉字内存释放,应该在SPLAY_REMOVE(cache_tree, &(font->cache_root), left);后加入 rtgui_free(left);,这样内存才不会不断泄露。 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|