Difference between pages "r6:Function:stack delete" and "r6:Function:stack guaranteed push"

From liblfds.org
(Difference between pages)
Jump to navigation Jump to search
m (1 revision imported)
 
m (1 revision imported)
 
Line 1: Line 1:
==Source Files==
==Source Files==
  /src/stack/stack_delete.c
  /src/stack/stack_push_pop.c
  /inc/liblfds.h
  /inc/liblfds.h


==Prototype==
==Prototype==
  void stack_delete( struct stack_state *ss,
  int stack_guaranteed_push( struct stack_state *ss, void *user_data );
                    void (*user_data_delete_function)(void *user_data, void *user_state),
                    void *user_state );


==Parameters==
==Parameters==
Line 12: Line 10:
: A stack state as allocated by ''[[r6:Function:stack_new|stack_new]]''.
: A stack state as allocated by ''[[r6:Function:stack_new|stack_new]]''.


''void (*user_data_delete_function)(void *user_data, void *user_state)''
''void *user_data''
: A callback function, which can be NULL.  This function is called with the user data void pointer from each element before that element is deleted, giving the user an opportunity to delete any allocated state.
: A void pointer of user data which will be pushed onto the stack.
 
''void *user_state''
: This pointer is passed into the ''user_data_delete_function'' as its second argument, enabling the caller to pass state into the callback function.


==Return Value==
==Return Value==
No return value.
The return value is 1 upon successful push, 0 upon failure.  Failure occurs only when ''malloc'' fails.


==Notes==
==Notes==
This function deletes the stack.  If there are any elements present in the stack at the time of deletion, the function loops, popping an element and passing its user data void pointer to user_data_delete_function, until the stack is empty.
The function ''[[r6:Function:stack_push|stack_push]]'' fails only when the stack's freelist is emptyIn this event, ''stack_guaranteed_push'' can be called, which allocates a new element and pushes using that new element, thus guaranteeing a push, barring the event of ''malloc'' failure.


==See Also==
==See Also==
* [[r6:API:Stack|Stack]]
* [[r6:API:Stack|Stack]]
* [[r6:Function:stack_new|stack_new]]
* [[r6:Function:stack_pop|stack_pop]]

Latest revision as of 14:07, 4 January 2015

Source Files

/src/stack/stack_push_pop.c
/inc/liblfds.h

Prototype

int stack_guaranteed_push( struct stack_state *ss, void *user_data );

Parameters

struct stack_state *ss

A stack state as allocated by stack_new.

void *user_data

A void pointer of user data which will be pushed onto the stack.

Return Value

The return value is 1 upon successful push, 0 upon failure. Failure occurs only when malloc fails.

Notes

The function stack_push fails only when the stack's freelist is empty. In this event, stack_guaranteed_push can be called, which allocates a new element and pushes using that new element, thus guaranteeing a push, barring the event of malloc failure.

See Also