Skip to content

Commit a9c3c22

Browse files
* Extended X interface: pixmap objects, colormap objects visual objects,
image objects, and lots of new methods. * Added counting of allocations and deallocations of builtin types if COUNT_ALLOCS is defined. Had to move calls to NEWREF down in some files. * Bug fix in sorting lists.
1 parent 35fe6ec commit a9c3c22

12 files changed

Lines changed: 150 additions & 18 deletions

File tree

‎Include/object.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ typedef struct _typeobject {
198198
/* More standard operations (at end for binary compatibility) */
199199

200200
long (*tp_hash) FPROTO((object *));
201+
#ifdef COUNT_ALLOCS
202+
/* these must be last */
203+
int tp_alloc;
204+
int tp_free;
205+
int tp_maxalloc;
206+
struct _typeobject *tp_next;
207+
#endif
201208
} typeobject;
202209

203210
extern typeobject Typetype; /* The type of type objects */
@@ -253,23 +260,39 @@ environment the global variable trick is not safe.)
253260
#endif
254261

255262
#ifndef TRACE_REFS
263+
#ifdef COUNT_ALLOCS
264+
#define DELREF(op) ((op)->ob_type->tp_free++, (*(op)->ob_type->tp_dealloc)((object *)(op)))
265+
#else
256266
#define DELREF(op) (*(op)->ob_type->tp_dealloc)((object *)(op))
267+
#endif
257268
#define UNREF(op) /*empty*/
258269
#endif
259270

271+
#ifdef COUNT_ALLOCS
272+
extern void inc_count PROTO((typeobject *));
273+
#endif
274+
260275
#ifdef REF_DEBUG
261276
extern long ref_total;
262277
#ifndef TRACE_REFS
278+
#ifdef COUNT_ALLOCS
279+
#define NEWREF(op) (inc_count((op)->ob_type), ref_total++, (op)->ob_refcnt = 1)
280+
#else
263281
#define NEWREF(op) (ref_total++, (op)->ob_refcnt = 1)
264282
#endif
283+
#endif
265284
#define INCREF(op) (ref_total++, (op)->ob_refcnt++)
266285
#define DECREF(op) \
267286
if (--ref_total, --(op)->ob_refcnt > 0) \
268287
; \
269288
else \
270289
DELREF(op)
271290
#else
291+
#ifdef COUNT_ALLOCS
292+
#define NEWREF(op) (inc_count((op)->ob_type), (op)->ob_refcnt = 1)
293+
#else
272294
#define NEWREF(op) ((op)->ob_refcnt = 1)
295+
#endif
273296
#define INCREF(op) ((op)->ob_refcnt++)
274297
#define DECREF(op) \
275298
if (--(op)->ob_refcnt > 0) \

‎Modules/arraymodule.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,10 @@ newarrayobject(size, descr)
252252
return err_nomem();
253253
}
254254
}
255-
NEWREF(op);
256255
op->ob_type = &Arraytype;
257256
op->ob_size = size;
258257
op->ob_descr = descr;
258+
NEWREF(op);
259259
return (object *) op;
260260
}
261261

‎Modules/config.c.in‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,9 @@ extern void initGlx();
310310
#ifdef USE_HTML
311311
extern void initHTML();
312312
#endif
313+
#ifdef USE_XLIB
314+
extern void initXlib();
315+
#endif
313316
/* -- ADDMODULE MARKER 1 -- */
314317

