1 /*
2  * ald_manager.c  dri file manager
3  *
4  * Copyright (C) 1997-1998 Masaki Chikama (Wren) <chikama@kasumi.ipl.mech.nagoya-u.ac.jp>
5  *               1998-                           <masaki-c@is.aist-nara.ac.jp>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21 */
22 /* $Id: ald_manager.c,v 1.3 2001/05/08 05:36:07 chikama Exp $ */
23 
24 #include <glib.h>
25 #include "portab.h"
26 #include "dri.h"
27 #include "cache.h"
28 #include "ald_manager.h"
29 
30 /* drifiles object */
31 static drifiles *dri[DRIFILETYPEMAX];
32 
33 /* cache handler for dri file */
34 static cacher *cacheid;
35 
36 /*
37  * static maethods
38 */
39 static void ald_free(dridata *dfile);
40 
41 /*
42  * free dridata
43  *   dfile: dridata to be free
44 */
ald_free(dridata * dfile)45 static void ald_free(dridata *dfile) {
46 	g_free(dfile->data_raw);
47 	g_free(dfile);
48 }
49 
50 /*
51  * load dri data
52  *   type: data type
53  *   no  : file no ( >= 0 )
54  *   return: loaded dridata object
55 */
ald_getdata(DRIFILETYPE type,int no)56 dridata *ald_getdata(DRIFILETYPE type, int no) {
57 	dridata *ddata;
58 
59 	/* check wrong request number */
60 	if (no < 0) return NULL;
61 
62 	/* check wrong type */
63 	if (type >= DRIFILETYPEMAX) return NULL;
64 
65 	/* check uninitilized data */
66 	if (dri[type] == NULL) return NULL;
67 
68 	/* if mmapped */
69 	if (dri[type]->mmapped) return dri_getdata(dri[type], no);
70 
71 	/* not mmapped */
72 	if (NULL == (ddata = (dridata *)cache_foreach(cacheid, (type << 16) + no))) {
73 		ddata = dri_getdata(dri[type], no);
74 		if (ddata != NULL) {
75 			cache_insert(cacheid, (type << 16) + no, (void *)ddata, ddata->size, &(ddata->in_use));
76 			ddata->in_use = TRUE;
77 		}
78 	}
79 
80 	return ddata;
81 }
82 
83 /*
84  * free dri object
85  *   data: object to be free
86  */
ald_freedata(dridata * data)87 void ald_freedata(dridata *data) {
88 	if (data == NULL) return;
89 
90 	if (data->a->mmapped) {
91 		g_free(data);
92 	} else {
93 		data->in_use = FALSE;
94 	}
95 }
96 
97 /*
98  * Initilize ald manager
99  *   type: data type
100  *   file: file name array
101  *   cnt : number in file name array
102  *   mmap: mmap file or not
103 */
ald_init(int type,char ** file,int cnt,boolean mmap)104 void ald_init(int type, char **file, int cnt, boolean mmap) {
105 	/* check wrong type */
106 	if (type < DRIFILETYPEMAX) {
107 		dri[type] = dri_init(file, cnt, mmap);
108 		if (!dri[type]->mmapped) {
109 			cacheid = cache_new(ald_free);
110 		}
111 	}
112 }
113