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
|
#include "shape.h"
#ifdef DMALLOC
#include "dmalloc.h"
#endif
static void *zmalloc(Vmalloc_t *arena, size_t request)
{
void *rv;
rv = vmalloc(arena, request);
if (rv) memset(rv, request, '\0');
return rv;
}
ilcurve_t *il_newcurve(Vmalloc_t *arena, ilcurvetype_t kind, int maxpts) {
ilcurve_t *rv;
rv = zmalloc(arena,sizeof(ilcurve_t));
rv->type = kind;
rv->n = 0;
rv->p = zmalloc(arena,sizeof(ilcoord_t)*maxpts);
return rv;
}
/* note that the value of contents is copied and contents is freed */
ilshape_t *il_newshape(Vmalloc_t *arena, ilcurve_t *contents, ilshape_t *link) {
ilshape_t *rv;
rv = zmalloc(arena, sizeof(ilshape_t));
if (contents) {
if (contents->type == IL_POLYLINE)
rv->type = IL_POLYGON;
else
rv->type = IL_SPLINEGON;
rv->def.curve = *contents;
/* i designed the interface wrong. either def.curve should
be a pointer, or the contents arg should be a value not a
pointer, but this is the easiest way to deal with the error
right now. */
vmfree(vmregion(contents),contents);
}
else rv->type = IL_NOSHAPE;
rv->next = link;
return rv;
}
void il_freeshape(Vmalloc_t *arena, ilshape_t *shape)
{
ilshape_t *link;
while (shape) {
link = shape->next;
switch(shape->type) {
case IL_POLYGON:
case IL_SPLINEGON:
if (shape->def.curve.p)
vmfree(arena,shape->def.curve.p);
break;
default:
break;
}
vmfree(arena,shape);
shape = link;
}
}
void il_freecurve(Vmalloc_t *arena, ilcurve_t *curve)
{
vmfree(arena,curve->p);
vmfree(arena,curve);
}
ilshape_t *il_copyshape(Vmalloc_t *arena, ilshape_t *src)
{
ilshape_t *rv;
size_t sz;
rv = zmalloc(arena,sizeof(ilshape_t));
rv->type = src->type;
switch (rv->type) {
case IL_POLYGON:
case IL_SPLINEGON:
rv->def.curve.type = src->def.curve.type;
rv->def.curve.n = src->def.curve.n;
sz = rv->def.curve.n * sizeof(ilcoord_t);
rv->def.curve.p = zmalloc(arena,sz);
memcpy(rv->def.curve.p,src->def.curve.p,sz);
break;
case IL_ELLIPSE:
case IL_CIRCLE:
rv->def.ellipse = src->def.ellipse;
break;
default:
abort();
}
return rv;
}
|