Skip to content

Commit

Permalink
Add new files needed for thread-safe HDS-v5
Browse files Browse the repository at this point in the history
  • Loading branch information
David Berry committed Jul 17, 2017
1 parent f71f261 commit 8888a46
Show file tree
Hide file tree
Showing 8 changed files with 1,282 additions and 0 deletions.
168 changes: 168 additions & 0 deletions dat1EraseHandle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
*+
* Name:
* dat1EraseHandle
* Purpose:
* Recursively erase a handle structure for a component of an HDF object.
* Language:
* Starlink ANSI C
* Type of Module:
* Library routine
* Invocation:
* Handle *dat1EraseHandle( Handle *parent, const char *name, int *status );
* Arguments:
* parent = Handle * (Given)
* Pointer to a Handle that 1) contains the Handle to be erase
* (if "name" is not NULL), or 2) is the Handle to be erased (if
* "name" is NULL).
* name = const char * (Given)
* The name of the HDF component within "parent" that is to be
* erased. An error is reported if the named compoent cannot be
* found.If NULL is supplied, the handle specified by "parent" is
* itself erased.
* status = int* (Given and Returned)
* Pointer to global status.
* Returned function value:
A NULL pointer is always returned.
* Description:
* The named component, and all its sub-components, is erased from the
* supplied parent handle.
* Notes:
* - This function attempts to execure even if an error has already
* occurred.
* Authors:
* DSB: David S Berry (EAO)
* {enter_new_authors_here}
* History:
* 5-JUL-2017 (DSB):
* Initial version
* {enter_further_changes_here}
* Copyright:
* Copyright (C) 2014 Cornell University
* All Rights Reserved.
* Licence:
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* - Neither the name of the {organization} nor the names of its
* contributors may be used to endorse or promote products
* derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
* Bugs:
* {note_any_bugs_here}
*-
*/

#include <string.h>

#include "ems.h"
#include "sae_par.h"
#include "dat1.h"
#include "dat_err.h"

Handle *dat1EraseHandle( Handle *parent, const char *name, int * status ){

/* Local Variables; */
Handle *comp = NULL;
Handle *child = NULL;
int ichild;

/* Return immediately no parent is supplied. */
if( !parent ) return NULL;

/* Find the handle to be erased - either the named component in the
parent, or the parent itself. */
if( name ) {
for( ichild = 0; ichild < parent->nchild; ichild++ ) {
child = parent->children[ ichild ];
if( child && child->name && !strcmp( child->name, name ) ) {
comp = child;

/* We will be erasing this handle ("child") below. The parent will therefore
no longer have this handle as a child. So remove it from the parent's list
of children by storing a NULL pointer. */
parent->children[ ichild ] = NULL;
break;
}
}

} else {
comp = parent;
}

/* Report an error if the named component was not found. */
if( !comp ) {
if( *status == SAI__OK ) {
*status = DAT__FATAL;
if( parent->name ) {
emsRepf( " ", "Failed to find handle for component '%s' within "
"parent '%s' (programming error).", status, name,
parent->name );
} else {
emsRepf( " ", "Failed to find handle for component '%s' within "
"top-level parent (programming error).", status, name );
}
}

/* Otherwise, attempt to erase all children in the component. */
} else {
for( ichild = 0; ichild < comp->nchild; ichild++ ) {
child = comp->children[ ichild ];
if( child ) {
if( child->name ){
dat1EraseHandle( child, NULL, status );
} else {
if( *status == SAI__OK ) {
*status = DAT__FATAL;
emsRepf( " ", "Child handle found with no name inside "
"parent '%s' (programming error).", status, name );
}
}
}
}

/* Then erase the child itself. */
comp = dat1FreeHandle( comp );
}

return NULL;

}


112 changes: 112 additions & 0 deletions dat1FreeHandle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
*+
* Name:
* dat1FreeHandle
* Purpose:
* Free resources used by a handle structure.
* Language:
* Starlink ANSI C
* Type of Module:
* Library routine
* Invocation:
* Handle *dat1FreeHandle( Handle *handle );
* Arguments:
* handle = HDSLoc * (Given)
* Pointer to the Handle to be freed. This function returns without
* action if NULL is supplied.
* Returned function value:
* A NULL pointer is always returned.
* Description:
* The memory used by the supplied Handle is freed, and a NULL pointer
* returned.
* Notes:
* - This function will attempt to execute even if an error has
* already occurred.
* Authors:
* DSB: David S Berry (EAO)
* {enter_new_authors_here}
* History:
* 5-JUL-2017 (DSB):
* Initial version
* {enter_further_changes_here}
* Copyright:
* Copyright (C) 2014 Cornell University
* All Rights Reserved.
* Licence:
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* - Neither the name of the {organization} nor the names of its
* contributors may be used to endorse or promote products
* derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
* Bugs:
* {note_any_bugs_here}
*-
*/

#include <pthread.h>
#include <string.h>
#include "dat1.h"
#include "ems.h"

Handle *dat1FreeHandle( Handle *handle ) {

/* Return immediately if no Handle was supplied. */
if( !handle ) return NULL;

/* Free the memory used by components of the Handle structure. */
if( handle->name ) MEM_FREE( handle->name );
if( handle->children ) MEM_FREE( handle->children );

/* Destroy the mutex */
pthread_mutex_destroy( &(handle->mutex2) );

/* Fill the handles with zeros in case any other points to the same
handle exist. */
memset( handle, 0, sizeof(*handle) );

/* Free the memory used by the Handle structure itself. */
MEM_FREE( handle );

/* Return a NULL pointer. */
return NULL;
}


Loading

0 comments on commit 8888a46

Please sign in to comment.