taskrambler  v0.1.8
Web server and task management solution.
cbuf.h File Reference
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include "class.h"
#include "stream.h"
#include "commons.h"
#include "utils/memory.h"
+ Include dependency graph for cbuf.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define ECBUFOVFL   100
 

Functions

 CLASS (Cbuf)
 
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

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.

Macro Definition Documentation

#define ECBUFOVFL   100

Definition at line 43 of file cbuf.h.

Referenced by cbufRead(), and cbufSetData().

Function Documentation

size_t cbufAddrIndex ( Cbuf  this,
const void *  c 
)

Definition at line 28 of file addr_index.c.

References cbufGetRead().

Referenced by cbufGetLine().

29 {
30  return c - (const void *)cbufGetRead(this);
31 }
char * cbufGetRead(Cbuf this)
Definition: get_read.c:26

+ 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.

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

Definition at line 29 of file get_data.c.

References cbufGetRead(), and cbufIncRead().

Referenced by httpParserParse().

30 {
31  char * ret = cbufGetRead(this);
32 
33  if (n > this->bused) {
34  return (char *)-1;
35  }
36 
37  cbufIncRead(this, n);
38  return ret;
39 }
void cbufIncRead(Cbuf this, size_t n)
Definition: inc_read.c:28
char * cbufGetRead(Cbuf this)
Definition: get_read.c:26

+ 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.

Referenced by cbufRead(), and cbufSetData().

29 {
30  return this->bsize - this->bused;
31 }

+ Here is the caller graph for this function:

char* cbufGetLine ( Cbuf  ,
char **   
)

Definition at line 30 of file get_line.c.

References cbufAddrIndex(), cbufGetRead(), cbufIncRead(), and cbufMemchr().

Referenced by httpParserParse().

31 {
32  char * nl = cbufMemchr(this, '\n');
33  char * ret = NULL;
34 
35  if (NULL != nl) {
36  size_t len = cbufAddrIndex(this, nl) + 1;
37 
38  *line_end = nl - 1;
39  *nl = 0;
40  *(nl-1) = ('\r' == *(nl-1))? 0 : *(nl-1);
41 
42  ret = cbufGetRead(this);
43  cbufIncRead(this, len);
44  }
45 
46  return ret;
47 }
char * cbufMemchr(Cbuf this, int c)
Definition: memchr.c:28
size_t cbufAddrIndex(Cbuf this, const void *c)
Definition: addr_index.c:28
void cbufIncRead(Cbuf this, size_t n)
Definition: inc_read.c:28
char * cbufGetRead(Cbuf this)
Definition: get_read.c:26

+ 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.

Referenced by cbufAddrIndex(), cbufGetData(), cbufGetLine(), cbufMemchr(), cbufSkipNonAlpha(), and httpParserParse().

27 {
28  return this->data + this->read;
29 }
int data
Definition: binarytree.c:7

+ Here is the caller graph for this function:

char* cbufGetWrite ( Cbuf  this)

Definition at line 26 of file get_write.c.

Referenced by cbufRead(), and cbufSetData().

27 {
28  return this->data + this->write;
29 }
int data
Definition: binarytree.c:7

+ Here is the caller graph for this function:

void cbufIncRead ( Cbuf  this,
size_t  n 
)

Definition at line 28 of file inc_read.c.

Referenced by cbufGetData(), cbufGetLine(), cbufSkipNonAlpha(), and httpParserParse().

29 {
30  this->read += n;
31  this->read = (this->read >= this->bsize)?
32  this->read - this->bsize : this->read;
33  this->bused -= n;
34 }

+ Here is the caller graph for this function:

void cbufIncWrite ( Cbuf  this,
size_t  n 
)

Definition at line 28 of file inc_write.c.

Referenced by cbufRead(), and cbufSetData().

29 {
30  this->write += n;
31  this->write = (this->write >= this->bsize)?
32  this->write - this->bsize : this->write;
33  this->bused += n;
34 }

