Skip to content

Commit

Permalink
use $CROAK(), plAlloc2dGrid to check malloc worked
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Apr 21, 2024
1 parent f95e3a2 commit bd9e97d
Showing 1 changed file with 25 additions and 39 deletions.
64 changes: 25 additions & 39 deletions plplot.pd
Original file line number Diff line number Diff line change
Expand Up @@ -3694,7 +3694,7 @@ pp_def ('plParseOpts',
newargc = argc = av_len (arr) + 1;
if (argc > 0) {
Newx(args, argc, char *);
if(args == NULL) croak("Failed to allocate memory in plParseOpts");
if(args == NULL) $CROAK("Failed to allocate memory in plParseOpts");

for (i = 0; i < argc; i++) {
STRLEN len;
Expand Down Expand Up @@ -3875,26 +3875,19 @@ pp_def ('plAllocGrid',
Doc => 'Allocates a PLcGrid object for use in pltr1',
Code => '
PLcGrid *grid;
int i, nx, ny;

nx = $SIZE(nx);
ny = $SIZE(ny);

int i, nx = $SIZE(nx), ny = $SIZE(ny);
Newx(grid, 1, PLcGrid);
if(grid == NULL) croak("Failed to allocate memory for grid");
if(grid == NULL) $CROAK("Failed to allocate memory for grid");
Newxz(grid->xg, nx, PLFLT);
if(grid->xg == NULL) croak("Failed to allocate memory for grid->xg");
if(grid->xg == NULL) $CROAK("Failed to allocate memory for grid->xg");
Newxz(grid->yg, ny, PLFLT);
if(grid->yg == NULL) croak("Failed to allocate memory for grid->yg");
if(grid->yg == NULL) $CROAK("Failed to allocate memory for grid->yg");
grid->nx = nx;
grid->ny = ny;

for (i = 0; i < nx; i++)
grid->xg[i] = $xg(nx => i);

for (i = 0; i < ny; i++)
grid->yg[i] = $yg(ny => i);

$grid() = (IV) grid;'
);

Expand All @@ -3920,25 +3913,18 @@ pp_def ('plAlloc2dGrid',
GenericTypes => [D],
Doc => 'Allocates a PLcGrid2 object for use in pltr2',
Code => '
PLcGrid2 *grid;
int i, j, nx, ny;

nx = $SIZE(nx);
ny = $SIZE(ny);

grid = (PLcGrid2*) malloc (sizeof (PLcGrid2));
plAlloc2dGrid (&(grid->xg), nx, ny);
plAlloc2dGrid (&(grid->yg), nx, ny);

int i, j, nx = $SIZE(nx), ny = $SIZE(ny);
PLcGrid2 *grid = (PLcGrid2*) malloc(sizeof(PLcGrid2));
if (!grid) $CROAK("Failed to allocate memory for grid");
plAlloc2dGrid(&(grid->xg), nx, ny);
plAlloc2dGrid(&(grid->yg), nx, ny);
for (i = 0; i < nx; i++)
for (j = 0; j < ny; j++) {
grid->xg[i][j] = $xg(nx => i, ny => j);
grid->yg[i][j] = $yg(nx => i, ny => j);
}

grid->nx = nx;
grid->ny = ny;

$grid() = (IV) grid;'
);

Expand All @@ -3947,13 +3933,13 @@ pp_def ('plAlloc2dGrid',

pp_addxs (<<"EOC");
void
plFree2dGrid (pg)
plFree2dGrid(pg)
long pg
PPCODE:
PLcGrid2* grid = (PLcGrid2*) pg;
plFree2dGrid (grid->xg, grid->nx, grid->ny);
plFree2dGrid (grid->yg, grid->nx, grid->ny);
free (grid);
plFree2dGrid(grid->xg, grid->nx, grid->ny);
plFree2dGrid(grid->yg, grid->nx, grid->ny);
free(grid);
EOC

pp_add_exported (plFree2dGrid);
Expand Down Expand Up @@ -4078,7 +4064,7 @@ pp_def ('plshades',

pltrcb = get_standard_pltrcb ($COMP(pltr));
if (pltrcb != pltr_callback)
pltrdt = (PLPointer) SvIV ($COMP(pltr_data));
pltrdt = (PLPointer) SvIV($COMP(pltr_data));
else
pltrdt = $COMP(pltr_data);

Expand Down Expand Up @@ -4118,7 +4104,7 @@ pp_def ('plcont',

pltrcb = get_standard_pltrcb ($COMP(pltr));
if (pltrcb != pltr_callback)
pltrdt = (PLPointer) SvIV ($COMP(pltr_data));
pltrdt = (PLPointer) SvIV($COMP(pltr_data));
else
pltrdt = $COMP(pltr_data);

Expand Down Expand Up @@ -4237,7 +4223,7 @@ pp_def ('plscmap1l',
else if ($SIZE(nrev) == $SIZE(n))
rev = $P(rev);
else
croak ("plscmap1l: rev must have either length == 0 or have the same length of the other input arguments");
$CROAK("plscmap1l: rev must have either length == 0 or have the same length of the other input arguments");

c_plscmap1l ($itype(), $SIZE(n), $P(isty), $P(coord1),
$P(coord2), $P(coord3), rev);'
Expand All @@ -4263,7 +4249,7 @@ pp_def ('plshade1',
size_y = $SIZE(ny);

plAlloc2dGrid (&a, size_x, size_y);
if(a == NULL) croak("Failed to allocate memory in plshade1_pp");
if(a == NULL) $CROAK("Failed to allocate memory in plshade1_pp");

for (i = 0; i < size_x; i++)
for (j = 0; j < size_y; j++)
Expand All @@ -4277,7 +4263,7 @@ pp_def ('plshade1',

pltrcb = get_standard_pltrcb ($COMP(pltr));
if (pltrcb != pltr_callback)
pltrdt = (PLPointer) SvIV ($COMP(pltr_data));
pltrdt = (PLPointer) SvIV($COMP(pltr_data));
else
pltrdt = $COMP(pltr_data);

Expand Down Expand Up @@ -4341,7 +4327,7 @@ pp_def ('plimagefr',

pltrcb = get_standard_pltrcb ($COMP(pltr));
if (pltrcb != pltr_callback)
pltrdt = (PLPointer) SvIV ($COMP(pltr_data));
pltrdt = (PLPointer) SvIV($COMP(pltr_data));
else
pltrdt = $COMP(pltr_data);

Expand Down Expand Up @@ -4601,15 +4587,15 @@ pp_def ('plstripc',

if (! SvROK (sv_legline)
|| SvTYPE (SvRV (sv_legline)) != SVt_PVAV)
croak ("plstripc: legline must be a reference to an array");
$CROAK("plstripc: legline must be a reference to an array");

av_legline = (AV*) SvRV (sv_legline);

if (av_len (av_legline) != 3)
croak ("plstripc: legline must have four elements");
$CROAK("plstripc: legline must have four elements");

if ($SIZE(n) != 4)
croak ("plstripc: colline and styline must have four elements");
$CROAK("plstripc: colline and styline must have four elements");

for (i = 0; i < 4; i++) {
SV** elem = av_fetch (av_legline, i, 0);
Expand Down Expand Up @@ -5197,7 +5183,7 @@ pp_def ('plvect',

pltrcb = get_standard_pltrcb ($COMP(pltr));
if (pltrcb != pltr_callback)
pltrdt = (PLPointer) SvIV ($COMP(pltr_data));
pltrdt = (PLPointer) SvIV($COMP(pltr_data));
else
pltrdt = $COMP(pltr_data);

Expand Down Expand Up @@ -5300,7 +5286,7 @@ if (!$noalpha) {
else if ($SIZE(nrev) == $SIZE(n))
rev = $P(rev);
else
croak ("plscmap1la: rev must have either length == 0 or have the same length of the other input arguments");
$CROAK("plscmap1la: rev must have either length == 0 or have the same length of the other input arguments");

c_plscmap1la ($itype(), $SIZE(n), $P(isty), $P(coord1),
$P(coord2), $P(coord3), $P(coord4), rev);'
Expand Down

0 comments on commit bd9e97d

Please sign in to comment.