r6.1.0:lfds610_freelist_new

From liblfds.org
Revision as of 14:07, 4 January 2015 by Admin (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Source Files

/liblfds610/src/lfds610_freelist/lfds610_freelist_new.c
/liblfds610/inc/liblfds610.h

Prototype

int lfds610_freelist_new( struct lfds610_freelist_state **fs, lfds610_atom_t number_elements,
                        int (*user_data_init_function)(void **user_data, void *user_state),
                        void *user_state );

Parameters

struct lfds610_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.

lfds610_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. After instantiation any thread (except the instantiating thread) must before using the freelist first call lfds610_freelist_use. The values of user_data_init_function and user_state are stored in the freelist state and are re-used when lfds610_freelist_new_elements is called.

See Also