function lfds700_ringbuffer_cleanup

From liblfds.org
Jump to navigation Jump to search

Source Files

└───liblfds700
    ├───inc
    │   └───liblfds700
    │           lfds700_ringbuffer.h
    └───src
        └───lfds700_ringbuffer
                lfds700_ringbuffer_cleanup.c

Opaque Structures

struct lfds700_ringbuffer_state;

Prototype

void lfds700_ringbuffer_cleanup( struct lfds700_ringbuffer_state *rs,
                                 void (*element_cleanup_callback)(struct lfds700_ringbuffer_state *rs, void *key, void *value, void *user_state, enum lfds700_misc_flag unread_flag) );

Parameters

struct lfds700_ringbuffer_state *rs

A pointer to an initialized struct lfds700_ringbuffer_state.

void (*element_cleanup_callback)(struct lfds700_ringbuffer_state *rs, void *key, void *value, void *user_state, enum lfds700_misc_flag unread_flag)

A callback function which is called with every element present in the ringbuffer at the time of cleanup. This argument can be NULL. If the key/value pair come from an element which has been written, but not yet read, the argument unread_flag is set to LFDS7000_MISC_RAISED.

Notes

The cleanup function actually does no work except, if the callback function is provided, to iterate over the ringbuffer and pass each element (read or unread, with an argument to the callback function indicating read/unread state) to the callback function.

The user can in the callback function use the LFDS700_RINGBUFFER_GET_USER_STATE_FROM_STATE macro on the stack state to get hold of the user state provided when the stack was initialized.

Example

#include <stdio.h>
#include "liblfds700.h"

void element_cleanup_callback( struct lfds700_ringbuffer_state *rs, void *key, void *value, void *user_state, enum lfds700_misc_flag unread_flag )
{
  // TRD : this will print 0 to 4, as there are five unread elements in the ringbuffer
  if( unread_flag == LFDS700_RAISED )
    printf( "key = %llu\n", (int long long unsigned) (lfds700_pal_uint_t) key );

  return;
}

int main()
{
  int long long unsigned
    loop;

  struct lfds700_misc_prng_state
    ps;

  struct lfds700_ringbuffer_element
    *re;

  struct lfds700_ringbuffer_state
    rs;

  lfds700_misc_library_init_valid_on_current_logical_core();

  lfds700_misc_prng_init( &ps );

  re = malloc( sizeof(struct lfds700_ringbuffer_element) * 11 );

  lfds700_ringbuffer_init_valid_on_current_logical_core( &rs, re, 11, NULL );

  for( loop = 0 ; loop < 5 ; loop++ )
    lfds700_ringbuffer_write( &rs, (void *) (lfds700_pal_uint_t) loop, NULL, NULL, NULL, NULL, &ps );

  lfds700_ringbuffer_cleanup( rs, element_cleanup_callback );

  free( re );

  lfds700_misc_library_cleanup();

  return( EXIT_SUCCESS );
}

See Also