1 /*
2  * Copyright © 2004 Keith Packard and Bart Massey.
3  * All Rights Reserved.  See the file COPYING in this directory
4  * for licensing information.
5  */
6 
7 #include	"nickle.h"
8 
9 static Value
ForeignEqual(Value av,Value bv,int expandOk)10 ForeignEqual (Value av, Value bv, int expandOk)
11 {
12     if (av->foreign.data == bv->foreign.data)
13 	return TrueVal;
14     return FalseVal;
15 }
16 
17 static Bool
ForeignPrint(Value f,Value av,char format,int base,int width,int prec,int fill)18 ForeignPrint (Value f, Value av, char format, int base, int width, int prec, int fill)
19 {
20     FilePrintf (f, "foreign %s (0x%x)", av->foreign.id, av->foreign.data);
21     return True;
22 }
23 
24 static HashValue
ForeignHash(Value av)25 ForeignHash (Value av)
26 {
27     return (HashValue) (intptr_t) av->foreign.data;
28 }
29 
30 static void
ForeignMark(void * object)31 ForeignMark (void *object)
32 {
33     Foreign *foreign = object;
34 
35     if (foreign->mark)
36 	(*foreign->mark) (foreign->data);
37 }
38 
39 static int
ForeignFree(void * object)40 ForeignFree (void *object)
41 {
42     Foreign *foreign = object;
43 
44     if (foreign->free)
45 	(*foreign->free) (foreign->data);
46     return 1;
47 }
48 
49 ValueRep ForeignRep = {
50     { ForeignMark, ForeignFree, "ForeignRep" },	    /* data */
51     rep_foreign,	    /* tag */
52     {		    /* binary */
53 	0,
54 	0,
55 	0,
56 	0,
57 	0,
58 	0,
59 	0,
60 	ForeignEqual,
61 	0,
62 	0
63     },
64     {
65 	0,
66 	0,
67 	0,
68     },
69     0, 0,
70     ForeignPrint,
71     0,
72     ForeignHash,
73 };
74 
75 int
ForeignInit(void)76 ForeignInit (void)
77 {
78     return 1;
79 }
80 
81 Value
NewForeign(const char * id,void * data,void (* mark)(void * data),void (* free)(void * data))82 NewForeign (const char *id, void *data, void (*mark) (void *data), void (*free) (void *data))
83 {
84     ENTER ();
85     Value   ret;
86 
87     ret = ALLOCATE (&ForeignRep.data, sizeof (Foreign));
88     ret->foreign.id = id;
89     ret->foreign.data = data;
90     ret->foreign.free = free;
91     ret->foreign.mark = mark;
92     RETURN (ret);
93 }
94