File: foreign.c

package info (click to toggle)
nickle 2.68-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,336 kB
  • ctags: 3,288
  • sloc: ansic: 31,198; yacc: 1,860; lex: 858; sh: 830; makefile: 229
file content (95 lines) | stat: -rw-r--r-- 1,553 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* $Header$ */

/*
 * Copyright © 2004 Keith Packard and Bart Massey.
 * All Rights Reserved.  See the file COPYING in this directory
 * for licensing information.
 */

#include	"nickle.h"

static Value
ForeignEqual (Value av, Value bv, int expandOk)
{
    if (av->foreign.data == bv->foreign.data)
	return TrueVal;
    return FalseVal;
}

static Bool
ForeignPrint (Value f, Value av, char format, int base, int width, int prec, int fill)
{
    FilePrintf (f, "foreign %s (0x%x)", av->foreign.id, av->foreign.data);
    return True;
}

static HashValue
ForeignHash (Value av)
{
    return (HashValue) av->foreign.data;
}

static void
ForeignMark (void *object)
{
    Foreign *foreign = object;

    if (foreign->mark)
	(*foreign->mark) (foreign->data);
}

static int
ForeignFree (void *object)
{
    Foreign *foreign = object;

    if (foreign->free)
	(*foreign->free) (foreign->data);
    return 1;
}

ValueRep ForeignRep = {
    { ForeignMark, ForeignFree, "ForeignRep" },	    /* data */
    rep_foreign,	    /* tag */
    {		    /* binary */
	0,
	0,
	0,
	0,
	0,
	0,
	0,
	ForeignEqual,
	0,
	0
    },
    {
	0,
	0,
	0,
    },
    0, 0,
    ForeignPrint,
    0,
    ForeignHash,
};

int
ForeignInit (void)
{
    return 1;
}

Value
NewForeign (const char *id, void *data, void (*mark) (void *data), void (*free) (void *data))
{
    ENTER ();
    Value   ret;

    ret = ALLOCATE (&ForeignRep.data, sizeof (Foreign));
    ret->foreign.id = id;
    ret->foreign.data = data;
    ret->foreign.free = free;
    ret->foreign.mark = mark;
    RETURN (ret);
}