+ Here is the caller graph for this function:

char cbufIsEmpty ( Cbuf  this)

Definition at line 26 of file is_empty.c.

Referenced by httpParserParse().

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

+ Here is the caller graph for this function:

Bool cbufIsLocked ( Cbuf  this)

Definition at line 28 of file is_locked.c.

Referenced by httpParserParse().

29 {
30  return this->lock;
31 }

+ Here is the caller graph for this function:

void cbufLock ( Cbuf  this)

Definition at line 26 of file lock.c.

References TRUE.

Referenced by httpParserParse().

27 {
28  this->lock = TRUE;
29 }
#define TRUE
Definition: commons.h:27

+ Here is the caller graph for this function:

char* cbufMemchr ( Cbuf  this,
int  c 
)

Definition at line 28 of file memchr.c.

References cbufGetRead().

Referenced by cbufGetLine().

29 {
30  return memchr(cbufGetRead(this), c, this->bused);
31 }
char * cbufGetRead(Cbuf this)
Definition: get_read.c:26

+ 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 cbuf/read.c.

References cbufGetFree(), cbufGetWrite(), cbufIncWrite(), ECBUFOVFL, and streamRead().

Referenced by httpParserParse().

33 {
34  size_t rsize = cbufGetFree(this);
35  ssize_t rrsize;
36 
37  if (0 == rsize) {
38  errno = ECBUFOVFL;
39  return -1;
40  }
41 
42  rrsize = streamRead(st, cbufGetWrite(this), rsize);
43 
44  if (0 < rrsize) {
45  cbufIncWrite(this, rrsize);
46  }
47 
48  return rrsize;
49 }
size_t cbufGetFree(Cbuf this)
Definition: get_free.c:28
#define ECBUFOVFL
Definition: cbuf.h:43
ssize_t streamRead(Stream, void *, size_t)
Definition: stream/read.c:35
char * cbufGetWrite(Cbuf this)
Definition: get_write.c:26
void cbufIncWrite(Cbuf this, size_t n)
Definition: inc_write.c:28

+ 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.

References FALSE.

Referenced by httpParserDtor(), and httpParserParse().

27 {
28  this->lock = FALSE;
29 }
#define FALSE
Definition: commons.h:28

+ Here is the caller graph for this function:

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

Definition at line 30 of file set_data.c.

References cbufGetFree(), cbufGetWrite(), cbufIncWrite(), and ECBUFOVFL.

Referenced by httpParserParse().

31 {
32  char * addr;
33 
34  if (n > cbufGetFree(this)) {
35  errno = ECBUFOVFL;
36  return (char *)-1;
37  }
38 
39  addr = memcpy(cbufGetWrite(this), src, n);
40  cbufIncWrite(this, n);
41 
42  return addr;
43 }
size_t cbufGetFree(Cbuf this)
Definition: get_free.c:28
#define ECBUFOVFL
Definition: cbuf.h:43
char * cbufGetWrite(Cbuf this)
Definition: get_write.c:26
void cbufIncWrite(Cbuf this, size_t n)
Definition: inc_write.c:28

+ 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.

References cbufGetRead(), and cbufIncRead().

Referenced by httpParserParse().

29 {
30  while(0 < this->bused && !isalpha(*cbufGetRead(this)))
31  cbufIncRead(this, 1);
32 }
void cbufIncRead(Cbuf this, size_t n)
Definition: inc_read.c:28
char * cbufGetRead(Cbuf this)
Definition: get_read.c:26

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ssize_t cbufWrite ( Cbuf  ,
Stream   
)
CLASS ( Cbuf  )

Definition at line 46 of file cbuf.h.

References Bool, and element::data.

46  {
47  char * shm_name; // shared memory identifier
48 
49  char * data;
50  Bool lock;
51 
52  size_t bsize;
53  size_t bused;
54 
55  size_t write;
56  size_t read;
57 };
#define Bool
Definition: commons.h:26
int data
Definition: binarytree.c:7