Skip to content

Commit 3582aff

Browse files
author
murrell
committed
add GEcreateDD() and GEfreeDD(); satisfies PR#18292
git-svn-id: https://svn.r-project.org/R/trunk@88803 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent d8dfa06 commit 3582aff

File tree

9 files changed

+199
-34
lines changed

9 files changed

+199
-34
lines changed

doc/NEWS.Rd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@
149149

150150
\subsection{C-LEVEL FACILITIES}{
151151
\itemize{
152-
\item .
152+
\item New functions \code{GEcreateDD()} and \code{GEfreeDD()}
153+
for allocating (and initialising) \code{DevDesc} structures.
154+
Can be used by external graphics devices.
155+
Satisfies \PR{18292}.
153156
}
154157
}
155158

doc/manual/R-ints.texi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2864,6 +2864,10 @@ for reallocation (and its name will appear as @code{""} in
28642864
@samp{.Devices}). Exactly one of the devices is `active': this is the
28652865
the null device if no other device has been opened and not killed.
28662866

2867+
There is a @code{GEcreateDD()} to allocate and initialise the @code{DevDesc}
2868+
structure and a @code{GEfreeDD()} to free it (if an error is encountered
2869+
during the device driver set up).
2870+
28672871
Each instance of a graphics device needs to set up a @code{GEDevDesc}
28682872
structure by code very similar to
28692873

@@ -2875,7 +2879,7 @@ structure by code very similar to
28752879
BEGIN_SUSPEND_INTERRUPTS @{
28762880
pDevDesc dev;
28772881
/* Allocate and initialize the device driver data */
2878-
if (!(dev = (pDevDesc) calloc(1, sizeof(DevDesc))))
2882+
if (!(dev = GEcreateDD()))
28792883
return 0; /* or error() */
28802884
/* set up device driver or free 'dev' and error() */
28812885
gdd = GEcreateDevDesc(dev);

src/include/R_ext/GraphicsEngine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ void GEaddDevice(pGEDevDesc);
322322
void GEaddDevice2(pGEDevDesc, const char *);
323323
void GEaddDevice2f(pGEDevDesc, const char *, const char *);
324324
void GEkillDevice(pGEDevDesc);
325+
pDevDesc GEcreateDD();
326+
void GEfreeDD(pDevDesc dd);
325327
pGEDevDesc GEcreateDevDesc(pDevDesc dev);
326328

327329
void GEdestroyDevDesc(pGEDevDesc dd);

src/library/grDevices/src/cairo/cairoBM.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,12 +653,12 @@ SEXP in_Cairo(SEXP args)
653653
BEGIN_SUSPEND_INTERRUPTS {
654654
pDevDesc dev;
655655
/* Allocate and initialize the device driver data */
656-
if (!(dev = (pDevDesc) calloc(1, sizeof(DevDesc)))) return 0;
656+
if (!(dev = GEcreateDD())) return 0;
657657
if (!BMDeviceDriver(dev, devtable[type].gtype, filename, quality,
658658
width, height, pointsize,
659659
bgcolor, res, antialias, family, dpi,
660660
symbolfamily, usePUA)) {
661-
free(dev);
661+
GEfreeDD(dev);
662662
error(_("unable to start device '%s'"), devtable[type].name);
663663
}
664664
gdd = GEcreateDevDesc(dev);

src/library/grDevices/src/devPS.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3244,13 +3244,13 @@ PSDeviceDriver(pDevDesc dd, const char *file, const char *paper,
32443244
/* Check and extract the device parameters */
32453245

32463246
if(strlen(file) > R_PATH_MAX - 1) {
3247-
free(dd);
3247+
GEfreeDD(dd);
32483248
error(_("filename too long in %s()"), "postscript");
32493249
}
32503250

32513251
/* allocate new postscript device description */
32523252
if (!(pd = (PostScriptDesc *) malloc(sizeof(PostScriptDesc)))) {
3253-
free(dd);
3253+
GEfreeDD(dd);
32543254
error(_("memory allocation problem in %s()"), "postscript");
32553255
}
32563256

@@ -3714,7 +3714,7 @@ static void PS_cleanup(int stage, pDevDesc dd, PostScriptDesc *pd)
37143714
freeDeviceEncList(pd->encodings);
37153715
case 1: /* Allocated PDFDesc */
37163716
free(pd);
3717-
free(dd);
3717+
GEfreeDD(dd);
37183718
}
37193719
}
37203720

@@ -6854,7 +6854,7 @@ PDFDeviceDriver(pDevDesc dd, const char *file, const char *paper,
68546854
bool timestamp, bool producer, const char *author)
68556855
{
68566856
/* If we need to bail out with some sort of "error" */
6857-
/* then we must free(dd) */
6857+
/* then we must GEfreeDD(dd) */
68586858

68596859
int i, gotFont;
68606860
double xoff = 0.0, yoff = 0.0, pointsize;
@@ -6871,13 +6871,13 @@ PDFDeviceDriver(pDevDesc dd, const char *file, const char *paper,
68716871
/* 'file' could be NULL */
68726872
if(file && strlen(file) > R_PATH_MAX - 1) {
68736873
/* not yet created PDFcleanup(0, pd); */
6874-
free(dd);
6874+
GEfreeDD(dd);
68756875
error(_("filename too long in %s()"), "pdf");
68766876
}
68776877

68786878
/* allocate new PDF device description */
68796879
if (!(pd = (PDFDesc *) malloc(sizeof(PDFDesc)))) {
6880-
free(dd);
6880+
GEfreeDD(dd);
68816881
error(_("memory allocation problem in %s()"), "pdf");
68826882
}
68836883
/* from here on, if need to bail out with "error", must also
@@ -6897,15 +6897,15 @@ PDFDeviceDriver(pDevDesc dd, const char *file, const char *paper,
68976897
pd->pos = (int *) calloc(pd->max_nobjs, sizeof(int));
68986898
if(!pd->pos) {
68996899
PDFcleanup(1, pd);
6900-
free(dd);
6900+
GEfreeDD(dd);
69016901
error("cannot allocate pd->pos");
69026902
}
69036903
/* This one is dynamic: initial allocation */
69046904
pd->pagemax = 100;
69056905
pd->pageobj = (int *) calloc(pd->pagemax, sizeof(int));
69066906
if(!pd->pageobj) {
69076907
PDFcleanup(2, pd);
6908-
free(dd);
6908+
GEfreeDD(dd);
69096909
error("cannot allocate pd->pageobj");
69106910
}
69116911

@@ -6944,7 +6944,7 @@ PDFDeviceDriver(pDevDesc dd, const char *file, const char *paper,
69446944

69456945
if(strlen(encoding) > R_PATH_MAX - 1) {
69466946
PDFcleanup(3, pd);
6947-
free(dd);
6947+
GEfreeDD(dd);
69486948
error(_("encoding path is too long in %s()"), "pdf");
69496949
}
69506950
/*
@@ -6960,7 +6960,7 @@ PDFDeviceDriver(pDevDesc dd, const char *file, const char *paper,
69606960
pd->encodings = enclist;
69616961
} else {
69626962
PDFcleanup(3, pd);
6963-
free(dd);
6963+
GEfreeDD(dd);
69646964
error(_("failed to load default encoding"));
69656965
}
69666966

@@ -7027,7 +7027,7 @@ PDFDeviceDriver(pDevDesc dd, const char *file, const char *paper,
70277027
}
70287028
if (!gotFont) {
70297029
PDFcleanup(4, pd);
7030-
free(dd);
7030+
GEfreeDD(dd);
70317031
error(_("failed to initialise default PDF font"));
70327032
}
70337033

@@ -7082,7 +7082,7 @@ PDFDeviceDriver(pDevDesc dd, const char *file, const char *paper,
70827082
}
70837083
if (gotFonts < nfonts) {
70847084
PDFcleanup(4, pd);
7085-
free(dd);
7085+
GEfreeDD(dd);
70867086
error(_("failed to initialise additional PDF fonts"));
70877087
}
70887088
}
@@ -7095,14 +7095,14 @@ PDFDeviceDriver(pDevDesc dd, const char *file, const char *paper,
70957095
pd->rasters = initRasterArray(pd->maxRasters);
70967096
if (!pd->rasters) {
70977097
PDFcleanup(4, pd);
7098-
free(dd);
7098+
GEfreeDD(dd);
70997099
error(_("failed to allocate rasters"));
71007100
}
71017101
pd->numMasks = 0;
71027102
pd->masks = initMaskArray(pd->maxRasters);
71037103
if (!pd->masks) {
71047104
PDFcleanup(5, pd);
7105-
free(dd);
7105+
GEfreeDD(dd);
71067106
error(_("failed to allocate masks"));
71077107
}
71087108

@@ -7112,7 +7112,7 @@ PDFDeviceDriver(pDevDesc dd, const char *file, const char *paper,
71127112
initDefinitions(pd);
71137113
if (!pd->definitions) {
71147114
PDFcleanup(6, pd);
7115-
free(dd);
7115+
GEfreeDD(dd);
71167116
error(_("failed to allocate definitions"));
71177117
}
71187118
pd->appendingPath = -1;
@@ -7185,7 +7185,7 @@ PDFDeviceDriver(pDevDesc dd, const char *file, const char *paper,
71857185
char errbuf[strlen(pd->papername) + 1];
71867186
strcpy(errbuf, pd->papername);
71877187
PDFcleanup(7, pd);
7188-
free(dd);
7188+
GEfreeDD(dd);
71897189
error(_("invalid paper type '%s' (pdf)"), errbuf);
71907190
}
71917191
pd->pagecentre = pagecentre;
@@ -7209,7 +7209,7 @@ PDFDeviceDriver(pDevDesc dd, const char *file, const char *paper,
72097209
pointsize = floor(ps);
72107210
if(R_TRANSPARENT(setbg) && R_TRANSPARENT(setfg)) {
72117211
PDFcleanup(7, pd);
7212-
free(dd);
7212+
GEfreeDD(dd);
72137213
error(_("invalid foreground/background color (pdf)"));
72147214
}
72157215

@@ -8210,7 +8210,7 @@ static void PDF_Open(pDevDesc dd, PDFDesc *pd)
82108210
pd->mainfp = R_fopen(R_ExpandFileName(buf), "wb");
82118211
if (!pd->mainfp) {
82128212
PDFcleanup(7, pd);
8213-
free(dd);
8213+
GEfreeDD(dd);
82148214
error(_("cannot open file '%s'"), buf);
82158215
}
82168216
pd->pdffp = pd->mainfp;
@@ -9976,7 +9976,7 @@ SEXP PostScript(SEXP args)
99769976
R_CheckDeviceAvailable();
99779977
BEGIN_SUSPEND_INTERRUPTS {
99789978
pDevDesc dev;
9979-
if (!(dev = (pDevDesc) calloc(1, sizeof(DevDesc))))
9979+
if (!(dev = GEcreateDD()))
99809980
return 0;
99819981
if(!PSDeviceDriver(dev, file, paper, family, afms, encoding, bg, fg,
99829982
width, height, (bool)horizontal, ps, onefile,
@@ -10086,7 +10086,7 @@ SEXP PDF(SEXP args)
1008610086
R_CheckDeviceAvailable();
1008710087
BEGIN_SUSPEND_INTERRUPTS {
1008810088
pDevDesc dev;
10089-
if (!(dev = (pDevDesc) calloc(1, sizeof(DevDesc))))
10089+
if (!(dev = GEcreateDD()))
1009010090
return 0;
1009110091
if(!PDFDeviceDriver(dev, file, paper, family, afms, encoding, bg, fg,
1009210092
width, height, ps, onefile, pagecentre,

src/library/grDevices/src/devPicTeX.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,12 +772,12 @@ SEXP PicTeX(SEXP args)
772772
R_CheckDeviceAvailable();
773773
BEGIN_SUSPEND_INTERRUPTS {
774774
pDevDesc dev;
775-
if (!(dev = (pDevDesc) calloc(1, sizeof(DevDesc))))
775+
if (!(dev = GEcreateDD()))
776776
error(_("unable to start %s() device"), "pictex");
777777
// return 0; // that is not a SEXP
778778
if(!PicTeXDeviceDriver(dev, file, bg, fg, width, height,
779779
(bool) debug)) {
780-
free(dev);
780+
GEfreeDD(dev);
781781
error(_("unable to start %s() device"), "pictex");
782782
}
783783
dd = GEcreateDevDesc(dev);

src/library/grDevices/src/devQuartz.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3247,16 +3247,15 @@ Quartz_C(QuartzParameters_t *par, quartz_create_fn_t q_create, int *errorCode)
32473247
R_CheckDeviceAvailable();
32483248
{
32493249
const char *devname = "quartz_off_screen";
3250-
/* FIXME: check this allocation */
3251-
pDevDesc dev = calloc(1, sizeof(DevDesc));
3250+
pDevDesc dev = GEcreateDD();
32523251

32533252
if (!dev) {
32543253
if (errorCode) errorCode[0] = -2;
32553254
return NULL;
32563255
}
32573256
if (!(qd = q_create(dev, &qfn, par))) {
32583257
vmaxset(vmax);
3259-
free(dev);
3258+
GEfreeDD(dev);
32603259
if (errorCode) errorCode[0] = -3;
32613260
return NULL;
32623261
}
@@ -3358,7 +3357,7 @@ SEXP Quartz(SEXP args)
33583357
R_GE_checkVersionOrDie(R_GE_version);
33593358
R_CheckDeviceAvailable();
33603359
BEGIN_SUSPEND_INTERRUPTS {
3361-
pDevDesc dev = calloc(1, sizeof(DevDesc));
3360+
pDevDesc dev = GEcreateDD();
33623361

33633362
if (!dev)
33643363
error(_("unable to create device description"));
@@ -3410,7 +3409,7 @@ SEXP Quartz(SEXP args)
34103409

34113410
if (qd == NULL) {
34123411
vmaxset(vmax);
3413-
free(dev);
3412+
GEfreeDD(dev);
34143413
error(_("unable to create quartz() device target, given type may not be supported"));
34153414
}
34163415
const char *devname = "quartz_off_screen";

0 commit comments

Comments
 (0)