summaryrefslogtreecommitdiffstats
path: root/Objects/object.c
blob: 9f5b6c1e303586ba50ea0137dd635704a1ec2a97 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* Object implementation; and 'noobject' implementation */

#include <stdio.h>

#include "PROTO.h"
#include "object.h"
#include "stringobject.h"
#include "objimpl.h"
#include "errors.h"

extern object *err_nomem PROTO((void)); /* XXX from modsupport.c */

int StopPrint; /* Flag to indicate printing must be stopped */

/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros */

object *
newobject(tp)
	typeobject *tp;
{
	object *op = (object *) malloc(tp->tp_basicsize);
	if (op == NULL)
		return err_nomem();
	NEWREF(op);
	op->ob_type = tp;
	return op;
}

#if 0 /* unused */

varobject *
newvarobject(tp, size)
	typeobject *tp;
	unsigned int size;
{
	varobject *op = (varobject *)
		malloc(tp->tp_basicsize + size * tp->tp_itemsize);
	if (op == NULL)
		return err_nomem();
	NEWREF(op);
	op->ob_type = tp;
	op->ob_size = size;
	return op;
}

#endif

static int prlevel;

void
printobject(op, fp, flags)
	object *op;
	FILE *fp;
	int flags;
{
	/* Hacks to make printing a long or recursive object interruptible */
	prlevel++;
	if (!StopPrint && intrcheck()) {
		fprintf(fp, "\n[print interrupted]\n");
		StopPrint = 1;
	}
	if (!StopPrint) {
		if (op == NULL) {
			fprintf(fp, "<nil>");
		}
		else if (op->ob_type->tp_print == NULL) {
			fprintf(fp, "<%s object at %lx>",
				op->ob_type->tp_name, (long)op);
		}
		else {
			(*op->ob_type->tp_print)(op, fp, flags);
		}
	}
	prlevel--;
	if (prlevel == 0)
		StopPrint = 0;
}

object *
reprobject(v)
	object *v;
{
	object *w;
	/* Hacks to make converting a long or recursive object interruptible */
	prlevel++;
	if (!StopPrint && intrcheck()) {
		StopPrint = 1;
		w = NULL;
		err_set(KeyboardInterrupt);
	}
	if (!StopPrint) {
		if (v == NULL) {
			w = newstringobject("<nil>");
		}
		else if (v->ob_type->tp_repr == NULL) {
			char buf[100];
			sprintf(buf, "<%.80s object at %lx>",
				v->ob_type->tp_name, (long)v);
			w = newstringobject(buf);
		}
		else {
			w = (*v->ob_type->tp_repr)(v);
		}
	}
	prlevel--;
	if (prlevel == 0)
		StopPrint = 0;
	return w;
}

int
cmpobject(v, w)
	object *v, *w;
{
	typeobject *tp;
	if (v == w)
		return 0;
	if (v == NULL)
		return -1;
	if (w == NULL)
		return 1;
	if ((tp = v->ob_type) != w->ob_type)
		return strcmp(tp->tp_name, w->ob_type->tp_name);
	if (tp->tp_compare == NULL)
		return (v < w) ? -1 : 1;
	return ((*tp->tp_compare)(v, w));
}


/*
NoObject is usable as a non-NULL undefined value, used by the macro None.
There is (and should be!) no way to create other objects of this type,
so there is exactly one.
*/

static void
noprint(op, fp, flags)
	object *op;
	FILE *fp;
	int flags;
{
	fprintf(fp, "<no value>");
}

static typeobject Notype = {
	OB_HEAD_INIT(&Typetype)
	0,
	"novalue",
	0,
	0,
	0,		/*tp_dealloc*/ /*never called*/
	noprint,	/*tp_print*/
};

object NoObject = {
	OB_HEAD_INIT(&Notype)
};


#ifdef TRACE_REFS

static object refchain = {&refchain, &refchain};

NEWREF(op)
	object *op;
{
	ref_total++;
	op->ob_refcnt = 1;
	op->_ob_next = refchain._ob_next;
	op->_ob_prev = &refchain;
	refchain._ob_next->_ob_prev = op;
	refchain._ob_next = op;
}

DELREF(op)
	object *op;
{
	op->_ob_next->_ob_prev = op->_ob_prev;
	op->_ob_prev->_ob_next = op->_ob_next;
	(*(op)->ob_type->tp_dealloc)(op);
}

printrefs(fp)
	FILE *fp;
{
	object *op;
	fprintf(fp, "Remaining objects:\n");
	for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
		fprintf(fp, "[%d] ", op->ob_refcnt);
		printobject(op, fp, 0);
		putc('\n', fp);
	}
}

#endif