27 */ |
27 */ |
28 |
28 |
29 #include "cx/buffer.h" |
29 #include "cx/buffer.h" |
30 #include "cx/utils.h" |
30 #include "cx/utils.h" |
31 |
31 |
32 #include <stdlib.h> |
|
33 #include <stdio.h> |
32 #include <stdio.h> |
34 #include <string.h> |
33 #include <string.h> |
35 #include <stdint.h> |
|
36 |
34 |
37 int cxBufferInit( |
35 int cxBufferInit( |
38 CxBuffer *buffer, |
36 CxBuffer *buffer, |
39 void *space, |
37 void *space, |
40 size_t capacity, |
38 size_t capacity, |
41 CxAllocator const *allocator, |
39 CxAllocator const *allocator, |
42 int flags |
40 int flags |
43 ) { |
41 ) { |
|
42 if (allocator == NULL) allocator = cxDefaultAllocator; |
44 buffer->allocator = allocator; |
43 buffer->allocator = allocator; |
45 buffer->flags = flags; |
44 buffer->flags = flags; |
46 if (!space) { |
45 if (!space) { |
47 buffer->bytes = cxMalloc(allocator, capacity); |
46 buffer->bytes = cxMalloc(allocator, capacity); |
48 if (buffer->bytes == NULL) { |
47 if (buffer->bytes == NULL) { |
67 |
66 |
68 void cxBufferDestroy(CxBuffer *buffer) { |
67 void cxBufferDestroy(CxBuffer *buffer) { |
69 if ((buffer->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS) { |
68 if ((buffer->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS) { |
70 cxFree(buffer->allocator, buffer->bytes); |
69 cxFree(buffer->allocator, buffer->bytes); |
71 } |
70 } |
|
71 } |
|
72 |
|
73 CxBuffer *cxBufferCreate( |
|
74 void *space, |
|
75 size_t capacity, |
|
76 CxAllocator const *allocator, |
|
77 int flags |
|
78 ) { |
|
79 CxBuffer *buf = cxMalloc(allocator, sizeof(CxBuffer)); |
|
80 if (buf == NULL) return NULL; |
|
81 if (0 == cxBufferInit(buf, space, capacity, allocator, flags)) { |
|
82 return buf; |
|
83 } else { |
|
84 cxFree(allocator, buf); |
|
85 return NULL; |
|
86 } |
|
87 } |
|
88 |
|
89 void cxBufferFree(CxBuffer *buffer) { |
|
90 if ((buffer->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS) { |
|
91 cxFree(buffer->allocator, buffer->bytes); |
|
92 } |
|
93 cxFree(buffer->allocator, buffer); |
72 } |
94 } |
73 |
95 |
74 int cxBufferSeek( |
96 int cxBufferSeek( |
75 CxBuffer *buffer, |
97 CxBuffer *buffer, |
76 off_t offset, |
98 off_t offset, |