315318
struct {
@@ -485,6 +488,10 @@ struct {
485488
{"HTML", initHTML},
486489
#endif
487490

491+
#ifdef USE_XLIB
492+
{"Xlib", initXlib},
493+
#endif
494+
488495
/* -- ADDMODULE MARKER 2 -- */
489496

490497
{0, 0} /* Sentinel */

‎Modules/imageop.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,9 +542,15 @@ imageop_rgb2rgb8(self, args)
542542
for ( i=0; i < nlen; i++ ) {
543543
/* Bits in source: aaaaaaaa BBbbbbbb GGGggggg RRRrrrrr */
544544
value = *cp++;
545+
#if 0
545546
r = (value >> 5) & 7;
546547
g = (value >> 13) & 7;
547548
b = (value >> 22) & 3;
549+
#else
550+
r = (int) ((value & 0xff) / 255. * 7. + .5);
551+
g = (int) (((value >> 8) & 0xff) / 255. * 7. + .5);
552+
b = (int) (((value >> 16) & 0xff) / 255. * 3. + .5);
553+
#endif
548554
nvalue = (r<<5) | (b<<3) | g;
549555
*ncp++ = nvalue;
550556
}

‎Modules/imgfile.c‎

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4343

4444
static object * ImgfileError; /* Exception we raise for various trouble */
4545

46+
static int top_to_bottom; /* True if we want top-to-bottom images */
4647

4748
/* The image library does not always call the error hander :-(,
4849
therefore we have a global variable indicating that it was called.
@@ -86,6 +87,20 @@ imgfile_open(fname)
8687
return image;
8788
}
8889

90+
static object *
91+
imgfile_ttob(self, args)
92+
object *self;
93+
object *args;
94+
{
95+
int newval;
96+
object *rv;
97+
98+
if (!getargs(args, "i", &newval))
99+
return NULL;
100+
rv = newintobject(top_to_bottom);
101+
top_to_bottom = newval;
102+
return rv;
103+
}
89104

90105
static object *
91106
imgfile_read(self, args)
@@ -100,7 +115,8 @@ imgfile_read(self, args)
100115
static short rs[8192], gs[8192], bs[8192];
101116
int x, y;
102117
IMAGE *image;
103-
118+
int yfirst, ylast, ystep;
119+
104120
if ( !getargs(args, "s", &fname) )
105121
return NULL;
106122

@@ -139,7 +155,17 @@ imgfile_read(self, args)
139155
}
140156
cdatap = getstringvalue(rv);
141157
idatap = (long *)cdatap;
142-
for ( y=0; y < ysize && !error_called; y++ ) {
158+
159+
if (top_to_bottom) {
160+
yfirst = ysize-1;
161+
ylast = -1;
162+
ystep = -1;
163+
} else {
164+
yfirst = 0;
165+
ylast = ysize;
166+
ystep = 1;
167+
}
168+
for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
143169
if ( zsize == 1 ) {
144170
getrow(image, rs, y, 0);
145171
for(x=0; x<xsize; x++ )
@@ -164,14 +190,17 @@ imgfile_read(self, args)
164190

165191
static IMAGE *glob_image;
166192
static long *glob_datap;
167-
static int glob_width, glob_z;
193+
static int glob_width, glob_z, glob_ysize;
168194

169195
static
170196
xs_get(buf, y)
171197
short *buf;
172198
int y;
173199
{
174-
getrow(glob_image, buf, y, glob_z);
200+
if (top_to_bottom)
201+
getrow(glob_image, buf, (glob_ysize-1-y), glob_z);
202+
else
203+
getrow(glob_image, buf, y, glob_z);
175204
}
176205

177206
static
@@ -221,6 +250,7 @@ xscale(image, xsize, ysize, zsize, datap, xnew, ynew, fmode, blur)
221250
glob_image = image;
222251
glob_datap = datap;
223252
glob_width = xnew;
253+
glob_ysize = ysize;
224254
if ( zsize == 1 ) {
225255
glob_z = 0;
226256
filterzoom(xs_get, xs_put_c, xsize, ysize, xnew, ynew, fmode, blur);
@@ -255,6 +285,7 @@ imgfile_readscaled(self, args)
255285
double blur;
256286
int extended;
257287
int fmode;
288+
int yfirst, ylast, ystep;
258289

259290
/*
260291
** Parse args. Funny, since arg 4 and 5 are optional
@@ -331,7 +362,16 @@ imgfile_readscaled(self, args)
331362
if ( extended ) {
332363
xscale(image, xsize, ysize, zsize, idatap, xwtd, ywtd, fmode, blur);
333364
} else {
334-
for ( y=0; y < ywtd && !error_called; y++ ) {
365+
if (top_to_bottom) {
366+
yfirst = ywtd-1;
367+
ylast = -1;
368+
ystep = -1;
369+
} else {
370+
yfirst = 0;
371+
ylast = ywtd;
372+
ystep = 1;
373+
}
374+
for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
335375
yorig = (int)(y*yfac);
336376
if ( zsize == 1 ) {
337377
getrow(image, rs, yorig, 0);
@@ -392,6 +432,8 @@ imgfile_write(self, args)
392432
short r, g, b;
393433
long rgb;
394434
int x, y;
435+
int yfirst, ylast, ystep;
436+
395437

396438
if ( !getargs(args, "(ss#iii)",
397439
&fname, &cdatap, &len, &xsize, &ysize, &zsize) )
@@ -425,7 +467,16 @@ imgfile_write(self, args)
425467

426468
idatap = (long *)cdatap;
427469

428-
for( y=0; y<ysize && !error_called; y++ ) {
470+
if (top_to_bottom) {
471+
yfirst = ysize-1;
472+
ylast = -1;
473+
ystep = -1;
474+
} else {
475+
yfirst = 0;
476+
ylast = ysize;
477+
ystep = 1;
478+
}
479+
for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
429480
if ( zsize == 1 ) {
430481
for( x=0; x<xsize; x++ )
431482
rs[x] = *cdatap++;
@@ -459,6 +510,7 @@ static struct methodlist imgfile_methods[] = {
459510
{ "read", imgfile_read },
460511
{ "readscaled", imgfile_readscaled, 1},
461512
{ "write", imgfile_write },
513+
{ "ttob", imgfile_ttob },
462514
{ NULL, NULL } /* Sentinel */
463515
};
464516

‎Objects/floatobject.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ newfloatobject(fval)
6060
register floatobject *op = (floatobject *) malloc(sizeof(floatobject));
6161
if (op == NULL)
6262
return err_nomem();
63-
NEWREF(op);
6463
op->ob_type = &Floattype;
6564
op->ob_fval = fval;
65+
NEWREF(op);
6666
return (object *) op;
6767
}
6868

