Blame include/freerdp/utils/ringbuffer.h

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2014 Thincast Technologies GmbH
Packit 1fb8d4
 * Copyright 2014 Hardening <contact@hardening-consulting.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 * http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifndef FREERDP_UTILS_RINGBUFFER_H
Packit 1fb8d4
#define FREERDP_UTILS_RINGBUFFER_H
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/wtypes.h>
Packit 1fb8d4
#include <freerdp/api.h>
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
/** @brief ring buffer meta data */
Packit 1fb8d4
struct _RingBuffer {
Packit 1fb8d4
	size_t initialSize;
Packit 1fb8d4
	size_t freeSize;
Packit 1fb8d4
	size_t size;
Packit 1fb8d4
	size_t readPtr;
Packit 1fb8d4
	size_t writePtr;
Packit 1fb8d4
	BYTE *buffer;
Packit 1fb8d4
};
Packit 1fb8d4
typedef struct _RingBuffer RingBuffer;
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
/** @brief a piece of data in the ring buffer, exactly like a glibc iovec */
Packit 1fb8d4
struct _DataChunk {
Packit 1fb8d4
	size_t size;
Packit 1fb8d4
	const BYTE *data;
Packit 1fb8d4
};
Packit 1fb8d4
typedef struct _DataChunk DataChunk;
Packit 1fb8d4
Packit 1fb8d4
#ifdef __cplusplus
Packit 1fb8d4
extern "C" {
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
/** initialise a ringbuffer
Packit 1fb8d4
 * @param initialSize the initial capacity of the ringBuffer
Packit 1fb8d4
 * @return if the initialisation was successful
Packit 1fb8d4
 */
Packit 1fb8d4
FREERDP_API BOOL ringbuffer_init(RingBuffer *rb, size_t initialSize);
Packit 1fb8d4
Packit 1fb8d4
/** destroys internal data used by this ringbuffer
Packit 1fb8d4
 * @param ringbuffer
Packit 1fb8d4
 */
Packit 1fb8d4
FREERDP_API void ringbuffer_destroy(RingBuffer *ringbuffer);
Packit 1fb8d4
Packit 1fb8d4
/** computes the space used in this ringbuffer
Packit 1fb8d4
 * @param ringbuffer
Packit 1fb8d4
 * @return the number of bytes stored in that ringbuffer
Packit 1fb8d4
 */
Packit 1fb8d4
FREERDP_API size_t ringbuffer_used(const RingBuffer *ringbuffer);
Packit 1fb8d4
Packit 1fb8d4
/** returns the capacity of the ring buffer
Packit 1fb8d4
 * @param ringbuffer
Packit 1fb8d4
 * @return the capacity of this ring buffer
Packit 1fb8d4
 */
Packit 1fb8d4
FREERDP_API size_t ringbuffer_capacity(const RingBuffer *ringbuffer);
Packit 1fb8d4
Packit 1fb8d4
/** writes some bytes in the ringbuffer, if the data doesn't fit, the ringbuffer
Packit 1fb8d4
 * is resized automatically
Packit 1fb8d4
 *
Packit 1fb8d4
 * @param rb the ringbuffer
Packit 1fb8d4
 * @param ptr a pointer on the data to add
Packit 1fb8d4
 * @param sz the size of the data to add
Packit 1fb8d4
 * @return if the operation was successful, it could fail in case of OOM during realloc()
Packit 1fb8d4
 */
Packit 1fb8d4
FREERDP_API BOOL ringbuffer_write(RingBuffer *rb, const BYTE *ptr, size_t sz);
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
/** ensures that we have sz bytes available at the write head, and return a pointer
Packit 1fb8d4
 * on the write head
Packit 1fb8d4
 *
Packit 1fb8d4
 * @param rb the ring buffer
Packit 1fb8d4
 * @param sz the size to ensure
Packit 1fb8d4
 * @return a pointer on the write head, or NULL in case of OOM
Packit 1fb8d4
 */
Packit 1fb8d4
FREERDP_API BYTE *ringbuffer_ensure_linear_write(RingBuffer *rb, size_t sz);
Packit 1fb8d4
Packit 1fb8d4
/** move ahead the write head in case some byte were written directly by using
Packit 1fb8d4
 * a pointer retrieved via ringbuffer_ensure_linear_write(). This function is
Packit 1fb8d4
 * used to commit the written bytes. The provided size should not exceed the
Packit 1fb8d4
 * size ensured by ringbuffer_ensure_linear_write()
Packit 1fb8d4
 *
Packit 1fb8d4
 * @param rb the ring buffer
Packit 1fb8d4
 * @param sz the number of bytes that have been written
Packit 1fb8d4
 * @return if the operation was successful, FALSE is sz is too big
Packit 1fb8d4
 */
Packit 1fb8d4
FREERDP_API BOOL ringbuffer_commit_written_bytes(RingBuffer *rb, size_t sz);
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
/** peeks the buffer chunks for sz bytes and returns how many chunks are filled.
Packit 1fb8d4
 * Note that the sum of the resulting chunks may be smaller than sz.
Packit 1fb8d4
 *
Packit 1fb8d4
 * @param rb the ringbuffer
Packit 1fb8d4
 * @param chunks an array of data chunks that will contain data / size of chunks
Packit 1fb8d4
 * @param sz the requested size
Packit 1fb8d4
 * @return the number of chunks used for reading sz bytes
Packit 1fb8d4
 */
Packit 1fb8d4
FREERDP_API int ringbuffer_peek(const RingBuffer *rb, DataChunk chunks[2], size_t sz);
Packit 1fb8d4
Packit 1fb8d4
/** move ahead the read head in case some byte were read using ringbuffer_peek()
Packit 1fb8d4
 * This function is used to commit the bytes that were effectively consumed.
Packit 1fb8d4
 *
Packit 1fb8d4
 * @param rb the ring buffer
Packit 1fb8d4
 * @param sz the
Packit 1fb8d4
 */
Packit 1fb8d4
FREERDP_API void ringbuffer_commit_read_bytes(RingBuffer *rb, size_t sz);
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
#ifdef __cplusplus
Packit 1fb8d4
}
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#endif /* FREERDP_UTILS_RINGBUFFER_H */