1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 
24 /*
25  * dtopen() with handle placed in specific vm region
26  */
27 
28 #include <dt.h>
29 
30 typedef struct Dc_s
31 {
32 	Dtdisc_t	ndisc;
33 	Dtdisc_t*	odisc;
34 	Vmalloc_t*	vm;
35 } Dc_t;
36 
37 static int
eventf(Dt_t * dt,int op,void * data,Dtdisc_t * disc)38 eventf(Dt_t* dt, int op, void* data, Dtdisc_t* disc)
39 {
40 	Dc_t*	dc = (Dc_t*)disc;
41 	int	r;
42 
43 	if (dc->odisc->eventf && (r = (*dc->odisc->eventf)(dt, op, data, dc->odisc)))
44 		return r;
45 	return op == DT_ENDOPEN ? 1 : 0;
46 }
47 
48 static void*
memoryf(Dt_t * dt,void * addr,size_t size,Dtdisc_t * disc)49 memoryf(Dt_t* dt, void* addr, size_t size, Dtdisc_t* disc)
50 {
51 	return vmresize(((Dc_t*)disc)->vm, addr, size, VM_RSMOVE);
52 }
53 
54 /*
55  * open a dictionary using disc->memoryf if set or vm otherwise
56  */
57 
58 Dt_t*
_dtnew(Vmalloc_t * vm,Dtdisc_t * disc,Dtmethod_t * meth,unsigned long version)59 _dtnew(Vmalloc_t* vm, Dtdisc_t* disc, Dtmethod_t* meth, unsigned long version)
60 {
61 	Dt_t*		dt;
62 	Dc_t		dc;
63 
64 	dc.odisc = disc;
65 	dc.ndisc = *disc;
66 	dc.ndisc.eventf = eventf;
67 	if (!dc.ndisc.memoryf)
68 		dc.ndisc.memoryf = memoryf;
69 	dc.vm = vm;
70 	if (dt = _dtopen(&dc.ndisc, meth, version))
71 		dtdisc(dt, disc, DT_SAMECMP|DT_SAMEHASH);
72 	return dt;
73 }
74 
75 #undef dtnew
76 
77 Dt_t*
dtnew(Vmalloc_t * vm,Dtdisc_t * disc,Dtmethod_t * meth)78 dtnew(Vmalloc_t* vm, Dtdisc_t* disc, Dtmethod_t* meth)
79 {
80 	return _dtnew(vm, disc, meth, 20050420L);
81 }
82