1
0
mirror of https://gitlab.xiph.org/xiph/icecast-common.git synced 2024-12-04 14:46:31 -05:00

Feature: Added igloo_buffer_* (copy of Icecast's buffer_*)

This commit is contained in:
Philipp Schafft 2018-11-02 15:45:40 +00:00
parent 6bca93fe65
commit d196273440
4 changed files with 525 additions and 2 deletions

View File

@ -19,11 +19,13 @@ pkginclude_HEADERS = \
include/igloo/timing.h \ include/igloo/timing.h \
include/igloo/ro.h \ include/igloo/ro.h \
include/igloo/types.h \ include/igloo/types.h \
include/igloo/typedef.h include/igloo/typedef.h \
include/igloo/buffer.h
libigloo_la_SOURCES = \ libigloo_la_SOURCES = \
src/libigloo.c \ src/libigloo.c \
src/ro.c src/ro.c \
src/buffer.c
libigloo_la_LIBADD = \ libigloo_la_LIBADD = \
avl/libiceavl.la \ avl/libiceavl.la \
httpp/libicehttpp.la \ httpp/libicehttpp.la \

205
include/igloo/buffer.h Normal file
View File

@ -0,0 +1,205 @@
/* Copyright (C) 2018 Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/*
* This file contains the API for a refobject based buffer object.
* It can be used to store data and allows on the fly re-allocation.
*/
#ifndef _LIBIGLOO__BUFFER_H_
#define _LIBIGLOO__BUFFER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdarg.h>
#include <igloo/ro.h>
/* About thread safety:
* This set of functions is intentinally not thread safe.
*/
igloo_RO_FORWARD_TYPE(igloo_buffer_t);
/* This creates a new buffer object.
* Parameters:
* preallocation
* The number of bytes to allocate for use later on. See igloo_buffer_preallocate() for details.
* userdata, name, associated
* See refobject_new().
*/
igloo_buffer_t * igloo_buffer_new(ssize_t preallocation, const char *name, igloo_ro_t associated);
/* Depreciated: This creates a new buffer with defaults.
* Do NOT use this. Use refobject_new(igloo_buffer_t)
*
* This is the same as:
* igloo_buffer_new(-1, NULL, NULL, REFOBJECT_NULL)
*/
igloo_buffer_t * igloo_buffer_new_simple(void);
/* This function preallocates space for later use.
* Parameters:
* buffer
* The buffer to operate on.
* request
* Number of bytes to additionally allocate.
* Notes:
* This function is very usedful when adding a large number of smaller buffers to avoid
* internal reallocation calls happening to often. However it is not required to call
* this function before adding data to the buffer.
*/
void igloo_buffer_preallocate(igloo_buffer_t *buffer, size_t request);
/* Gets data and length of the buffer.
* Parameters:
* buffer
* The buffer to operate on.
* data
* Pointer to the stored data. If NULL the pointer is not returned.
* length
* Pointer to the length of how many bytes are in the buffer. If NULL
* length is not returned.
*/
int igloo_buffer_get_data(igloo_buffer_t *buffer, const void **data, size_t *length);
/* Gets data as a string. The string is '\0'-terminated.
* Parameters:
* buffery
* The buffer to operate on.
* string
* The string representing the data hold by the buffer.
*/
int igloo_buffer_get_string(igloo_buffer_t *buffer, const char **string);
/* Sets the length of the buffer.
* Parameters:
* buffer
* The buffer to operate on.
* length
* New length of the buffer.
* Notes:
* This can only be used to reduce the size of the buffer. To add data to
* the buffer use igloo_buffer_push_*().
*
* Calling this with length set to 0 clears the buffer but does not deallocate it.
*/
int igloo_buffer_set_length(igloo_buffer_t *buffer, size_t length);
/* Shifts data out of the buffer.
* Parameters:
* buffer
* The buffer to operate on.
* amount
* The amount of bytes to be removed from the begin of the buffer.
* Notes:
* This function can be useful for skipping some small header. However this
* must not be used to implement a kind of ring buffer as it will result in
* poor performance caused by massive reallocations and memory copies.
*/
int igloo_buffer_shift(igloo_buffer_t *buffer, size_t amount);
/* This pushes data to the end of the buffer.
* Parameters:
* buffer
* The buffer to operate on.
* data
* The data to push.
* length
* The length of the data to push in byte.
* Notes:
* Consider using igloo_buffer_zerocopy_*().
*/
int igloo_buffer_push_data(igloo_buffer_t *buffer, const void *data, size_t length);
/* This pushes a string to the end of the buffer.
* Parameters:
* buffer
* The buffer to operate on.
* string
* The string to be pushed. The tailing '\0'-termination will not be
* part of the buffer.
* Notes:
* Consider using igloo_buffer_zerocopy_*().
*/
int igloo_buffer_push_string(igloo_buffer_t *buffer, const char *string);
/* This pushes a formated string to the end of the buffer.
* Parameters:
* buffer
* The buffer to operate on.
* format
* The format string as for printf() family functions.
* ...
* The parameters according to the format string.
*/
int igloo_buffer_push_printf(igloo_buffer_t *buffer, const char *format, ...);
/* This pushes a formated string to the end of the buffer using a va_list.
* Parameters:
* buffer
* The buffer to operate on.
* format
* The format string as for printf() family functions.
* ap
* The parameters according to the format string as va_list.
* See also:
* vprintf(3).
*/
int igloo_buffer_push_vprintf(igloo_buffer_t *buffer, const char *format, va_list ap);
/* This pushes the content of another buffer to the end of the buffer.
* Parameters:
* buffer
* The buffer to operate on.
* source
* The buffer which's content is to be copied.
*/
int igloo_buffer_push_buffer(igloo_buffer_t *buffer, igloo_buffer_t *source);
/* This requests for a memory buffer that can be pushed to without the need for copy.
* Parameters:
* buffer
* The buffer to operate on.
* data
* Pointer to memory that can be written and will become part of the buffer object.
* request
* Size of the memory area that is returned by data in bytes.
* Notes:
* This is the first step of the zero copy push. After the memory returned by data has been
* written (e.g. used in a call to read(2)) igloo_buffer_zerocopy_push_complete() must be called.
*/
int igloo_buffer_zerocopy_push_request(igloo_buffer_t *buffer, void **data, size_t request);
/* This is the final step of a zero copy push.
* Parameters:
* buffer
* The buffer to operate on.
* done
* Amount of data in bytes that has actually been written into the memory area.
* May be zero to what has been requested with request.
*/
int igloo_buffer_zerocopy_push_complete(igloo_buffer_t *buffer, size_t done);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -32,6 +32,8 @@ extern "C" {
/* Included in case is not yet included */ /* Included in case is not yet included */
#include "typedef.h" #include "typedef.h"
typedef struct igloo_buffer_tag igloo_buffer_t;
/* /*
* This header includes forward declarations for several basic types. * This header includes forward declarations for several basic types.
*/ */
@ -44,6 +46,7 @@ igloo_RO_FORWARD_TYPE(igloo_ro_base_t);
typedef union __attribute__ ((__transparent_union__)) { typedef union __attribute__ ((__transparent_union__)) {
/* Those are libigloo's own types */ /* Those are libigloo's own types */
igloo_RO_TYPE(igloo_ro_base_t) igloo_RO_TYPE(igloo_ro_base_t)
igloo_RO_TYPE(igloo_buffer_t)
/* Now we add the current compilation unit's private types if any */ /* Now we add the current compilation unit's private types if any */
#ifdef igloo_RO_PRIVATETYPES #ifdef igloo_RO_PRIVATETYPES

313
src/buffer.c Normal file
View File

@ -0,0 +1,313 @@
/* Icecast
*
* This program is distributed under the GNU General Public License, version 2.
* A copy of this license is included with this source.
*
* Copyright 2018, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <igloo/ro.h>
#include <igloo/buffer.h>
struct igloo_buffer_tag {
igloo_ro_base_t __base;
/* Buffer itself */
void *buffer;
/* Length in bytes of buffer */
size_t length;
/* Amount of bytes in use of the buffer. This includes offset bytes */
size_t fill;
/* Bytes of offset at the start of the buffer */
size_t offset;
};
static void __free(igloo_ro_t self);
igloo_RO_PUBLIC_TYPE(igloo_buffer_t,
igloo_RO_TYPEDECL_FREE(__free),
igloo_RO_TYPEDECL_NEW_NOOP()
);
static void __free(igloo_ro_t self)
{
igloo_buffer_t *buffer = igloo_RO_TO_TYPE(self, igloo_buffer_t);
free(buffer->buffer);
}
igloo_buffer_t * igloo_buffer_new(ssize_t preallocation, const char *name, igloo_ro_t associated)
{
igloo_buffer_t *buffer = igloo_ro_new_ext(igloo_buffer_t, name, associated);
if (!buffer)
return NULL;
if (preallocation > 0)
igloo_buffer_preallocate(buffer, preallocation);
return buffer;
}
igloo_buffer_t * igloo_buffer_new_simple(void)
{
return igloo_ro_new(igloo_buffer_t);
}
void igloo_buffer_preallocate(igloo_buffer_t *buffer, size_t request)
{
void *n;
size_t newlen;
if (!buffer)
return;
/* Remove the offset if it makes sense to do so. */
if (buffer->offset == buffer->fill) {
buffer->offset = 0;
buffer->fill = 0;
} else if ((2*buffer->offset) < buffer->fill || buffer->offset >= 512 || (buffer->offset > 128 && buffer->offset >= request)) {
buffer->fill -= buffer->offset;
memmove(buffer->buffer, buffer->buffer + buffer->offset, buffer->fill);
buffer->offset = 0;
}
if (!request)
return;
newlen = buffer->fill + request;
if (buffer->length >= newlen)
return;
/* Make sure we at least add 64 bytes and are 64 byte aligned */
newlen = newlen + 64 - (newlen % 64);
n = realloc(buffer->buffer, newlen);
/* Just return if this failed */
if (!n)
return;
buffer->buffer = n;
buffer->length = newlen;
}
int igloo_buffer_get_data(igloo_buffer_t *buffer, const void **data, size_t *length)
{
if (!buffer)
return -1;
if (data) {
*data = buffer->buffer + buffer->offset;
}
if (length) {
*length = buffer->fill - buffer->offset;
}
return 0;
}
int igloo_buffer_get_string(igloo_buffer_t *buffer, const char **string)
{
char *ret;
if (!buffer || !string)
return -1;
/* Ensure we have space for one additional byte ('\0'-termination). */
if (buffer->length == buffer->fill) {
igloo_buffer_preallocate(buffer, 1);
if (buffer->length == buffer->fill)
return -1;
}
/* Actually add a '\0'-termination. */
ret = buffer->buffer;
ret[buffer->fill] = 0;
*string = ret + buffer->offset;
return 0;
}
int igloo_buffer_set_length(igloo_buffer_t *buffer, size_t length)
{
if (!buffer)
return -1;
if (length > (buffer->fill - buffer->offset))
return -1;
buffer->fill = length + buffer->offset;
return 0;
}
int igloo_buffer_shift(igloo_buffer_t *buffer, size_t amount)
{
if (!buffer)
return -1;
if (amount > (buffer->fill - buffer->offset))
return -1;
buffer->offset += amount;
/* run cleanup */
igloo_buffer_preallocate(buffer, 0);
return 0;
}
int igloo_buffer_push_data(igloo_buffer_t *buffer, const void *data, size_t length)
{
void *buf;
int ret;
if (!buffer)
return -1;
if (!length)
return 0;
if (!data)
return -1;
ret = igloo_buffer_zerocopy_push_request(buffer, &buf, length);
if (ret != 0)
return ret;
memcpy(buf, data, length);
ret = igloo_buffer_zerocopy_push_complete(buffer, length);
return ret;
}
int igloo_buffer_push_string(igloo_buffer_t *buffer, const char *string)
{
if (!buffer || !string)
return -1;
return igloo_buffer_push_data(buffer, string, strlen(string));
}
int igloo_buffer_push_printf(igloo_buffer_t *buffer, const char *format, ...)
{
int ret;
va_list ap;
if (!buffer || !format)
return -1;
if (!*format)
return 0;
va_start(ap, format);
ret = igloo_buffer_push_vprintf(buffer, format, ap);
va_end(ap);
return ret;
}
int igloo_buffer_push_vprintf(igloo_buffer_t *buffer, const char *format, va_list ap)
{
void *buf;
int ret;
size_t length = 1024;
if (!buffer || !format)
return -1;
if (!*format)
return 0;
ret = igloo_buffer_zerocopy_push_request(buffer, &buf, length);
if (ret != 0)
return ret;
ret = vsnprintf(buf, length, format, ap);
if (ret >= 0 && (size_t)ret < length) {
return igloo_buffer_zerocopy_push_complete(buffer, ret);
} else if (ret < 0) {
/* This vsnprintf() likely does not follow POSIX.
* We don't know what length we need to asume. So asume a big one and hope for the best. */
length = 8192;
} else {
/* Reallocate the buffer to the size reported plus one for '\0'-termination */
length = ret + 1;
}
/* We have not written any data yet. */
ret = igloo_buffer_zerocopy_push_complete(buffer, 0);
if (ret != 0)
return ret;
/* Now let's try again. */
ret = igloo_buffer_zerocopy_push_request(buffer, &buf, length);
if (ret != 0)
return ret;
ret = vsnprintf(buf, length, format, ap);
if (ret < 0 || (size_t)ret >= length) {
/* This still didn't work. Giving up. */
igloo_buffer_zerocopy_push_complete(buffer, 0);
return -1;
}
return igloo_buffer_zerocopy_push_complete(buffer, ret);
}
int igloo_buffer_push_buffer(igloo_buffer_t *buffer, igloo_buffer_t *source)
{
const void *data;
size_t length;
int ret;
if (!buffer || !source)
return -1;
ret = igloo_buffer_get_data(source, &data, &length);
if (ret != 0)
return ret;
return igloo_buffer_push_data(buffer, data, length);
}
int igloo_buffer_zerocopy_push_request(igloo_buffer_t *buffer, void **data, size_t request)
{
if (!buffer || !data)
return -1;
igloo_buffer_preallocate(buffer, request);
if (request > (buffer->length - buffer->fill))
return -1;
*data = buffer->buffer + buffer->fill;
return 0;
}
int igloo_buffer_zerocopy_push_complete(igloo_buffer_t *buffer, size_t done)
{
if (!buffer)
return -1;
if (done > (buffer->length - buffer->fill))
return -1;
buffer->fill += done;
return 0;
}