#ifndef SWIFT_UTILS_RING_BUFFER_H
#define SWIFT_UTILS_RING_BUFFER_H

#include <swift/atomic.h>
#include <swift/stdlib.h>
#include <swift/io.h>

typedef struct swift_ring_buffer_t swift_ring_buffer_t;
typedef struct swift_ring_buffer_stream_t swift_ring_buffer_stream_t;

struct swift_ring_buffer_t {
	swift_ref_t ref;
	unsigned char *data;
	size_t head, tail, count, size;
};

struct swift_ring_buffer_stream_t {
	swift_stream_t stream;
	swift_ring_buffer_t *buffer;
};

#define SWIFT_RING_BUFFER_STREAM(ptr) \
	(SWIFT_CONTAINER_OF((ptr), swift_ring_buffer_stream_t, stream))

SWIFT_NET_API swift_ring_buffer_t *swift_create_ring_buffer(size_t size);
SWIFT_NET_API void swift_destroy_ring_buffer(swift_ring_buffer_t *buffer);
SWIFT_NET_API swift_ring_buffer_t *swift_copy_ring_buffer(swift_ring_buffer_t *buffer);
SWIFT_NET_API bool swift_resize_ring_buffer(swift_ring_buffer_t *buffer, size_t size);
SWIFT_NET_API bool swift_expand_ring_buffer(swift_ring_buffer_t *buffer, size_t count);
SWIFT_NET_API size_t swift_read_ring_buffer(swift_ring_buffer_t *buffer, void *data,
	size_t size);
SWIFT_NET_API size_t swift_write_ring_buffer(swift_ring_buffer_t *buffer, const void *data,
	size_t size);

SWIFT_NET_API swift_stream_t *swift_create_ring_buffer_stream(
	size_t size);
SWIFT_NET_API swift_stream_t *swift_create_ring_buffer_stream_from(
	swift_ring_buffer_t *buffer);
#endif
