📄 memlib.c
字号:
MemLstDeleteMember(psTemp,psTemp,&psTemp);
}
/*
** Reset the pointer sent in, should be NULL now
*/
(*ppsListRoot) = psTemp;
return(C_OK);
}
/******************************************************************************
*+
** Function Name: MemQueDeqMember
**
** Description: Returns the pvData of the head member from the (FIFO) queue
**
** Return Codes:
** C_OK
**
** Written by: John Tal
**
**
** Modification History:
**
** Date Programmer Mod# Modification
** ---------------------------------------------------------------------------
** 09-FEB-1991 J. Tal V1.0-000 New
**
*-
*/
#if C_ANSI
SHORT APIENTRY
MemQueDeqMember(LLIST_PP ppsListHead, LLIST_PP ppsListTail, PVOID * ppvData)
#else
SHORT APIENTRY
MemQueDeqMember(ppsListHead,ppsListTail, ppvData)
LLIST_PP ppsListHead;
LLIST_PP ppsListTail;
PVOID * ppvData;
#endif
{
LLIST_P psTempHead;
LLIST_P psTempTail;
psTempHead = (*ppsListHead);
psTempTail = (*ppsListTail);
/*
** FIFO remove is simple, always take the head
*/
if(psTempHead != NULL)
(*ppvData) = psTempHead -> pvData;
MemLstDeleteMember(psTempHead, psTempHead, ppsListHead);
/*
** Check if deleted only member (head and tail)
*/
if(psTempHead == psTempTail)
(*ppsListTail) = NULL;
return(C_OK);
}
/******************************************************************************
*+
** Function Name: MemQueEnqMember
**
** Description: Adds pvData to a member to the end of the (FIFO) queue
** MemQue*** functions provide a generic FIFO queue
** service.
**
** You send in the head and tail and the data.
**
** A head, tail for a queue are implemented
** via the MemLst*** functions and are compatible with
** them should you want to use the MemLst*** functions
** directly on the queue (use MemLstFindMember to
** search the queue).
**
** These Que functions were added by suggestion of
** John Tomlinson and Pat Smith of Plant Automation Division.
**
**
** Return Codes:
** C_OK
**
** Written by: John Tal
**
**
** Modification History:
**
** Date Programmer Mod# Modification
** ---------------------------------------------------------------------------
** 09-FEB-1991 J. Tal V1.0-000 New
**
*-
*/
#if C_ANSI
SHORT APIENTRY
MemQueEnqMember(LLIST_PP ppsListHead, LLIST_PP ppsListTail, PVOID pvData)
#else
SHORT APIENTRY
MemQueEnqMember(ppsListHead, ppsListTail, pvData)
LLIST_PP ppsListHead;
LLIST_PP ppsListTail;
PVOID pvData;
#endif
{
LLIST_P psTemp;
/*
** Alloc a new member, caller is abstracted from this process
** of creating new queue internal members (LLIST_P)
**
** NOTE: psTemp is allocated off the stack but is not a
** dangling pointer (cf C_ C Coding Guidelines)
*/
MemLstAllocMember(&psTemp);
if(psTemp == NULL)
return(C_NOTOK);
/*
** Point to the data we were sent
*/
psTemp -> pvData = pvData;
/*
** Connect to existing queue entries.
** If no entries, this is head and tail.
** If entries, put after current tail and reset tail (head unaffected)
*/
if((*ppsListHead) == NULL)
{
(*ppsListHead) = psTemp;
(*ppsListTail) = psTemp;
}
else
{
MemLstAddAfterMember((*ppsListTail),psTemp);
(*ppsListTail) = psTemp;
}
return(C_OK);
}
/******************************************************************************
*+
** Function Name: MemStkClearStack
**
** Description: LOGICALLY clears a stack. See MemStkVacateStack.
**
** Stacks are implemented here in link list using the
** MemLst routines.
**
** Stacks are also known as LIFO (Last In First Out) Queues
**
** Push a stack and that member is placed on top
** Pop a stack and top member is returned and removed
**
**
** Top of Stack StackMember -> Pointer to data to hold/remember
** |
** v
** StackMember -> Pointer to data to hold/remember
** |
** v
** Bot of Stack StackMember -> Pointer to data to hold/remember
**
**
**
**
** Return Codes:
** C_OK
**
** Written by: John Tal
**
**
** Modification History:
**
** Date Programmer Mod# Modification
** ---------------------------------------------------------------------------
** 06/12/90 J. Tal V1.0-000 New
**
*-
*/
#if C_ANSI
SHORT APIENTRY
MemStkClearStack(LLIST_PP ppsLlistStack)
#else
SHORT APIENTRY
MemStkClearStack(ppsLlistStack)
LLIST_PP ppsLlistStack;
#endif
{
(*ppsLlistStack) = NULL;
return(C_OK);
}
/******************************************************************************
*+
** Function Name: MemStkEmptyStack
**
** Description: Checks if a Stack is empty
**
** Return Codes:
** C_OK
**
** Written by: John Tal
**
**
** Modification History:
**
** Date Programmer Mod# Modification
** ---------------------------------------------------------------------------
** 06/12/90 J. Tal V1.0-000 New
**
*-
*/
#if C_ANSI
SHORT APIENTRY
MemStkEmptyStack(LLIST_P psLlistStack, BOOL *fEmpty)
#else
SHORT APIENTRY
MemStkEmptyStack(psLlistStack,fEmpty)
LLIST_P psLlistStack;
BOOL * fEmpty;
#endif
{
if(psLlistStack == NULL)
*fEmpty = C_TRUE;
else
*fEmpty = C_FALSE;
return(C_OK);
}
/******************************************************************************
*+
** Function Name: MemStkPop
**
** Description: Pops an element off the stack
**
** Return Codes:
** C_OK
**
** Written by: John Tal
**
**
** Modification History:
**
** Date Programmer Mod# Modification
** ---------------------------------------------------------------------------
** 06/12/90 J. Tal V1.0-000 New
**
*-
*/
#if C_ANSI
SHORT APIENTRY
MemStkPop(LLIST_PP ppsLlistStackHead, PVOID *pvElementPointer)
#else
SHORT APIENTRY
MemStkPop(ppsLlistStackHead,pvElementPointer)
LLIST_PP ppsLlistStackHead;
PVOID * pvElementPointer;
#endif
{
LLIST_P psLlistElement;
if( (*ppsLlistStackHead) != NULL )
{
/*
** To modify a pointer inside of a function requires
** sending in the address of the pointer.
*/
(*pvElementPointer) = (*ppsLlistStackHead) -> pvData;
/*
** Reset to new stack head pointer
*/
psLlistElement = (*ppsLlistStackHead) -> psNext;
/*
** Free the old stack head pointer
*/
free(*ppsLlistStackHead);
/*
** Reset the head to the new one for return
*/
(*ppsLlistStackHead) = psLlistElement;
}
else
{
/*
** Stack is empty, return NULL
*/
(*pvElementPointer) = NULL;
}
return(C_OK);
}
/******************************************************************************
*+
** Function Name: MemStkPush
**
** Description: Pushes an element onto the stack
**
** Return Codes:
** C_OK
**
** Written by: John Tal
**
**
** Modification History:
**
** Date Programmer Mod# Modification
** ---------------------------------------------------------------------------
** 06/12/90 J. Tal V1.0-000 New
**
*-
*/
#if C_ANSI
SHORT APIENTRY
MemStkPush(LLIST_PP ppsLlistStackHead, PVOID pvElementPointer)
#else
SHORT APIENTRY
MemStkPush(ppsLlistStackHead,pvElementPointer)
LLIST_PP ppsLlistStackHead;
PVOID pvElementPointer;
#endif
{
LLIST_P psLlistStack;
MemLstAllocMember(&psLlistStack); /* allocate new member */
if(psLlistStack == NULL)
return(C_NOTOK);
psLlistStack -> pvData = pvElementPointer;
psLlistStack -> psNext = (*ppsLlistStackHead);
(*ppsLlistStackHead) = psLlistStack;
return(C_OK);
}
/******************************************************************************
*+
** Function Name: MemStkVacateStack
**
** Description: Actually clears a stack by popping all entries into
** the bit bucket.
**
** Return Codes:
** C_OK
**
** Written by: John Tal
**
**
** Modification History:
**
** Date Programmer Mod# Modification
** ---------------------------------------------------------------------------
** 06/12/90 J. Tal V1.0-000 New
**
*-
*/
#if C_ANSI
SHORT APIENTRY
MemStkVacateStack(LLIST_PP ppsLlistStack, BOOL fFreeData)
#else
SHORT APIENTRY
MemStkVacateStack(ppsLlistStack,fFreeData)
LLIST_PP ppsLlistStack;
BOOL fFreeData;
#endif
{
PVOID pvData;
while(*ppsLlistStack != NULL)
{
MemStkPop(ppsLlistStack,&pvData);
if(fFreeData)
if(pvData != NULL)
free(pvData);
}
(*ppsLlistStack) = NULL;
return(C_OK);
}
/******************************************************************************
*+
** Function Name: MemTreAllocNode
**
** Description: Allocates memory for a new tree node.
** Initializes new node pointers.
**
** Return Codes:
** C_OK
** C_NOTOK
**
** Written by: John Tal
**
**
** Modification History:
**
** Date Programmer Mod# Modification
** ---------------------------------------------------------------------------
** 06/12/90 J. Tal V1.0-000 New
**
*-
*/
#if C_ANSI
SHORT APIENTRY
MemTreAllocNode(TNODE_PP ppsRetNode)
#else
SHORT APIENTRY
MemTreAllocNode(ppsRetNode)
TNODE_PP ppsRetNode;
#endif
{
TNODE_P psItem;
psItem = (TNODE_P) calloc(sizeof(TNODE_T),sizeof(BYTE));
(*ppsRetNode) = psItem;
if(psItem != NULL)
return(C_OK);
else
return(C_NOTOK);
}
/******************************************************************************
*+
** Function Name: MemTreDeletNode
**
** Description: Search for a node in a tree and deletes that node
**
** External Functions:
** MemTreLeaf
** MemTreRotateNodeRight
** MemTreRotateNodeLeft
** MemTreDeleteNode (Recursive)
**
** Return Codes:
** C_OK
** C_NOTOK
**
** Written by: John Tal
**
**
** Modification History:
**
** Date Programmer Mod# Modification
** ---------------------------------------------------------------------------
** 06/12/90 J. Tal V1.0-000 New
**
*-
*/
/***************************************************************************
Function: MemTreDeleteNode
Description: Searchs for a node in a tree and deletes that node
Inputs: Pointer to tree root
Char pointer of data value to search for.
Pointer to compare function for tree data type.
Outputs: Pointer to tree root.
****************************************************************************/
#if C_ANSI
SHORT APIENTRY
MemTreDeleteNode(TNODE_P psTree,PVOID pvVal,SHORT (*compare_func)(PVOID,PVOID),TNODE_PP ppsRetNode)
#else
SHORT APIENTRY
MemTreDeleteNode(psTree,pvVal,compare_func,ppsRetNode)
TNODE_P psTree;
PVOID pvVal;
SHORT (*compare_func)();
TNODE_PP ppsRetNode;
#endif
{
if(psTree == NULL)
{
(*ppsRetNode) = NULL;
return(C_NOTOK);
}
if( ((*compare_func)(pvVal,psTree -> pvData)) == 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -