src/buffer.c

Sun, 05 Jan 2025 18:19:42 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 05 Jan 2025 18:19:42 +0100
changeset 1110
a0e9be7ed131
parent 1065
6eb7b54975ee
permissions
-rw-r--r--

re-implement flushing

fixes #542 fixes #543

483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "cx/buffer.h"
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
530
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents: 529
diff changeset
31 #include <stdio.h>
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 #include <string.h>
1040
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 1030
diff changeset
33 #include <errno.h>
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34
1028
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
35 static int buffer_copy_on_write(CxBuffer* buffer) {
1024
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
36 if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) return 0;
1028
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
37 void *newspace = cxMalloc(buffer->allocator, buffer->capacity);
1024
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
38 if (NULL == newspace) return -1;
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
39 memcpy(newspace, buffer->space, buffer->size);
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
40 buffer->space = newspace;
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
41 buffer->flags &= ~CX_BUFFER_COPY_ON_WRITE;
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
42 buffer->flags |= CX_BUFFER_FREE_CONTENTS;
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
43 return 0;
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
44 }
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
45
501
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
46 int cxBufferInit(
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
47 CxBuffer *buffer,
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 void *space,
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 size_t capacity,
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 761
diff changeset
50 const CxAllocator *allocator,
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 int flags
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 ) {
989
8aa57a7fecc4 improve consistency for allocator arguments - fixes #485
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
53 if (allocator == NULL) {
8aa57a7fecc4 improve consistency for allocator arguments - fixes #485
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
54 allocator = cxDefaultAllocator;
8aa57a7fecc4 improve consistency for allocator arguments - fixes #485
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
55 }
1028
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
56 if (flags & CX_BUFFER_COPY_ON_EXTEND) {
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
57 flags |= CX_BUFFER_AUTO_EXTEND;
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
58 }
501
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
59 buffer->allocator = allocator;
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
60 buffer->flags = flags;
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
61 if (!space) {
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
62 buffer->bytes = cxMalloc(allocator, capacity);
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
63 if (buffer->bytes == NULL) {
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
64 return -1; // LCOV_EXCL_LINE
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 }
501
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
66 buffer->flags |= CX_BUFFER_FREE_CONTENTS;
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
67 } else {
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
68 buffer->bytes = space;
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
69 }
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
70 buffer->capacity = capacity;
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
71 buffer->size = 0;
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
72 buffer->pos = 0;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
74 buffer->flush = NULL;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
75
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
76 return 0;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
77 }
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
79 int cxBufferEnableFlushing(
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
80 CxBuffer *buffer,
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
81 CxBufferFlushConfig config
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
82 ) {
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
83 buffer->flush = malloc(sizeof(CxBufferFlushConfig));
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
84 if (buffer->flush == NULL) return -1; // LCOV_EXCL_LINE
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
85 memcpy(buffer->flush, &config, sizeof(CxBufferFlushConfig));
501
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
86 return 0;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
89 void cxBufferDestroy(CxBuffer *buffer) {
1024
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
90 if (buffer->flags & CX_BUFFER_FREE_CONTENTS) {
501
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
91 cxFree(buffer->allocator, buffer->bytes);
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 }
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
93 free(buffer->flush);
1030
06091e067bee add cxBufferAppend() - fixes #534
Mike Becker <universe@uap-core.de>
parents: 1028
diff changeset
94 memset(buffer, 0, sizeof(CxBuffer));
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96
683
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
97 CxBuffer *cxBufferCreate(
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
98 void *space,
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
99 size_t capacity,
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 761
diff changeset
100 const CxAllocator *allocator,
683
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
101 int flags
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
102 ) {
989
8aa57a7fecc4 improve consistency for allocator arguments - fixes #485
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
103 if (allocator == NULL) {
8aa57a7fecc4 improve consistency for allocator arguments - fixes #485
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
104 allocator = cxDefaultAllocator;
8aa57a7fecc4 improve consistency for allocator arguments - fixes #485
Mike Becker <universe@uap-core.de>
parents: 985
diff changeset
105 }
683
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
106 CxBuffer *buf = cxMalloc(allocator, sizeof(CxBuffer));
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
107 if (buf == NULL) return NULL;
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
108 if (0 == cxBufferInit(buf, space, capacity, allocator, flags)) {
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
109 return buf;
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
110 } else {
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
111 // LCOV_EXCL_START
683
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
112 cxFree(allocator, buf);
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
113 return NULL;
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
114 // LCOV_EXCL_STOP
683
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
115 }
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
116 }
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
117
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
118 void cxBufferFree(CxBuffer *buffer) {
985
68754c7de906 major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents: 970
diff changeset
119 if (buffer == NULL) return;
1030
06091e067bee add cxBufferAppend() - fixes #534
Mike Becker <universe@uap-core.de>
parents: 1028
diff changeset
120 const CxAllocator *allocator = buffer->allocator;
991
ec49db06dae0 cxBufferFree() now calls cxBufferDestroy()
Mike Becker <universe@uap-core.de>
parents: 989
diff changeset
121 cxBufferDestroy(buffer);
1030
06091e067bee add cxBufferAppend() - fixes #534
Mike Becker <universe@uap-core.de>
parents: 1028
diff changeset
122 cxFree(allocator, buffer);
683
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
123 }
aa0d09f2d81c add convenience functions for allocating a buffer on the heap
Mike Becker <universe@uap-core.de>
parents: 673
diff changeset
124
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 int cxBufferSeek(
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
126 CxBuffer *buffer,
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 off_t offset,
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 int whence
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 ) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 size_t npos;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 switch (whence) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 case SEEK_CUR:
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 npos = buffer->pos;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 break;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 case SEEK_END:
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 npos = buffer->size;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 break;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 case SEEK_SET:
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 npos = 0;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 break;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 default:
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 return -1;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 size_t opos = npos;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 npos += offset;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) {
1040
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 1030
diff changeset
149 errno = EOVERFLOW;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 return -1;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152
1007
81b2986d2b04 fix that cxBufferSeek() cannot move pos past the end - fixes #523
Mike Becker <universe@uap-core.de>
parents: 1005
diff changeset
153 if (npos > buffer->size) {
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 return -1;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 } else {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 buffer->pos = npos;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 return 0;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161
529
814d51173f20 #171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents: 501
diff changeset
162 void cxBufferClear(CxBuffer *buffer) {
1024
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
163 if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) {
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
164 memset(buffer->bytes, 0, buffer->size);
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
165 }
529
814d51173f20 #171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents: 501
diff changeset
166 buffer->size = 0;
814d51173f20 #171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents: 501
diff changeset
167 buffer->pos = 0;
814d51173f20 #171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents: 501
diff changeset
168 }
814d51173f20 #171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents: 501
diff changeset
169
761
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
170 void cxBufferReset(CxBuffer *buffer) {
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
171 buffer->size = 0;
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
172 buffer->pos = 0;
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
173 }
61d5197d612b add cxBufferReset() - resolves #338
Mike Becker <universe@uap-core.de>
parents: 683
diff changeset
174
1004
d8044fd5005c make cxBufferEof() return a bool
Mike Becker <universe@uap-core.de>
parents: 991
diff changeset
175 bool cxBufferEof(const CxBuffer *buffer) {
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 return buffer->pos >= buffer->size;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 int cxBufferMinimumCapacity(
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
180 CxBuffer *buffer,
532
080c304ec176 fix cxBufferMinimumCapacity implementation still using additional_bytes
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
181 size_t newcap
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 ) {
532
080c304ec176 fix cxBufferMinimumCapacity implementation still using additional_bytes
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
183 if (newcap <= buffer->capacity) {
080c304ec176 fix cxBufferMinimumCapacity implementation still using additional_bytes
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
184 return 0;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186
1028
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
187 const int force_copy_flags = CX_BUFFER_COPY_ON_WRITE | CX_BUFFER_COPY_ON_EXTEND;
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
188 if (buffer->flags & force_copy_flags) {
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
189 void *newspace = cxMalloc(buffer->allocator, newcap);
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
190 if (NULL == newspace) return -1;
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
191 memcpy(newspace, buffer->space, buffer->size);
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
192 buffer->space = newspace;
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
193 buffer->capacity = newcap;
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
194 buffer->flags &= ~force_copy_flags;
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
195 buffer->flags |= CX_BUFFER_FREE_CONTENTS;
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
196 return 0;
1024
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
197 } else if (cxReallocate(buffer->allocator,
536
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 534
diff changeset
198 (void **) &buffer->bytes, newcap) == 0) {
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 buffer->capacity = newcap;
533
8d70ad8da899 do not implicitly zero freshly allocated memory
Mike Becker <universe@uap-core.de>
parents: 532
diff changeset
200 return 0;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201 } else {
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
202 return -1; // LCOV_EXCL_LINE
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
206 static size_t cx_buffer_flush_helper(
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
207 const CxBuffer *buffer,
544
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
208 size_t size,
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
209 const unsigned char *src,
544
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
210 size_t nitems
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
211 ) {
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
212 // flush data from an arbitrary source
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
213 // does not need to be the buffer's contents
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
214 size_t max_items = buffer->flush->blksize / size;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
215 size_t fblocks = 0;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
216 size_t flushed_total = 0;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
217 while (nitems > 0 && fblocks < buffer->flush->blkmax) {
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
218 fblocks++;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
219 size_t items = nitems > max_items ? max_items : nitems;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
220 size_t flushed = buffer->flush->wfunc(
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
221 src, size, items, buffer->flush->target);
544
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
222 if (flushed > 0) {
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
223 flushed_total += flushed;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
224 src += flushed * size;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
225 nitems -= flushed;
544
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
226 } else {
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
227 // if no bytes can be flushed out anymore, we give up
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
228 break;
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
229 }
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
230 }
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
231 return flushed_total;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
232 }
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
233
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
234 static size_t cx_buffer_flush_impl(CxBuffer *buffer, size_t size) {
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
235 // flush the current contents of the buffer
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
236 unsigned char *space = buffer->bytes;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
237 size_t remaining = buffer->pos / size;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
238 size_t flushed_total = cx_buffer_flush_helper(
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
239 buffer, size, space, remaining);
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
240
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
241 // shift the buffer left after flushing
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
242 // IMPORTANT: up to this point, copy on write must have been
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
243 // performed already, because we can't do error handling here
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
244 cxBufferShiftLeft(buffer, flushed_total*size);
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
245
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
246 return flushed_total;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
247 }
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
248
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
249 size_t cxBufferFlush(CxBuffer *buffer) {
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
250 if (buffer_copy_on_write(buffer)) return 0;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
251 return cx_buffer_flush_impl(buffer, 1);
544
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
252 }
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
253
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
254 size_t cxBufferWrite(
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 761
diff changeset
255 const void *ptr,
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
256 size_t size,
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
257 size_t nitems,
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
258 CxBuffer *buffer
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
259 ) {
543
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
260 // optimize for easy case
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
261 if (size == 1 && (buffer->capacity - buffer->pos) >= nitems) {
1028
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
262 if (buffer_copy_on_write(buffer)) return 0;
543
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
263 memcpy(buffer->bytes + buffer->pos, ptr, nitems);
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
264 buffer->pos += nitems;
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
265 if (buffer->pos > buffer->size) {
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
266 buffer->size = buffer->pos;
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
267 }
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
268 return nitems;
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
269 }
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
270
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
271 size_t len;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
272 if (cx_szmul(size, nitems, &len)) {
1040
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 1030
diff changeset
273 errno = EOVERFLOW;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
274 return 0;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
275 }
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
276 if (buffer->pos > SIZE_MAX - len) {
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
277 errno = EOVERFLOW;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
278 return 0;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
279 }
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
280 size_t required = buffer->pos + len;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
281
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
282 bool perform_flush = false;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
283 if (required > buffer->capacity) {
1024
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
284 if (buffer->flags & CX_BUFFER_AUTO_EXTEND) {
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
285 if (buffer->flush != NULL && required > buffer->flush->threshold) {
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
286 perform_flush = true;
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
287 } else {
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
288 if (cxBufferMinimumCapacity(buffer, required)) {
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
289 return 0; // LCOV_EXCL_LINE
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
290 }
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
291 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
292 } else {
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
293 if (buffer->flush != NULL) {
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
294 perform_flush = true;
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
295 } else {
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
296 // truncate data, if we can neither extend nor flush
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
297 len = buffer->capacity - buffer->pos;
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
298 if (size > 1) {
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
299 len -= len % size;
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
300 }
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
301 nitems = len / size;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
302 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
303 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
304 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
305
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
306 // check here and not above because of possible truncation
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
307 if (len == 0) {
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
308 return 0;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
309 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
310
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
311 // check if we need to copy
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
312 if (buffer_copy_on_write(buffer)) return 0;
544
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
313
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
314 // perform the operation
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
315 if (perform_flush) {
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
316 size_t items_flush;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
317 if (buffer->pos == 0) {
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
318 // if we don't have data in the buffer, but are instructed
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
319 // to flush, it means that we are supposed to relay the data
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
320 items_flush = cx_buffer_flush_helper(buffer, size, ptr, nitems);
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
321 if (items_flush == 0) {
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
322 // we needed to flush, but could not flush anything
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
323 // give up and avoid endless trying
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
324 return 0;
544
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
325 }
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
326 size_t ritems = nitems - items_flush;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
327 const unsigned char *rest = ptr;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
328 rest += items_flush * size;
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
329 return items_flush + cxBufferWrite(rest, size, ritems, buffer);
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
330 } else {
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
331 items_flush = cx_buffer_flush_impl(buffer, size);
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
332 if (items_flush == 0) {
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
333 return 0;
544
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
334 }
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
335 return cxBufferWrite(ptr, size, nitems, buffer);
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
336 }
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
337 } else {
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
338 memcpy(buffer->bytes + buffer->pos, ptr, len);
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
339 buffer->pos += len;
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
340 if (buffer->pos > buffer->size) {
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
341 buffer->size = buffer->pos;
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
342 }
1110
a0e9be7ed131 re-implement flushing
Mike Becker <universe@uap-core.de>
parents: 1065
diff changeset
343 return nitems;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
344 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
345
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
346 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
347
1030
06091e067bee add cxBufferAppend() - fixes #534
Mike Becker <universe@uap-core.de>
parents: 1028
diff changeset
348 size_t cxBufferAppend(
06091e067bee add cxBufferAppend() - fixes #534
Mike Becker <universe@uap-core.de>
parents: 1028
diff changeset
349 const void *ptr,
06091e067bee add cxBufferAppend() - fixes #534
Mike Becker <universe@uap-core.de>
parents: 1028
diff changeset
350 size_t size,
06091e067bee add cxBufferAppend() - fixes #534
Mike Becker <universe@uap-core.de>
parents: 1028
diff changeset
351 size_t nitems,
06091e067bee add cxBufferAppend() - fixes #534
Mike Becker <universe@uap-core.de>
parents: 1028
diff changeset
352 CxBuffer *buffer
06091e067bee add cxBufferAppend() - fixes #534
Mike Becker <universe@uap-core.de>
parents: 1028
diff changeset
353 ) {
06091e067bee add cxBufferAppend() - fixes #534
Mike Becker <universe@uap-core.de>
parents: 1028
diff changeset
354 size_t pos = buffer->pos;
06091e067bee add cxBufferAppend() - fixes #534
Mike Becker <universe@uap-core.de>
parents: 1028
diff changeset
355 buffer->pos = buffer->size;
06091e067bee add cxBufferAppend() - fixes #534
Mike Becker <universe@uap-core.de>
parents: 1028
diff changeset
356 size_t written = cxBufferWrite(ptr, size, nitems, buffer);
06091e067bee add cxBufferAppend() - fixes #534
Mike Becker <universe@uap-core.de>
parents: 1028
diff changeset
357 buffer->pos = pos;
06091e067bee add cxBufferAppend() - fixes #534
Mike Becker <universe@uap-core.de>
parents: 1028
diff changeset
358 return written;
06091e067bee add cxBufferAppend() - fixes #534
Mike Becker <universe@uap-core.de>
parents: 1028
diff changeset
359 }
06091e067bee add cxBufferAppend() - fixes #534
Mike Becker <universe@uap-core.de>
parents: 1028
diff changeset
360
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
361 int cxBufferPut(
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
362 CxBuffer *buffer,
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
363 int c
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
364 ) {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
365 c &= 0xFF;
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
366 unsigned char const ch = c;
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
367 if (cxBufferWrite(&ch, 1, 1, buffer) == 1) {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
368 return c;
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
369 } else {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
370 return EOF;
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
371 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
372 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
373
1005
67fa386ce600 add cxBufferTerminate()
Mike Becker <universe@uap-core.de>
parents: 1004
diff changeset
374 int cxBufferTerminate(CxBuffer *buffer) {
67fa386ce600 add cxBufferTerminate()
Mike Becker <universe@uap-core.de>
parents: 1004
diff changeset
375 bool success = 0 == cxBufferPut(buffer, 0);
67fa386ce600 add cxBufferTerminate()
Mike Becker <universe@uap-core.de>
parents: 1004
diff changeset
376 if (success) {
67fa386ce600 add cxBufferTerminate()
Mike Becker <universe@uap-core.de>
parents: 1004
diff changeset
377 buffer->pos--;
67fa386ce600 add cxBufferTerminate()
Mike Becker <universe@uap-core.de>
parents: 1004
diff changeset
378 buffer->size--;
67fa386ce600 add cxBufferTerminate()
Mike Becker <universe@uap-core.de>
parents: 1004
diff changeset
379 return 0;
67fa386ce600 add cxBufferTerminate()
Mike Becker <universe@uap-core.de>
parents: 1004
diff changeset
380 } else {
1024
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
381 return -1;
1005
67fa386ce600 add cxBufferTerminate()
Mike Becker <universe@uap-core.de>
parents: 1004
diff changeset
382 }
67fa386ce600 add cxBufferTerminate()
Mike Becker <universe@uap-core.de>
parents: 1004
diff changeset
383 }
67fa386ce600 add cxBufferTerminate()
Mike Becker <universe@uap-core.de>
parents: 1004
diff changeset
384
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
385 size_t cxBufferPutString(
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
386 CxBuffer *buffer,
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
387 const char *str
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
388 ) {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
389 return cxBufferWrite(str, 1, strlen(str), buffer);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
390 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
391
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
392 size_t cxBufferRead(
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
393 void *ptr,
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
394 size_t size,
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
395 size_t nitems,
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
396 CxBuffer *buffer
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
397 ) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
398 size_t len;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
399 if (cx_szmul(size, nitems, &len)) {
1040
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 1030
diff changeset
400 errno = EOVERFLOW;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
401 return 0;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
402 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
403 if (buffer->pos + len > buffer->size) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
404 len = buffer->size - buffer->pos;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
405 if (size > 1) len -= len % size;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
406 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
407
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
408 if (len <= 0) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
409 return len;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
410 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
411
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
412 memcpy(ptr, buffer->bytes + buffer->pos, len);
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
413 buffer->pos += len;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
414
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
415 return len / size;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
416 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
417
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
418 int cxBufferGet(CxBuffer *buffer) {
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
419 if (cxBufferEof(buffer)) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
420 return EOF;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
421 } else {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
422 int c = buffer->bytes[buffer->pos];
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
423 buffer->pos++;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
424 return c;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
425 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
426 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
427
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
428 int cxBufferShiftLeft(
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
429 CxBuffer *buffer,
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
430 size_t shift
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
431 ) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
432 if (shift >= buffer->size) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
433 buffer->pos = buffer->size = 0;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
434 } else {
1028
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
435 if (buffer_copy_on_write(buffer)) return -1;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
436 memmove(buffer->bytes, buffer->bytes + shift, buffer->size - shift);
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
437 buffer->size -= shift;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
438
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
439 if (buffer->pos >= shift) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
440 buffer->pos -= shift;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
441 } else {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
442 buffer->pos = 0;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
443 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
444 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
445 return 0;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
446 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
447
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
448 int cxBufferShiftRight(
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
449 CxBuffer *buffer,
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
450 size_t shift
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
451 ) {
1040
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 1030
diff changeset
452 if (buffer->size > SIZE_MAX - shift) {
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 1030
diff changeset
453 errno = EOVERFLOW;
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 1030
diff changeset
454 return -1;
1ecf4dbbc60c add some more overflow treatment and make sure to set errno properly
Mike Becker <universe@uap-core.de>
parents: 1030
diff changeset
455 }
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
456 size_t req_capacity = buffer->size + shift;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
457 size_t movebytes;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
458
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
459 // auto extend buffer, if required and enabled
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
460 if (buffer->capacity < req_capacity) {
1024
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
461 if (buffer->flags & CX_BUFFER_AUTO_EXTEND) {
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
462 if (cxBufferMinimumCapacity(buffer, req_capacity)) {
1065
6eb7b54975ee improve coverage metrics
Mike Becker <universe@uap-core.de>
parents: 1040
diff changeset
463 return -1; // LCOV_EXCL_LINE
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
464 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
465 movebytes = buffer->size;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
466 } else {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
467 movebytes = buffer->capacity - shift;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
468 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
469 } else {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
470 movebytes = buffer->size;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
471 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
472
1024
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
473 if (movebytes > 0) {
1028
3e4905241838 add copy-on-extend feature to UCX buffer - fixes #533
Mike Becker <universe@uap-core.de>
parents: 1024
diff changeset
474 if (buffer_copy_on_write(buffer)) return -1;
1024
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
475 memmove(buffer->bytes + shift, buffer->bytes, movebytes);
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
476 buffer->size = shift + movebytes;
8f99f6c28bd3 add copy-on-write feature to UCX buffer - fixes #531
Mike Becker <universe@uap-core.de>
parents: 1007
diff changeset
477 }
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
478
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
479 buffer->pos += shift;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
480 if (buffer->pos > buffer->size) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
481 buffer->pos = buffer->size;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
482 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
484 return 0;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
485 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
486
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
487 int cxBufferShift(
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
488 CxBuffer *buffer,
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
489 off_t shift
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
490 ) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
491 if (shift < 0) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
492 return cxBufferShiftLeft(buffer, (size_t) (-shift));
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
493 } else if (shift > 0) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
494 return cxBufferShiftRight(buffer, (size_t) shift);
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
495 } else {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
496 return 0;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
497 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
498 }

mercurial