/****************************************************************************
**  SIONLIB     http://www.fz-juelich.de/jsc/sionlib                       **
*****************************************************************************
**  Copyright (c) 2008-2018                                                **
**  Forschungszentrum Juelich, Juelich Supercomputing Centre               **
**                                                                         **
**  See the file COPYRIGHT in the package base directory for details       **
****************************************************************************/

/*!
 * \cond sion_internal
 * \file
 *
 * This functions control and manage a memory based buffer for sion
 * files. Depending on a set of environment variables the buffer will work as
 * write-through or as write-back buffer. On Unix systems a POSIX shared memory
 * segment will be used, on Blue Gene/P it is persistent memory.
 *
 * A buffer of n MB will store the last n MB of data per task in the buffer.  If
 * the buffer memory is filled, the full buffer will be written to disk and the
 * buffer will be exhausted.
 *
 */

#ifndef SION_SION_BUFFER_H
#define SION_SION_BUFFER_H

#include <stdint.h>

#include "sion_filedesc.h"

/*!\brief Checks if environment variables are set to use buffer.
 *
 * @param  *sion_filedesc                       sion file description struct (_sion_filedesc)
 *
 * @return SION_SUCCESS if env could be checked
 */
int _lsion_buffer_check_env(_sion_filedesc *sion_filedesc);

/*!\brief Allocate and initalize the buffer.
 *
 * @param  *sion_filedesc                       sion file description struct (_sion_filedesc)
 *
 * @return SION_SUCCESS if ok
 */
int _lsion_buffer_init(_sion_filedesc *sion_filedesc);

/*!\brief Push data to buffer.
 *
 * @param  *sion_filedesc                       sion file description struct (_sion_filedesc)
 * @param  *data                                pointer to data
 * @param  *bytes                               number of bytes to push
 *
 * @return number of bytes stored
 */
int64_t _lsion_buffer_push(_sion_filedesc *sion_filedesc, const void *data, int64_t bytes);

/*!\brief Pop all data from buffer.
 *
 * @param  *sion_filedesc                       sion file description struct (_sion_filedesc)
 * @param  *data                                pointer to data
 * @param  *bytes                               number of bytes in buffer
 *
 * @return 1 if there is data to process
 *         0 not data in buffer
 */
int _lsion_buffer_get_data_ptr(_sion_filedesc *sion_filedesc, void **data, int64_t *bytes);

/*!\brief Flush buffer.
 *
 * @param  *sion_filedesc sion file description struct (_sion_filedesc)
 *
 * @return SION_SUCCESS if ok
 */
int _lsion_buffer_flush(_sion_filedesc *sion_filedesc);

#endif

/// \endcond
