📄 membuffer.c
字号:
/////////////////////////////////////////////////////////////////////////////// Copyright (c) 2000-2003 Intel Corporation // All rights reserved. //// 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 name of Intel Corporation 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 INTEL 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.//////////////////////////////////////////////////////////////////////////////************************************************************************* Purpose: This file contains functions that operate on memory and * buffers, allocation, re-allocation, and modification of the memory ************************************************************************/#include <assert.h>#include <stdlib.h>#include <string.h>#include <membuffer.h>#include "upnp.h"/************************************************************************* string *************************************************************************//************************************************************************* Function : str_alloc** Parameters :* IN const char* str ; input string object* IN size_t str_len ; input string length** Description : Allocate memory and copy information from the input * string to the newly allocated memory.** Return : char* ;* Pointer to the newly allocated memory.* NULL if memory cannot be allocated.** Note :************************************************************************/char *str_alloc( IN const char *str, IN size_t str_len ){ char *s; s = ( char * )malloc( str_len + 1 ); if( s == NULL ) { return NULL; // no mem } memcpy( s, str, str_len ); s[str_len] = '\0'; return s;}/************************************************************************* memptr *************************************************************************//************************************************************************* Function : memptr_cmp** Parameters :* IN memptr* m ; input memory object* IN const char* s ; constatnt string for the memory object to be * compared with** Description : Compares characters of strings passed for number of * bytes. If equal for the number of bytes, the length of the bytes * determines which buffer is shorter.** Return : int ;* < 0 string1 substring less than string2 substring * 0 string1 substring identical to string2 substring * > 0 string1 substring greater than string2 substring ** Note :************************************************************************/intmemptr_cmp( IN memptr * m, IN const char *s ){ int cmp; cmp = strncmp( m->buf, s, m->length ); if( cmp == 0 && m->length < strlen( s ) ) { // both strings equal for 'm->length' chars // if m is shorter than s, then s is greater return -1; } return cmp;}/************************************************************************* Function : memptr_cmp_nocase** Parameters :* IN memptr* m ; input memory object* IN const char* s ; constatnt string for the memory object to be * compared with** Description : Compares characters of 2 strings irrespective of the * case for a specific count of bytes If the character comparison * is the same the length of the 2 srings determines the shorter * of the 2 strings.** Return : int ;* < 0 string1 substring less than string2 substring * 0 string1 substring identical to string2 substring * > 0 string1 substring greater than string2 substring * * Note :************************************************************************/intmemptr_cmp_nocase( IN memptr * m, IN const char *s ){ int cmp; cmp = strncasecmp( m->buf, s, m->length ); if( cmp == 0 && m->length < strlen( s ) ) { // both strings equal for 'm->length' chars // if m is shorter than s, then s is greater return -1; } return cmp;}/************************************************************************* membuffer *************************************************************************//************************************************************************* Function : membuffer_initialize** Parameters :* INOUT membuffer* m ; buffer to be initialized** Description : Initialize the buffer** Return : void ;** Note :************************************************************************/static XINLINE voidmembuffer_initialize( INOUT membuffer * m ){ m->buf = NULL; m->length = 0; m->capacity = 0;}/************************************************************************* Function : membuffer_set_size** Parameters :* INOUT membuffer* m ; buffer whose size is to be modified* IN size_t new_length ; new size to which the buffer will be * modified** Description : Increases or decreases buffer cap so that at least* 'new_length' bytes can be stored** Return : int ;* UPNP_E_SUCCESS - On Success* UPNP_E_OUTOF_MEMORY - On failure to allocate memory.** Note :************************************************************************/intmembuffer_set_size( INOUT membuffer * m, IN size_t new_length ){ size_t diff; size_t alloc_len; char *temp_buf; if( new_length >= m->length ) // increase length { // need more mem? if( new_length <= m->capacity ) { return 0; // have enough mem; done } diff = new_length - m->length; alloc_len = MAXVAL( m->size_inc, diff ) + m->capacity; } else // decrease length { assert( new_length <= m->length ); // if diff is 0..m->size_inc, don't free if( ( m->capacity - new_length ) <= m->size_inc ) { return 0; } alloc_len = new_length + m->size_inc; } assert( alloc_len >= new_length ); temp_buf = realloc( m->buf, alloc_len + 1 ); //LEAK_FIX_MK //temp_buf = Realloc( m->buf,m->length, alloc_len + 1 );//LEAK_FIX_MK if( temp_buf == NULL ) { // try smaller size alloc_len = new_length; temp_buf = realloc( m->buf, alloc_len + 1 ); //LEAK_FIX_MK //temp_buf = Realloc( m->buf,m->length, alloc_len + 1 );//LEAK_FIX_MK if( temp_buf == NULL ) { return UPNP_E_OUTOF_MEMORY; } } // save m->buf = temp_buf; m->capacity = alloc_len; return 0;}/************************************************************************* Function : membuffer_init** Parameters :* INOUT membuffer* m ; buffer to be initialized** Description : Wrapper to membuffer_initialize().* Set the size of the buffer to MEMBUF_DEF_SIZE_INC* Initializes m->buf to NULL, length=0** Return : void ;** Note :************************************************************************/voidmembuffer_init( INOUT membuffer * m ){ assert( m != NULL ); m->size_inc = MEMBUF_DEF_SIZE_INC; membuffer_initialize( m );}/************************************************************************* Function : membuffer_destroy** Parameters :* INOUT membuffer* m ; buffer to be destroyed** Description : Free's memory allocated for membuffer* m.** Return : void ;** Note :************************************************************************/voidmembuffer_destroy( INOUT membuffer * m ){ if( m == NULL ) { return; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -