Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do ternary duplication if input is read-only #7172

Merged
merged 2 commits into from
Dec 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/gmt_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -9086,7 +9086,6 @@ struct GMT_MATRIX * gmtlib_duplicate_matrix (struct GMT_CTRL *GMT, struct GMT_MA
void gmtlib_free_matrix_ptr (struct GMT_CTRL *GMT, struct GMT_MATRIX *M, bool free_matrix) {
/* Free everything but the struct itself */
struct GMT_MATRIX_HIDDEN *MH = NULL;
enum GMT_enum_alloc alloc_mode;
if (!M) return; /* Nothing to deallocate */
/* Only free M->data if allocated by GMT AND free_matrix is true */
MH = gmt_get_M_hidden (M);
Expand All @@ -9102,7 +9101,6 @@ void gmtlib_free_matrix_ptr (struct GMT_CTRL *GMT, struct GMT_MATRIX *M, bool fr
for (unsigned int k = 0; k < M->n_headers; k++) gmt_M_str_free (M->header[k]);
gmt_M_free (GMT, M->header);
}
alloc_mode = MH->alloc_mode;
gmt_M_free (GMT, M->hidden);
}

Expand Down
18 changes: 17 additions & 1 deletion src/psternary.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,8 @@ EXTERN_MSC int GMT_psternary (void *V_API, int mode, void *args) {
struct PSTERNARY_CTRL *Ctrl = NULL;
struct GMT_CTRL *GMT = NULL, *GMT_cpy = NULL; /* General GMT internal parameters */
struct GMT_OPTION *options = NULL, *boptions[3] = {NULL, NULL, NULL};
struct GMT_DATASET *D = NULL; /* Pointer to GMT multisegment table(s) */
struct GMT_DATASET *D = NULL; /* Pointer to GMT multisegment table(s) after conversion to x,y */
struct GMT_DATASET_HIDDEN *DH = NULL;
struct GMT_DATASEGMENT *S = NULL;
struct PSL_CTRL *PSL = NULL; /* General PSL internal parameters */
struct GMTAPI_CTRL *API = gmt_get_api_ptr (V_API); /* Cast from void to GMTAPI_CTRL pointer */
Expand Down Expand Up @@ -409,6 +410,21 @@ EXTERN_MSC int GMT_psternary (void *V_API, int mode, void *args) {
if ((D = GMT_Read_Data (API, GMT_IS_DATASET, GMT_IS_FILE, 0, GMT_READ_NORMAL, NULL, NULL, NULL)) == NULL)
Return (API->error);

DH = gmt_get_DD_hidden (D);
if (DH->alloc_mode == GMT_ALLOC_EXTERNALLY) {
/* We duplicate input since we will modify it and move columns - not good if read-only data is our source */
struct GMT_DATASET *D2 = NULL;
if ((D2 = GMT_Duplicate_Data (API, GMT_IS_DATASET, GMT_DUPLICATE_DATA, D)) == NULL) {
GMT_Report (API, GMT_MSG_ERROR, "Unable to duplicate input dataset\n");
Return (API->error);
}
if (GMT_Destroy_Data (API, &D) != GMT_NOERROR) { /* No longer needed */
GMT_Report (API, GMT_MSG_ERROR, "Unable to destroy input dataset\n");
Return (API->error);
}
D = D2; /* Now use the internally allocated version instead */
}

if (GMT->common.J.active && GMT->common.J.string[1] == '-') /* Gave a negative width to reverse direction of axes */
reverse = true; /* Need to do this here given the abs_to_xy projection below */

Expand Down