‎Objects/intobject.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ newintobject(ival)
8787
}
8888
v = free_list;
8989
free_list = *(intobject **)free_list;
90-
NEWREF(v);
9190
v->ob_type = &Inttype;
9291
v->ob_ival = ival;
92+
NEWREF(v);
9393
return (object *) v;
9494
}
9595

‎Objects/listobject.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ newlistobject(size)
5858
return err_nomem();
5959
}
6060
}
61-
NEWREF(op);
6261
op->ob_type = &Listtype;
6362
op->ob_size = size;
6463
for (i = 0; i < size; i++)
6564
op->ob_item[i] = NULL;
65+
NEWREF(op);
6666
return (object *) op;
6767
}
6868

@@ -520,7 +520,7 @@ cmp(v, w)
520520
return cmpobject(* (object **) v, * (object **) w);
521521

522522
/* Call the user-supplied comparison function */
523-
t = mkvalue("OO", v, w);
523+
t = mkvalue("OO", * (object **) v, * (object **) w);
524524
if (t == NULL)
525525
return 0;
526526
res = call_object(cmpfunc, t);

‎Objects/object.c‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,45 @@ long ref_total;
3434
These are used by the individual routines for object creation.
3535
Do not call them otherwise, they do not initialize the object! */
3636

37+
#ifdef COUNT_ALLOCS
38+
static typeobject *type_list;
39+
40+
void
41+
dump_counts()
42+
{
43+
typeobject *tp;
44+
45+
for (tp = type_list; tp; tp = tp->tp_next)
46+
printf("%s %d %d %d\n", tp->tp_name, tp->tp_alloc, tp->tp_free,
47+
tp->tp_maxalloc);
48+
}
49+
50+
void
51+
inc_count(tp)
52+
typeobject *tp;
53+
{
54+
if (tp->tp_alloc == 0) {
55+
/* first time; hang in linked list */
56+
if (tp->tp_next != NULL) /* sanity check */
57+
abort();
58+
tp->tp_next = type_list;
59+
type_list = tp;
60+
}
61+
tp->tp_alloc++;
62+
if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
63+
tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
64+
}
65+
#endif
66+
3767
object *
3868
newobject(tp)
3969
typeobject *tp;
4070
{
4171
object *op = (object *) malloc(tp->tp_basicsize);
4272
if (op == NULL)
4373
return err_nomem();
44-
NEWREF(op);
4574
op->ob_type = tp;
75+
NEWREF(op);
4676
return op;
4777
}
4878

@@ -55,9 +85,9 @@ newvarobject(tp, size)
5585
malloc(tp->tp_basicsize + size * tp->tp_itemsize);
5686
if (op == NULL)
5787
return (varobject *)err_nomem();
58-
NEWREF(op);
5988
op->ob_type = tp;
6089
op->ob_size = size;
90+
NEWREF(op);
6191
return op;
6292
}
6393

@@ -301,6 +331,9 @@ NEWREF(op)
301331
op->_ob_prev = &refchain;
302332
refchain._ob_next->_ob_prev = op;
303333
refchain._ob_next = op;
334+
#ifdef COUNT_ALLOCS
335+
inc_count(op->ob_type);
336+
#endif
304337
}
305338

306339
UNREF(op)
@@ -335,6 +368,9 @@ DELREF(op)
335368
object *op;
336369
{
337370
UNREF(op);
371+
#ifdef COUNT_ALLOCS
372+
op->ob_type->tp_free++;
373+
#endif
338374
(*(op)->ob_type->tp_dealloc)(op);
339375
op->ob_type = NULL;
340376
}

0 commit comments

Comments
 (0)