Server 0.0.1
HTTP/REST server implementation

include/cbuf.h File Reference

#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include "class.h"
#include "stream.h"
#include "commons.h"
Include dependency graph for cbuf.h:

Go to the source code of this file.

Data Structures

struct  Cbuf

Defines

#define ECBUFOVFL   100

Functions

ssize_t cbufRead (Cbuf, Stream)
ssize_t cbufWrite (Cbuf, Stream)
char * cbufGetLine (Cbuf, char **)
char * cbufGetData (Cbuf, size_t)
char * cbufSetData (Cbuf, const void *, size_t)
void cbufEmpty (Cbuf)
char * cbufGetRead (Cbuf this)
char * cbufGetWrite (Cbuf this)
char * cbufMemchr (Cbuf this, int c)
size_t cbufAddrIndex (Cbuf this, const void *c)
void cbufIncRead (Cbuf this, size_t n)
void cbufIncWrite (Cbuf this, size_t n)
size_t cbufGetFree (Cbuf this)
char cbufIsEmpty (Cbuf this)
void cbufSkipNonAlpha (Cbuf this)
Bool cbufIsLocked (Cbuf this)
void cbufLock (Cbuf this)
void cbufRelease (Cbuf this)

Detailed Description

My implementation of a ringbuffer. It maps a shared memory object twice directly following thus make it possible to read and write from any position within the buffer without the nasty wrap calculations. This is achived because the same memory region is mapped at the two addresses.

Author:
Georg Hopp

Copyright © 2012 Georg Hopp

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Definition in file cbuf.h.


Define Documentation

#define ECBUFOVFL   100

Definition at line 42 of file cbuf.h.


Function Documentation

size_t cbufAddrIndex ( Cbuf  this,
const void *  c 
)

Definition at line 28 of file addr_index.c.

{
        return c - (const void *)cbufGetRead(this);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void cbufEmpty ( Cbuf  )

Definition at line 26 of file empty.c.

{
        this->bused = 0;
        this->read  = this->write;
}
char* cbufGetData ( Cbuf  ,
size_t   
)

Definition at line 29 of file get_data.c.

{
        char * ret = cbufGetRead(this);

        if (n > this->bused) {
                return (char *)-1;
        }

        cbufIncRead(this, n);
        return ret;
}

Here is the call graph for this function:

Here is the caller graph for this function:

size_t cbufGetFree ( Cbuf  this)

Definition at line 28 of file get_free.c.

{
        return this->bsize - this->bused;
}

Here is the caller graph for this function:

char* cbufGetLine ( Cbuf  ,
char **   
)

Definition at line 30 of file get_line.c.

{
        char * nl  = cbufMemchr(this, '\n');
        char * ret = NULL;

        if (NULL != nl) {
                size_t len = cbufAddrIndex(this, nl) + 1;

                *line_end = nl - 1;
                *nl       = 0;
                *(nl-1)   = ('\r' == *(nl-1))? 0 : *(nl-1);

                ret = cbufGetRead(this);
                cbufIncRead(this, len);
        }

        return ret;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* cbufGetRead ( Cbuf  this)

Definition at line 26 of file get_read.c.

{
        return this->data + this->read;
}

Here is the caller graph for this function:

char* cbufGetWrite ( Cbuf  this)

Definition at line 26 of file get_write.c.

{
        return this->data + this->write;
}

Here is the caller graph for this function:

void cbufIncRead ( Cbuf  this,
size_t  n 
)

Definition at line 28 of file inc_read.c.

{
        this->read  += n;
        this->read   = (this->read >= this->bsize)?
                this->read - this->bsize : this->read;
        this->bused -= n;
}

Here is the caller graph for this function:

void cbufIncWrite ( Cbuf  this,
size_t  n 
)

Definition at line 28 of file inc_write.c.

{
        this->write += n;
        this->write  = (this->write >= this->bsize)?
                this->write - this->bsize : this->write;
        this->bused += n;
}

Here is the caller graph for this function:

char cbufIsEmpty ( Cbuf  this)

Definition at line 26 of file is_empty.c.

{
        return (0 == this->bused);
}

Here is the caller graph for this function:

Bool cbufIsLocked ( Cbuf  this)

Definition at line 28 of file is_locked.c.

{
        return this->lock;
}

Here is the caller graph for this function:

void cbufLock ( Cbuf  this)

Definition at line 26 of file lock.c.

{
        this->lock = TRUE;
}

Here is the caller graph for this function:

char* cbufMemchr ( Cbuf  this,
int  c 
)

Definition at line 28 of file memchr.c.

{
        return memchr(cbufGetRead(this), c, this->bused);
}

Here is the call graph for this function:

Here is the caller graph for this function:

ssize_t cbufRead ( Cbuf  ,
Stream   
)

Definition at line 32 of file read.c.

{
        ssize_t rrsize = 0;
        size_t  rsize  = cbufGetFree(this);

        if (0 == rsize) {
                errno = ECBUFOVFL;
                return -1;
        }

        rrsize = streamRead(st, cbufGetWrite(this), rsize);

        switch (rrsize) {
                case  0:
                        rrsize = -2;
                        // DROP THROUGH

                case -1:
                        break;

                default:
                        cbufIncWrite(this, rrsize);
                        break;
        }

        return rrsize;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void cbufRelease ( Cbuf  this)

Definition at line 26 of file release.c.

{
        this->lock = FALSE;
}

Here is the caller graph for this function:

char* cbufSetData ( Cbuf  ,
const void *  ,
size_t   
)

Definition at line 30 of file set_data.c.

{
        char * addr;

        if (n > cbufGetFree(this)) {
                errno = ECBUFOVFL;
                return (char *)-1;
        }

        addr = memcpy(cbufGetWrite(this), src, n);
        cbufIncWrite(this, n);

        return addr;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void cbufSkipNonAlpha ( Cbuf  this)

Definition at line 28 of file skip_non_alpha.c.

{
        while(0 < this->bused && !isalpha(*cbufGetRead(this)))
                cbufIncRead(this, 1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

ssize_t cbufWrite ( Cbuf  ,
Stream   
)

Definition at line 31 of file write.c.

{
        ssize_t wwsize = 0;
        size_t  wsize  = this->bused;

        if (0 == wsize) return 0;

        wwsize = streamWrite(st, cbufGetRead(this), wsize);

        switch (wwsize) {
                case -1:
                        break;

                default:
                        cbufIncRead(this, wwsize);
                        break;
        }

        return wwsize;
}

Here is the call graph for this function:

Here is the caller graph for this function:

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines