Skip to content

Do not adjust externally allocated segments #7171

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

Merged
merged 1 commit into from
Dec 24, 2022
Merged
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
16 changes: 13 additions & 3 deletions src/gmt_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -3078,17 +3078,27 @@ GMT_LOCAL void gmtio_adjust_segment (struct GMT_CTRL *GMT, struct GMT_DATASEGMEN
/* Change the number of columns in this segment to n_columns (free or allocate as needed) */
uint64_t col;
struct GMT_DATASEGMENT_HIDDEN *SH = gmt_get_DS_hidden (S);
for (col = n_columns; col < S->n_columns; col++) gmt_M_free (GMT, S->data[col]); /* Free up if n_columns < S->columns */
for (col = n_columns; col < S->n_columns; col++) { /* Free up if n_columns < S->columns if allowed */
if (SH->alloc_mode[col] == GMT_ALLOC_INTERNALLY)
gmt_M_free (GMT, S->data[col]);
else
S->data[col] = NULL; /* Let go of link to an external vector */
}
/* Reallocate number of columns (increase or decrease lengths) */
S->data = gmt_M_memory (GMT, S->data, n_columns, double *);
S->min = gmt_M_memory (GMT, S->min, n_columns, double);
S->max = gmt_M_memory (GMT, S->max, n_columns, double);
S->min = gmt_M_memory (GMT, S->min, n_columns, double);
S->max = gmt_M_memory (GMT, S->max, n_columns, double);
SH->alloc_mode = gmt_M_memory (GMT, SH->alloc_mode, n_columns, enum GMT_enum_alloc);
for (col = S->n_columns; col < n_columns; col++) { /* Allocate new columns and initialize the min/max arrays */
S->min[col] = +DBL_MAX;
S->max[col] = -DBL_MAX;
S->data[col] = gmt_M_memory (GMT, NULL, S->n_rows, double);
SH->alloc_mode[col] = GMT_ALLOC_INTERNALLY;
}
/* Change the number of known/active columns to reflect the changes.
* NOTE: If columns were externally allocated they are not freed (which is good)
* but they are also "lost" in the sense they are no longer accessible as the
* data pointer has been shrunk and set to NULL. */
S->n_columns = n_columns;
}

Expand Down