Coconut Framework  beta
CNBuffer.h
Go to the documentation of this file.
1 
8 #ifndef CNBUFFER_H
9 #define CNBUFFER_H
10 
11 #include <stdlib.h>
12 #include <string.h>
13 
17 struct CNBuffer
18 {
20  size_t writePosition ;
22  size_t currentSize ;
24  unsigned char * context ;
25 } ;
26 
35 static inline size_t
36 CNCalcSizeOfBuffer(size_t size)
37 {
38  static const size_t CNUnitSizeOfBuffer = 512 ;
39  if(size > 0){
40  size_t s0 = (size + CNUnitSizeOfBuffer - 1) / CNUnitSizeOfBuffer ;
41  return s0 * CNUnitSizeOfBuffer ;
42  } else {
43  return CNUnitSizeOfBuffer ;
44  }
45 }
46 
51 static inline void
52 CNInitBuffer(struct CNBuffer * dst)
53 {
54  dst->writePosition = 0 ;
56  dst->context = malloc(dst->currentSize) ;
57 }
58 
63 static inline void
65 {
66  free(dst->context) ;
67 }
68 
73 static inline void
74 CNResetBuffer(struct CNBuffer * dst)
75 {
76  dst->writePosition = 0 ;
77 }
78 
88 static inline void *
90 {
91  void * result = realloc(dst->context, dst->currentSize) ;
92  dst->writePosition = 0 ;
93  dst->currentSize = 0 ;
94  dst->context = NULL ;
95  return result ;
96 }
97 
103 static inline size_t
104 CNSizeOfBuffer(const struct CNBuffer * src)
105 {
106  return src->writePosition ;
107 }
108 
114 static inline const void *
115 CNContextOfBuffer(const struct CNBuffer * src)
116 {
117  return src->context ;
118 }
119 
126 static inline void
127 CNPutBytesToBuffer(struct CNBuffer * dst, size_t srcsize, const void * srcptr)
128 {
129  size_t writepos = dst->writePosition ;
130  size_t nextpos = writepos + srcsize ;
131  if(dst->currentSize < nextpos){
132  size_t nextsize = CNCalcSizeOfBuffer(nextpos) ;
133  dst->context = realloc(dst->context, nextsize) ;
134  dst->currentSize = nextsize ;
135  }
136  unsigned char * dstptr = &(dst->context[writepos]) ;
137  memcpy(dstptr, srcptr, srcsize) ;
138  dst->writePosition = nextpos ;
139 }
140 
141 #endif /* CNBUFFER_H */
Write buffer to store variable-sized continuous data.
Definition: CNBuffer.h:17
static void CNPutBytesToBuffer(struct CNBuffer *dst, size_t srcsize, const void *srcptr)
Write byte data into the buffer.
Definition: CNBuffer.h:127
static size_t CNCalcSizeOfBuffer(size_t size)
Calc memory size of CNBuffer.
Definition: CNBuffer.h:36
static void CNResetBuffer(struct CNBuffer *dst)
Make the buffer empty.
Definition: CNBuffer.h:74
static void CNDestroyBuffer(struct CNBuffer *dst)
Destroy the context of CNBuffer.
Definition: CNBuffer.h:64
size_t writePosition
Current size of the buffer.
Definition: CNBuffer.h:20
static size_t CNSizeOfBuffer(const struct CNBuffer *src)
Get the size of valid data in CNBuffer.
Definition: CNBuffer.h:104
static void CNInitBuffer(struct CNBuffer *dst)
Initialize CNBuffer object.
Definition: CNBuffer.h:52
size_t currentSize
The position to put the next data.
Definition: CNBuffer.h:22
unsigned char * context
Pointer for the buffer context.
Definition: CNBuffer.h:24
static void * CNDetachDataFromBuffer(struct CNBuffer *dst)
Detach context from the buffer.
Definition: CNBuffer.h:89
static const void * CNContextOfBuffer(const struct CNBuffer *src)
Get the context of CNBuffer.
Definition: CNBuffer.h:115