src/server/ucx/buffer.h

Sun, 30 Jun 2013 15:11:48 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 30 Jun 2013 15:11:48 +0200
changeset 82
740cfd9dd443
parent 71
069c152f6272
permissions
-rw-r--r--

added some nsapi stuff

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2013 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef BUFFER_H
#define	BUFFER_H

#include "ucx.h"
#include <sys/types.h>
#include <stdio.h>

#ifdef	__cplusplus
extern "C" {
#endif

/* no autoextend or autofree behaviour */
#define UCX_BUFFER_DEFAULT      0x00
/* the buffer shall free the occupied memory space */
#define UCX_BUFFER_AUTOFREE     0x01
/* the buffer may automatically double its size on write operations */
#define UCX_BUFFER_AUTOEXTEND   0x02

/* the user shall not modify values, but may get the latest pointer */
typedef struct {
    char *space;
    size_t pos;
    size_t capacity;
    size_t size;
    int flags;
} UcxBuffer;

/* if space is NULL, new space is allocated and the autofree flag is enforced */
UcxBuffer *ucx_buffer_new(void *space, size_t size, int flags);
void ucx_buffer_free(UcxBuffer* buffer);

/*
 * the autofree flag is enforced for the new buffer
 * if length is zero, the whole remaining buffer shall be extracted
 * the position of the new buffer is set to zero
 */
UcxBuffer* ucx_buffer_extract(UcxBuffer *src,
        size_t start, size_t length, int flags);
#define ucx_buffer_clone(src,flags) \
    ucx_buffer_extract(src, 0, 0, flags)

/*
 * Moves the position of the buffer to a new position relative to whence.
 *
 * SEEK_SET marks the start of the buffer
 * SEEK_CUR marks the current position
 * SEEK_END marks the first 0-byte in the buffer
 *
 * ucx_buffer_seek returns 0 on success and -1 if the new position is beyond the
 * bounds of the allocated buffer. In that case the position of the buffer
 * remains unchanged.
 *
 */
int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence);

#define ucx_buffer_clear(buffer)    memset(buffer->space, 0, buffer->size); \
                                    buffer->size = 0; buffer->pos = 0;

/*
 * returns non-zero, if the current buffer position has exceeded the last
 * available byte of the underlying buffer
 *
 */
int ucx_buffer_eof(UcxBuffer *buffer);


int ucx_buffere_extend(UcxBuffer *buffer, size_t len);

size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems,
        UcxBuffer *buffer);

size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems,
        UcxBuffer *buffer);

int ucx_buffer_putc(UcxBuffer *b, int c);
int ucx_buffer_getc(UcxBuffer *b);


/*
 * copies all bytes from s1 to s2
 * uses the read function r to read from s1 und writes the data using the
 * write function w to s2
 * returns the number of bytes copied
 */
size_t ucx_buffer_generic_copy(void *s1, void *s2, read_func r, write_func w,
        size_t bufsize);

size_t ucx_buffer_generic_ncopy(void *s1, void *s2, read_func r, write_func w,
        size_t bufsize, size_t n);

#define UCX_DEFAULT_BUFFER_SIZE 0x1000

#define ucx_buffer_copy(s1,s2,r,w) \
    ucx_buffer_generic_copy(s1, s2, (read_func)r, (write_func)w, \
    UCX_DEFAULT_BUFFER_SIZE)

#define ucx_buffer_ncopy(s1,s2,r,w, n) \
    ucx_buffer_generic_ncopy(s1, s2, (read_func)r, (write_func)w, \
    UCX_DEFAULT_BUFFER_SIZE, n)

#ifdef	__cplusplus
}
#endif

#endif	/* BUFFER_H */

mercurial