/*================================================================ * ixmlElement_setTagName * Sets the given element's tagName. * Parameters: * tagName: new tagName for the element. * *=================================================================*/ int ixmlElement_setTagName( IN IXML_Element * element, IN char *tagName ) { int rc = IXML_SUCCESS; assert( ( element != NULL ) && ( tagName != NULL ) ); if( ( element == NULL ) || ( tagName == NULL ) ) { return IXML_FAILED; } if( element->tagName != NULL ) { #ifdef _UPNP_MM_ upnp_free( element->tagName ); #else free( element->tagName ); #endif } #ifdef _UPNP_MM_ element->tagName = upnp_strdup( tagName ); #else element->tagName = strdup( tagName ); #endif if( element->tagName == NULL ) { rc = IXML_INSUFFICIENT_MEMORY; } return rc; }
/*================================================================ * ixmlElement_removeAttribute * Removes an attribute value by name. The attribute node is * not removed. * External function. * Parameters: * name: the name of the attribute to remove. * Return Values: * IXML_SUCCESS or error code. * *=================================================================*/ int ixmlElement_removeAttribute( IN IXML_Element * element, IN char *name ) { IXML_Node *attrNode; if( ( element == NULL ) || ( name == NULL ) ) { return IXML_INVALID_PARAMETER; } attrNode = element->n.firstAttr; while( attrNode != NULL ) { if( strcmp( attrNode->nodeName, name ) == 0 ) { break; //found it } else { attrNode = attrNode->nextSibling; } } if( attrNode != NULL ) { // has the attribute if( attrNode->nodeValue != NULL ) { #ifdef _UPNP_MM_ upnp_free( attrNode->nodeValue ); #else free( attrNode->nodeValue ); #endif attrNode->nodeValue = NULL; } } return IXML_SUCCESS; }
/**************************************************************************** * Function: FreeListDestroy * * Description: * Releases the resources stored with the free list. * Parameters: * free_list - must be valid, non null, pointer to a linked list. * Returns: * 0 on success. Nonzero on failure. * Always returns 0. *****************************************************************************/ int FreeListDestroy( FreeList * free_list ) { FreeListNode *temp = NULL; int i = 0; assert( free_list != NULL ); if( free_list == NULL ) return EINVAL; while( free_list->head ) { i++; temp = free_list->head->next; #ifdef _UPNP_MM_ upnp_free( free_list->head ); #else free( free_list->head ); #endif free_list->head = temp; } free_list->freeListLength = 0; return 0; }
/**************************************************************************** * Function: FreeListFree * * Description: * Returns an item to the Free List. * If the free list is smaller than the max size than * adds the item to the free list. * Otherwise returns the item to the O.S. * Parameters: * free_list - must be valid, non null, pointer to a free list. * element - must be a pointer allocated by FreeListAlloc * Returns: * 0 on success. Nonzero on failure. * Always returns 0. *****************************************************************************/ int FreeListFree( FreeList * free_list, void *element ) { FreeListNode *temp = NULL; assert( free_list != NULL ); if( free_list == NULL ) return EINVAL; if( ( element != NULL ) && ( ( free_list->freeListLength + 1 ) < free_list->maxFreeListLength ) ) { free_list->freeListLength++; temp = ( FreeListNode * ) element; temp->next = free_list->head; free_list->head = temp; } else { #ifdef _UPNP_MM_ upnp_free( element ); #else free( element ); #endif } return 0; }
/************************************************************************ * Function : free_URL_list * * Parameters : * URL_list * list ; URL List object * * Description : Frees the memory associated with a URL_list. Frees the * dynamically allocated members of of list. Does NOT free the * pointer to the list itself ( i.e. does NOT free(list)) * * Return : void ; * * Note : ************************************************************************/ void free_URL_list( URL_list * list ) { if( list->URLs ) #ifdef _UPNP_MM_ upnp_free( list->URLs ); #else free( list->URLs ); #endif if( list->parsedURLs ) #ifdef _UPNP_MM_ upnp_free( list->parsedURLs ); #else free( list->parsedURLs ); #endif list->size = 0; }
void iasnprintfFree( char *fChar ) { #ifdef _UPNP_MM_ upnp_free( fChar ); #else free( fChar ); #endif fChar = NULL; }
/************************************************************************ * Function : send_search_result * * Parameters: * IN void *data: Search reply from the device * * Description: * This function sends a callback to the control point application with * a SEARCH result * * Returns: void * ***************************************************************************/ void send_search_result( IN void *data ) { ResultData *temp = ( ResultData * ) data; temp->ctrlpt_callback( UPNP_DISCOVERY_SEARCH_RESULT, &temp->param, temp->cookie ); #ifdef _UPNP_MM_ upnp_free( temp ); #else free( temp ); #endif }
// free static inline void fr() { int i; for ( i = 0 ; i < MAX_ALLOC ; i ++) { TotalFree ++; if (!my_alloc[i]) { NullFree ++; } upnp_free(my_alloc[i]); my_alloc[i] = NULL; } }
/*================================================================ * ixmlNamedNodeMap_free * frees a NamedNodeMap. * External function. * *=================================================================*/ void ixmlNamedNodeMap_free( IXML_NamedNodeMap * nnMap ) { IXML_NamedNodeMap *pNext; while( nnMap != NULL ) { pNext = nnMap->next; #ifdef _UPNP_MM_ upnp_free( nnMap ); #else free( nnMap ); #endif nnMap = pNext; } }
/************************************************************************ * Function : advertiseAndReplyThread * * Parameters: * IN void *data: Structure containing the search request * * Description: * This function is a wrapper function to reply the search request * coming from the control point. * * Returns: void * * always return NULL ***************************************************************************/ void * advertiseAndReplyThread( IN void *data ) { SsdpSearchReply *arg = ( SsdpSearchReply * ) data; AdvertiseAndReply( 0, arg->handle, arg->event.RequestType, &arg->dest_addr, arg->event.DeviceType, arg->event.UDN, arg->event.ServiceType, arg->MaxAge ); #ifdef _UPNP_MM_ upnp_free( arg ); #else free( arg ); #endif return NULL; }
int iasnprintf( char **ret, int incr, int max, const char *fmt, ... ) { int size = incr; int retc = 0; va_list ap; char *temp = NULL; assert( ret ); assert( fmt ); #ifdef _UPNP_MM_ ( *ret ) = ( char * )upnp_malloc( incr ); #else ( *ret ) = ( char * )malloc( incr ); #endif while( 1 ) { va_start( ap, fmt ); retc = vsnprintf( ( *ret ), size, fmt, ap ); va_end( ap ); if( retc < 0 ) { //size not big enough //and vsnprintf does NOT return the //necessary number of bytes if( ( max != -1 ) && ( size == max ) ) //max reached { break; } incr *= 2; //increase increment //increase size and try again if( ( max != -1 ) && ( ( size + incr ) > max ) ) { incr = ( max - size ); } #ifdef _UPNP_MM_ temp = ( char * )upnp_realloc( ( *ret ), size + incr ); #else temp = ( char * )realloc( ( *ret ), size + incr ); #endif if( temp == NULL ) { break; } size += incr; ( *ret ) = temp; } else { if( ( retc + 1 ) > size ) { //size not big enough //and vsnprintf //returns the necessary //number of bytes if( ( max != -1 ) && ( retc + 1 > max ) ) { retc = -1; break; } #ifdef _UPNP_MM_ temp = ( char * )upnp_realloc( ( *ret ), retc + 1 ); #else temp = ( char * )realloc( ( *ret ), retc + 1 ); #endif if( temp == NULL ) { retc = -1; break; } size = retc + 1; ( *ret ) = temp; //size increased try again } else if( ( retc + 1 ) < size ) { //size is bigger than needed //try and reallocate smaller #ifdef _UPNP_MM_ temp = ( char * )upnp_realloc( ( *ret ), retc + 1 ); #else temp = ( char * )realloc( ( *ret ), retc + 1 ); #endif if( temp != NULL ) { ( *ret ) = temp; } break; } else //size is just right, exit { break; } } } if( retc < 0 ) { #ifdef _UPNP_MM_ upnp_free( ( *ret ) ); #else free( ( *ret ) ); #endif ( *ret ) = NULL; } return retc; }
/*================================================================ * ixmlElement_setAttributeNS * Adds a new attribute. If an attribute with the same local name * and namespace URI is already present on the element, its prefix * is changed to be the prefix part of the qualifiedName, and its * value is changed to be the value parameter. This value is a * simple string. * External function. * * Parameter: * namespaceURI: the namespace of the attribute to create or alter. * qualifiedName: the qualified name of the attribute to create or alter. * value: the value to set in string form. * * Return Value: * IXML_SUCCESS or failure * *=================================================================*/ int ixmlElement_setAttributeNS( IN IXML_Element * element, IN DOMString namespaceURI, IN DOMString qualifiedName, IN DOMString value ) { IXML_Node *attrNode = NULL; IXML_Node newAttrNode; IXML_Attr *newAttr; int rc; if( ( element == NULL ) || ( namespaceURI == NULL ) || ( qualifiedName == NULL ) || ( value == NULL ) ) { return IXML_INVALID_PARAMETER; } if( Parser_isValidXmlName( qualifiedName ) == FALSE ) { return IXML_INVALID_CHARACTER_ERR; } ixmlNode_init( &newAttrNode ); #ifdef _UPNP_MM_ newAttrNode.nodeName = upnp_strdup( qualifiedName ); #else newAttrNode.nodeName = strdup( qualifiedName ); #endif if( newAttrNode.nodeName == NULL ) { return IXML_INSUFFICIENT_MEMORY; } rc = Parser_setNodePrefixAndLocalName( &newAttrNode ); if( rc != IXML_SUCCESS ) { Parser_freeNodeContent( &newAttrNode ); return rc; } // see DOM 2 spec page 59 if( ( newAttrNode.prefix != NULL && namespaceURI == NULL ) || ( strcmp( newAttrNode.prefix, "xml" ) == 0 && strcmp( namespaceURI, "http://www.w3.org/XML/1998/namespace" ) != 0 ) || ( strcmp( qualifiedName, "xmlns" ) == 0 && strcmp( namespaceURI, "http://www.w3.org/2000/xmlns/" ) != 0 ) ) { Parser_freeNodeContent( &newAttrNode ); return IXML_NAMESPACE_ERR; } attrNode = element->n.firstAttr; while( attrNode != NULL ) { if( strcmp( attrNode->localName, newAttrNode.localName ) == 0 && strcmp( attrNode->namespaceURI, namespaceURI ) == 0 ) { break; //found it } else { attrNode = attrNode->nextSibling; } } if( attrNode != NULL ) { if( attrNode->prefix != NULL ) { #ifdef _UPNP_MM_ upnp_free( attrNode->prefix ); // remove the old prefix #else free( attrNode->prefix ); // remove the old prefix #endif } // replace it with the new prefix #ifdef _UPNP_MM_ attrNode->prefix = upnp_strdup( newAttrNode.prefix ); #else attrNode->prefix = strdup( newAttrNode.prefix ); #endif if( attrNode->prefix == NULL ) { Parser_freeNodeContent( &newAttrNode ); return IXML_INSUFFICIENT_MEMORY; } if( attrNode->nodeValue != NULL ) { #ifdef _UPNP_MM_ upnp_free( attrNode->nodeValue ); #else free( attrNode->nodeValue ); #endif } #ifdef _UPNP_MM_ attrNode->nodeValue = upnp_strdup( value ); #else attrNode->nodeValue = strdup( value ); #endif if( attrNode->nodeValue == NULL ) { #ifdef _UPNP_MM_ upnp_free( attrNode->prefix ); #else free( attrNode->prefix ); #endif Parser_freeNodeContent( &newAttrNode ); return IXML_INSUFFICIENT_MEMORY; } } else { // add a new attribute rc = ixmlDocument_createAttributeNSEx( ( IXML_Document * ) element->n.ownerDocument, namespaceURI, qualifiedName, &newAttr ); if( rc != IXML_SUCCESS ) { return rc; } #ifdef _UPNP_MM_ newAttr->n.nodeValue = upnp_strdup( value ); #else newAttr->n.nodeValue = strdup( value ); #endif if( newAttr->n.nodeValue == NULL ) { ixmlAttr_free( newAttr ); return IXML_INSUFFICIENT_MEMORY; } if( ixmlElement_setAttributeNodeNS( element, newAttr, NULL ) != IXML_SUCCESS ) { ixmlAttr_free( newAttr ); return IXML_FAILED; } } Parser_freeNodeContent( &newAttrNode ); return IXML_SUCCESS; }
/*================================================================ * ixmlElement_setAttribute * Adds a new attribute. If an attribute with that name is already * present in the element, its value is changed to be that of the value * parameter. If not, a new attribute is inserted into the element. * * External function. * Parameters: * name: the name of the attribute to create or alter. * value: value to set in string form * Return Values: * IXML_SUCCESS or failure code. * *=================================================================*/ int ixmlElement_setAttribute( IN IXML_Element * element, IN char *name, IN char *value ) { IXML_Node *attrNode; IXML_Attr *newAttrNode; short errCode = IXML_SUCCESS; if( ( element == NULL ) || ( name == NULL ) || ( value == NULL ) ) { errCode = IXML_INVALID_PARAMETER; goto ErrorHandler; } if( Parser_isValidXmlName( name ) == FALSE ) { errCode = IXML_INVALID_CHARACTER_ERR; goto ErrorHandler; } attrNode = element->n.firstAttr; while( attrNode != NULL ) { if( strcmp( attrNode->nodeName, name ) == 0 ) { break; //found it } else { attrNode = attrNode->nextSibling; } } if( attrNode == NULL ) { // add a new attribute errCode = ixmlDocument_createAttributeEx( ( IXML_Document * ) element->n. ownerDocument, name, &newAttrNode ); if( errCode != IXML_SUCCESS ) { goto ErrorHandler; } attrNode = ( IXML_Node * ) newAttrNode; #ifdef _UPNP_MM_ attrNode->nodeValue = upnp_strdup( value ); #else attrNode->nodeValue = strdup( value ); #endif if( attrNode->nodeValue == NULL ) { ixmlAttr_free( newAttrNode ); errCode = IXML_INSUFFICIENT_MEMORY; goto ErrorHandler; } errCode = ixmlElement_setAttributeNode( element, newAttrNode, NULL ); if( errCode != IXML_SUCCESS ) { ixmlAttr_free( newAttrNode ); goto ErrorHandler; } } else { if( attrNode->nodeValue != NULL ) { // attribute name has a value already #ifdef _UPNP_MM_ upnp_free( attrNode->nodeValue ); #else free( attrNode->nodeValue ); #endif } #ifdef _UPNP_MM_ attrNode->nodeValue = upnp_strdup( value ); #else attrNode->nodeValue = strdup( value ); #endif if( attrNode->nodeValue == NULL ) { errCode = IXML_INSUFFICIENT_MEMORY; } } ErrorHandler: return errCode; }
static inline void ran() { /* random test */ int i, j, r; int total_free; SUBTITLE(random); for (i = 0 ; i < MAX_ALLOC ; i ++) { if (my_alloc[i]) { printf("Warning! allocate buffer[%d] is not clear !\n", i); upnp_free(my_alloc[i]); my_alloc[i] = NULL; } } /* start random free/alloc phase */ total_free = 0; while (total_free < MAX_ALLOC) { r = (random() % MAX_ALLOC); if (my_alloc[r]) { total_free ++; upnp_free(my_alloc[r]); my_alloc[r] = NULL; }else { j = (random() % 4); switch(j) { case 0: /* alloc */ my_alloc[r] = al(); break; case 1: /* calloc */ my_alloc[r] = cal(); break; case 2: /* strdup */ my_alloc[r] = sdu(); break; default: /* realloc */ my_alloc[r] = real(); break; } } } /* free phase */ for (i = 0 ; i < MAX_ALLOC ; i ++) { if (my_alloc[i]) { upnp_free(my_alloc[i]); my_alloc[i] = NULL; } } }
// strdup static void * sdu() { int size = 0; char *temp = NULL; char *retptr = NULL; while (!size) { size = (random() % MAX_ALLOCATE_SIZE); } TotalAlloc ++; TotalAllocSize += size; temp = (char *)upnp_malloc(size); if (!temp) { if (size) { printf("allocate Warning: out of memory (size : %d)\n", size); upnp_mm_dump(); NullAlloc ++; } return NULL; } bzero(temp, size); memset((void *)temp, 'A', (size - 1)); TotalAlloc ++; retptr = upnp_strdup(temp); upnp_free(temp); /* check phase */ if (retptr) { char *ptr = retptr; int i; int slen = strlen(retptr); TotalAllocSize += size; if (slen != (size - 1)) { printf("strdup error ! duplicated string length : %d [expected: %d]\n", slen, (size - 1)); upnp_mm_dump(); exit(1); } if (slen) { for (i = 0 ; i < slen ; i ++, ptr ++) { if ((*ptr) != 'A') { printf("strdup error ! duplicated character error : %c [expected: %c]\n", *ptr, 'A'); upnp_mm_dump(); exit(1); } } } }else { printf("strdup Warning: out of memory (size : %d)\n", size); upnp_mm_dump(); NullAlloc ++; } return retptr; }
/************************************************************************ * Function : searchExpired * * Parameters: * IN void * arg: * * Description: * This function * * Returns: void * ***************************************************************************/ void searchExpired( void *arg ) { int *id = ( int * )arg; int handle = -1; struct Handle_Info *ctrlpt_info = NULL; //remove search Target from list and call client back ListNode *node = NULL; SsdpSearchArg *item; Upnp_FunPtr ctrlpt_callback; void *cookie = NULL; int found = 0; HandleLock( ); //remove search target from search list if( GetClientHandleInfo( &handle, &ctrlpt_info ) != HND_CLIENT ) { #ifdef _UPNP_MM_ upnp_free( id ); #else free( id ); #endif HandleUnlock( ); return; } ctrlpt_callback = ctrlpt_info->Callback; node = ListHead( &ctrlpt_info->SsdpSearchList ); while( node != NULL ) { item = ( SsdpSearchArg * ) node->item; if( item->timeoutEventId == ( *id ) ) { #ifdef _UPNP_MM_ upnp_free( item->searchTarget ); #else free( item->searchTarget ); #endif cookie = item->cookie; found = 1; item->searchTarget = NULL; #ifdef _UPNP_MM_ upnp_free( item ); #else free( item ); #endif ListDelNode( &ctrlpt_info->SsdpSearchList, node, 0 ); break; } node = ListNext( &ctrlpt_info->SsdpSearchList, node ); } HandleUnlock( ); if( found ) { ctrlpt_callback( UPNP_DISCOVERY_SEARCH_TIMEOUT, NULL, cookie ); } #ifdef _UPNP_MM_ upnp_free( id ); #else free( id ); #endif }