📄 slog_bbuf.c
字号:
void SLOG_Bbuf_Init( SLOG_intvlrec_blist_t *slog_bbuf ){ if ( slog_bbuf != NULL ) { slog_bbuf->Nbytes_in_file = 0; slog_bbuf->count_irec = 0; slog_bbuf->lhead = NULL; slog_bbuf->ltail = NULL; slog_bbuf->lptr = NULL; }}/* This routine moves( i.e. copy pointer of ) the interval record _NODE_ to the specified slog_bbuf. It makes no attempt to allocate any memory for the new interval record node and its association and argument vector. That means even if the input irec has zero bytesize in file. This routine will still move the irec as requested. Garbage in Garbage out. */int SLOG_Bbuf_AddMovedIrecNode( SLOG_intvlrec_blist_t *slog_bbuf, SLOG_intvlrec_lptr_t *cur_node ){ SLOG_uint32 sizeof_IntvlRec_in_file; if ( cur_node == NULL ) { fprintf( errfile, __FILE__":SLOG_Bbuf_AddMovedIrecNode() - " "the input irec node is a NULL pointer\n" ); fflush( errfile ); return SLOG_FAIL; } if ( cur_node->irec == NULL ) { fprintf( errfile, __FILE__":SLOG_Bbuf_AddMovedIrecNode() - " "the input irec node contains NULL irec pointer\n" ); fflush( errfile ); return SLOG_FAIL; } /* Update the USED space & count in SLOG_intvlrec_blist_t */ sizeof_IntvlRec_in_file = ( cur_node->irec )->bytesize; slog_bbuf->Nbytes_in_file += sizeof_IntvlRec_in_file; slog_bbuf->count_irec++; /* Attach the node to the bi-directional linked list, SLOG_BiDirBuffer */ cur_node->prev = slog_bbuf->ltail; cur_node->next = NULL; /* this line gurantees bi-directional linked list */ if ( cur_node->prev != NULL ) /* Check if the cur_node is Blist's head, lhead->prev == NULL */ ( cur_node->prev )->next = cur_node; slog_bbuf->ltail = cur_node; /* this line gurantees that the lhead is pointing at the head */ if ( slog_bbuf->lhead == NULL ) slog_bbuf->lhead = slog_bbuf->ltail; return SLOG_SUCCESS;}/* To avoid memory leak, the caller of SLOG_Bbuf_AddCopiedIntvlRec() _MAY_ need to deallocate the memory for intvlrec.args[]. Deallocation is needed when copied intvlrec is temporary/local variable in the calling routine. Because SLOG_Bbuf_AddCopiedIntvlRec() calls SLOG_Irec_Copy() which has allocated memory for another copy of (destination) intvlrec.args[]. Doing so will make memory management easier, since all copying are done by creation of another object with identical values. So memory cleanup can be done simply by removing old ones*/int SLOG_Bbuf_AddCopiedIntvlRec( SLOG_intvlrec_blist_t *slog_bbuf, const SLOG_intvlrec_t *irec ){ SLOG_intvlrec_lptr_t *cur_node; SLOG_uint32 sizeof_IntvlRec_in_file; if ( irec == NULL ) { fprintf( errfile, __FILE__":SLOG_Bbuf_AddCopiedIntvlRec() - " "the input irec is a NULL pointer\n" ); fflush( errfile ); return SLOG_FAIL; } /* Create & Update the node in the SLOG_BiDirBuffer */ cur_node = ( SLOG_intvlrec_lptr_t * ) malloc( sizeof( SLOG_intvlrec_lptr_t ) ); if ( cur_node == NULL ) { fprintf( errfile, __FILE__":SLOG_Bbuf_AddCopiedIntvlRec() - " "malloc() fails, " "Cannot allocate a node for SLOG_Bbuf\n" ); fflush( errfile ); return SLOG_FAIL; } cur_node->irec = SLOG_Irec_Copy( irec ); if ( cur_node->irec == NULL ) { fprintf( errfile, __FILE__":SLOG_Bbuf_AddCopiedIntvlRec() - " "SLOG_Irec_Copy() fails\n" ); fflush( errfile ); return SLOG_FAIL; } /* Update the USED space & count in SLOG_intvlrec_blist_t */ sizeof_IntvlRec_in_file = ( cur_node->irec )->bytesize; slog_bbuf->Nbytes_in_file += sizeof_IntvlRec_in_file; slog_bbuf->count_irec++; /* Attach the node to the bi-directional linked list, SLOG_BiDirBuffer */ cur_node->prev = slog_bbuf->ltail; cur_node->next = NULL; /* this line gurantees bi-directional linked list */ if ( cur_node->prev != NULL ) /* Check if the cur_node is Blist's head, lhead->prev == NULL */ ( cur_node->prev )->next = cur_node; slog_bbuf->ltail = cur_node; /* this line gurantees that the lhead is pointing at the head */ if ( slog_bbuf->lhead == NULL ) slog_bbuf->lhead = slog_bbuf->ltail; return SLOG_SUCCESS;}/* This routine only moves( i.e. copy pointer of ) the interval record to the specified slog_bbuf. It makes no attempt to allocate any memory for the new interval record and its association and argument vector. That means even if the input irec has zero bytesize in file. This routine will still move the irec as requested. Garbage in Garbage out. const SLOG_intvlrec_t *irec*/int SLOG_Bbuf_AddMovedIntvlRec( SLOG_intvlrec_blist_t *slog_bbuf, SLOG_intvlrec_t *irec ){ SLOG_intvlrec_lptr_t *cur_node; SLOG_uint32 sizeof_IntvlRec_in_file; if ( irec == NULL ) { fprintf( errfile, __FILE__":SLOG_Bbuf_AddMovedIntvlRec() - " "the input irec is a NULL pointer\n" ); fflush( errfile ); return SLOG_FAIL; } /* Create & Update the node in the SLOG_BiDirBuffer */ cur_node = ( SLOG_intvlrec_lptr_t * ) malloc( sizeof( SLOG_intvlrec_lptr_t ) ); if ( cur_node == NULL ) { fprintf( errfile, __FILE__":SLOG_Bbuf_AddMovedIntvlRec() - " "malloc() fails, " "Cannot allocate a node for SLOG_Bbuf\n" ); fflush( errfile ); return SLOG_FAIL; } cur_node->irec = irec; /* Update the USED space & count in SLOG_intvlrec_blist_t */ sizeof_IntvlRec_in_file = ( cur_node->irec )->bytesize; slog_bbuf->Nbytes_in_file += sizeof_IntvlRec_in_file; slog_bbuf->count_irec++; /* Attach the node to the bi-directional linked list, SLOG_BiDirBuffer */ cur_node->prev = slog_bbuf->ltail; cur_node->next = NULL; /* this line gurantees bi-directional linked list */ if ( cur_node->prev != NULL ) /* Check if the cur_node is Blist's head, lhead->prev == NULL */ ( cur_node->prev )->next = cur_node; slog_bbuf->ltail = cur_node; /* this line gurantees that the lhead is pointing at the head */ if ( slog_bbuf->lhead == NULL ) slog_bbuf->lhead = slog_bbuf->ltail; return SLOG_SUCCESS;}/* Remove or Rearrange the _prev_ and _next_ pointer links before removal of the node pointed by slog_bbuf->lptr*/int SLOG_Bbuf_RemoveNodeLinks( SLOG_intvlrec_blist_t *slog_bbuf ){ if ( slog_bbuf->lptr == NULL ) { fprintf( errfile, __FILE__":SLOG_Bbuf_RemoveNodeLinks() - " "slog_bbuf->lptr is NULL\n" ); fflush( errfile ); return SLOG_FAIL; } if ( ( slog_bbuf->lptr )->prev != NULL ) ( ( slog_bbuf->lptr )->prev )->next = ( slog_bbuf->lptr )->next; else { if ( ( slog_bbuf->lptr )->next != NULL ) { slog_bbuf->lhead = ( slog_bbuf->lptr )->next; ( slog_bbuf->lhead )->prev = NULL; } else slog_bbuf->lhead = NULL; } if ( ( slog_bbuf->lptr )->next != NULL ) ( ( slog_bbuf->lptr )->next )->prev = ( slog_bbuf->lptr )->prev; else { if ( ( slog_bbuf->lptr )->prev != NULL ) { slog_bbuf->ltail = ( slog_bbuf->lptr )->prev; ( slog_bbuf->ltail )->next = NULL; } else slog_bbuf->ltail = NULL; } /* Update the slog_bbuf's bookkeeping data */ slog_bbuf->Nbytes_in_file -= ( ( slog_bbuf->lptr )->irec )->bytesize; slog_bbuf->count_irec--; return SLOG_SUCCESS;}/* Free up the memory allocated to the various internal data structure*/void SLOG_Bbuf_FreeNode( SLOG_intvlrec_blist_t *slog_bbuf ){ if ( slog_bbuf->lptr != NULL ) { SLOG_Irec_Free( ( slog_bbuf->lptr )->irec ); free( slog_bbuf->lptr ); }}void SLOG_Bbuf_DelAllNodes( SLOG_intvlrec_blist_t *slog_bbuf ){ SLOG_intvlrec_lptr_t *lptr; while ( slog_bbuf->ltail != NULL ) { lptr = slog_bbuf->ltail; slog_bbuf->ltail = ( slog_bbuf->ltail )->prev; SLOG_Irec_Free( lptr->irec ); free( lptr ); } slog_bbuf->Nbytes_in_file = 0; slog_bbuf->count_irec = 0; slog_bbuf->lhead = NULL; slog_bbuf->ltail = NULL; slog_bbuf->lptr = NULL; /* slog_bbuf->lptr = slog_bbuf->ltail; while ( slog_bbuf->ltail != NULL ) { SLOG_Bbuf_RemoveNodeLinks( slog_bbuf ); SLOG_Bbuf_FreeNode( slog_bbuf ); slog_bbuf->lptr = slog_bbuf->ltail; } */}void SLOG_Bbuf_Free( SLOG_intvlrec_blist_t *slog_bbuf ){ if ( slog_bbuf != NULL ) { SLOG_Bbuf_DelAllNodes( slog_bbuf ); free( slog_bbuf ); slog_bbuf = NULL; }}void SLOG_Bbuf_CopyBBufVal( const SLOG_intvlrec_blist_t *src, SLOG_intvlrec_blist_t *dest ){ dest->Nbytes_in_file = src->Nbytes_in_file; dest->count_irec = src->count_irec; dest->lhead = src->lhead; dest->ltail = src->ltail; dest->lptr = src->lptr;}int SLOG_Bbuf_IsConsistent( const SLOG_intvlrec_blist_t *slog_bbuf ){ int flag1, flag2, flag3, flag4, flag; flag1 = ( slog_bbuf->lhead == NULL ); flag2 = ( slog_bbuf->ltail == NULL ); flag3 = ( slog_bbuf->Nbytes_in_file == 0 ); flag4 = ( slog_bbuf->count_irec == 0 ); flag = flag1 + flag2 + flag3 + flag4; if ( flag == 4 || flag == 0 ) return SLOG_TRUE; else { fprintf( errfile, __FILE__":SLOG_Bbuf_IsConsistent() - " "Internal Inconsistency in input slog_bbuf " "is detected\n" ); fprintf( errfile, "\t""slog_bbuf->Nbytes_in_file = "fmt_ui32"\n", slog_bbuf->Nbytes_in_file ); fprintf( errfile, "\t""slog_bbuf->count_irec = "fmt_ui32"\n", slog_bbuf->count_irec ); fprintf( errfile, "\t""slog_bbuf->lhead = %p\n", slog_bbuf->lhead ); fprintf( errfile, "\t""slog_bbuf->ltail = %p\n", slog_bbuf->ltail ); fflush( errfile ); return SLOG_FALSE; }}int SLOG_Bbuf_IsEmpty( const SLOG_intvlrec_blist_t *slog_bbuf ){ if ( SLOG_Bbuf_IsConsistent( slog_bbuf ) ) { return ( slog_bbuf->count_irec == 0 ); } else { fprintf( errfile, __FILE__":SLOG_Bbuf_IsEmpty() - " "SLOG_Bbuf_IsConsistent() fails\n" ); fflush( errfile ); exit( 1 ); return SLOG_FALSE; }}void SLOG_Bbuf_Print( const SLOG_intvlrec_blist_t *slog_bbuf, FILE *outfd ){ const SLOG_intvlrec_lptr_t *lptr; int count; count = 0; for ( lptr = slog_bbuf->lhead; lptr != NULL; lptr = lptr->next, count++ ) { fprintf( outfd, "%d: ", count ); SLOG_Irec_Print( lptr->irec, outfd ); fprintf( outfd, "\n" ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -