]> rtime.felk.cvut.cz Git - ulut.git/blob - ulut/ul_cbuff.c
Added constructor, destructor and always_inline attributes to the uLUt definitions.
[ulut.git] / ulut / ul_cbuff.c
1 /*******************************************************************
2   uLan Utilities Library - C library of basic reusable constructions
3
4   ul_cbuff.c    - circular buffer
5
6   (C) Copyright 2005-2006 by Pavel Pisa - Originator
7
8   The uLan utilities library can be used, copied and modified under
9   next licenses
10     - GPL - GNU General Public License
11     - LGPL - GNU Lesser General Public License
12     - MPL - Mozilla Public License
13     - and other licenses added by project originators
14   Code can be modified and re-distributed under any combination
15   of the above listed licenses. If contributor does not agree with
16   some of the licenses, he/she can delete appropriate line.
17   Warning, if you delete all lines, you are not allowed to
18   distribute source code and/or binaries utilizing code.
19
20   See files COPYING and README for details.
21
22  *******************************************************************/
23
24 #include <string.h>
25 #include "ul_utmalloc.h"
26
27 #define UL_CBUFF_INCLUDE_INTERNAL
28
29 #include "ul_cbuff.h"
30
31 int ul_cbuff_init(ul_cbuff_t *buff, ul_cbuff_state_t *state,
32                   void *buff_start, ul_cbuff_loc_t size)
33 {
34   ul_cbuff_msg_head_t *msg_head;
35   int state_alloc=(state==NULL);
36
37   if(state_alloc){
38     state=(ul_cbuff_state_t *)malloc(sizeof(ul_cbuff_state_t));
39     if(state==NULL)
40       return -1;
41   }
42
43   if(buff_start!=NULL){
44     size=ul_cbuff_align(size+1)-ul_cbuff_align(1);
45   }else{
46     size=ul_cbuff_align(size);
47     buff_start=malloc(size);
48     if(buff_start==NULL){
49       if(state_alloc)
50         free(state);
51       return -1;
52     }
53   }
54
55   state->buff_size=size;
56   state->head=0;
57   state->lasttail=0;
58   state->cycles=0;
59   state->readers=0;
60   state->rear_size=0;
61
62   buff->state=state;
63   buff->buff_start=(unsigned char *)buff_start;
64
65   msg_head=(ul_cbuff_msg_head_t *)buff_start;
66   msg_head->flags=0;
67
68   return 0;
69 }