Skip to content

Commit

Permalink
clearing and destroying XField arrays...
Browse files Browse the repository at this point in the history
  • Loading branch information
attipaci committed Dec 7, 2024
1 parent 267d794 commit cb52b2a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/xchange.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ void xClearStruct(XStructure *s);
XField *xCreateField(const char *name, XType type, int ndim, const int *sizes, const void *value);
XField *xCreateScalarField(const char *name, XType type, const void *value);
XField *xCopyOfField(const XField *f);
void xClearField(XField *f);
void xDestroyField(XField *f);
XField *xGetField(const XStructure *s, const char *name);
XField *xSetField(XStructure *s, XField *f);
Expand Down
31 changes: 27 additions & 4 deletions src/xstruct.c
Original file line number Diff line number Diff line change
Expand Up @@ -727,28 +727,51 @@ void xDestroyStruct(XStructure *s) {
}

/**
* Destroys an X structure field, freeing up resources used. For
* Clears an X structure field, freeing up all referfenced resources. However, the
* field itself is kept, but its contents are reset.
*
* \param f Pointer to the field to be destroyed.
* \param f Pointer to the field to be cleared.
*
* \sa xDestroyField()
*/
void xDestroyField(XField *f) {
void xClearField(XField *f) {
if(!f) return;

if(f->value) {

if(f->type == X_STRUCT) {
XStructure *sub = (XStructure *) f->value;
int i = xGetFieldCount(f);
while(--i >= 0) xClearStruct(&sub[i]);
}

if(f->type == X_FIELD) {
XField *array = (XField *) f->value;
int i;
for(i = xGetFieldCount(f); --i >=0;) xClearField(&array[i]);
}

free(f->value);
}

if(f->name != NULL) free(f->name);

free(f);
memset(f, 0, sizeof(XField));
}

/**
* Destroys an X structure field, freeing up all referenced resources, and
* destroying the field itself.
*
* \param f Pointer to the field to be destroyed.
*
* \sa xClearField()
*/
void xDestroyField(XField *f) {
if(!f) return;
xClearField(f);
free(f);
}

/**
* Destroys the contents of an X structure, leaving the structure empty.
Expand Down

0 comments on commit cb52b2a

Please sign in to comment.