Difference between pages "r6.0.1:Lfds601 freelist guaranteed pop" and "r6.0.1:Lfds601 freelist new"

From liblfds.org
(Difference between pages)
Jump to navigation Jump to search
m (1 revision imported)
 
m (1 revision imported)
 
Line 1: Line 1:
{{DISPLAYTITLE:r6.0.1:lfds601_freelist_guaranteed_pop}}
{{DISPLAYTITLE:r6.0.1:lfds601_freelist_new}}
==Source Files==
==Source Files==
  /liblfds601/src/lfds601_freelist/lfds601_freelist_pop_push.c
  /liblfds601/src/lfds601_freelist/lfds601_freelist_new.c
  /liblfds601/inc/liblfds601.h
  /liblfds601/inc/liblfds601.h


==Prototype==
==Prototype==
  struct lfds601_freelist_element *lfds601_freelist_guaranteed_pop( struct lfds601_freelist_state *fs, struct lfds601_freelist_element **fe );
  int lfds601_freelist_new( struct lfds601_freelist_state **fs, lfds601_atom_t number_elements,
                        int (*user_data_init_function)(void **user_data, void *user_state),
                        void *user_state );


==Parameters==
==Parameters==
''struct lfds601_freelist_state *fs''
''struct lfds601_freelist_state **fs''
: A freelist state as allocated by ''[[r6.0.1:lfds601_freelist_new|lfds601_freelist_new]]''.
: The address of a pointer onto which is allocated the state which represents this freelist. The pointer is set to NULL if freelist creation fails.


''struct lfds601_freelist_element **fe''
''lfds601_atom_t number_elements''
: A pointer to a freelist element pointer which will be set to point to a freshly allocated ''struct freelist''.  Set to NULL if allocation fails.
: The maximum number of elements which can be present in the freelist.  These elements will be allocated when the freelist is created.  If not all elements could be allocated (malloc() fails, or ''user_data_init_function'' returns an error), freelist creation fails.
 
''int (*user_data_init_function)(void **user_data, void *user_state)''
: This is a callback function, which can be NULL.  Each element in the freelist contains as its payload a single void pointer, which the user can set to any value (typically, it points to state the user has allocated).  When the freelist is created, this callback function will be called for each element, where the address of the user data void pointer is provided to the user, for example, to have memory allocated onto it.  This function must return 1 on successful initialisation and 0 on failure.
 
''void *user_state''
: This is passed to each call of ''user_data_init_function'' as the second argument to that functionThe user is expected to use this to pass state information into his initialisation function.  For example, if to initialise freelist elements, a lookup must occur into another data structure, that data structure can be made visible in the initialisation function by being passed in via this argument.


==Return Value==
==Return Value==
Returns a pointer to the address of the freelist element (e.g. the value ''*fe'' is set to).  Returns NULL if element creation failed.  This element is actually allocated, rather than being taken from the freelist, and so will always succeed, unless ''malloc'' or the user initialisation function fail.  The user initialisation function used is that which was passed to ''lfds601_freelist_new'' when the freelist was instantiated.
Returns 1 on success and 0 on failure, with ''*fs'' being set to NULL on failure.


==Notes==
==Notes==
To obtain the user data void pointer from the element, use ''[[r6.0.1:lfds601_freelist_get_user_data_from_element|lfds601_freelist_get_user_data_from_element]]''.
This function instantiates a freelist.  The values of ''user_data_init_function'' and ''user_state'' are stored in the freelist state and are re-used when ''[[r6.0.1:lfds601_freelist_new_elements|lfds601_freelist_new_elements]]'' is called.


==See Also==
==See Also==
* [[r6.0.1:lfds601_freelist|lfds601_freelist]]
* [[r6.0.1:lfds601_freelist|lfds601_freelist]]
* [[r6.0.1:lfds601_freelist_push|lfds601_freelist_push]]
* [[r6.0.1:lfds601_freelist_delete|lfds601_freelist_delete]]

Latest revision as of 14:07, 4 January 2015

Source Files

/liblfds601/src/lfds601_freelist/lfds601_freelist_new.c
/liblfds601/inc/liblfds601.h

Prototype

int lfds601_freelist_new( struct lfds601_freelist_state **fs, lfds601_atom_t number_elements,
                        int (*user_data_init_function)(void **user_data, void *user_state),
                        void *user_state );

Parameters

struct lfds601_freelist_state **fs

The address of a pointer onto which is allocated the state which represents this freelist. The pointer is set to NULL if freelist creation fails.

lfds601_atom_t number_elements

The maximum number of elements which can be present in the freelist. These elements will be allocated when the freelist is created. If not all elements could be allocated (malloc() fails, or user_data_init_function returns an error), freelist creation fails.

int (*user_data_init_function)(void **user_data, void *user_state)

This is a callback function, which can be NULL. Each element in the freelist contains as its payload a single void pointer, which the user can set to any value (typically, it points to state the user has allocated). When the freelist is created, this callback function will be called for each element, where the address of the user data void pointer is provided to the user, for example, to have memory allocated onto it. This function must return 1 on successful initialisation and 0 on failure.

void *user_state

This is passed to each call of user_data_init_function as the second argument to that function. The user is expected to use this to pass state information into his initialisation function. For example, if to initialise freelist elements, a lookup must occur into another data structure, that data structure can be made visible in the initialisation function by being passed in via this argument.

Return Value

Returns 1 on success and 0 on failure, with *fs being set to NULL on failure.

Notes

This function instantiates a freelist. The values of user_data_init_function and user_state are stored in the freelist state and are re-used when lfds601_freelist_new_elements is called.

See Also