src/buffer.c

Wed, 08 Feb 2023 20:26:26 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 08 Feb 2023 20:26:26 +0100
changeset 654
c9d008861178
parent 650
77021e06b1a8
child 673
60fb6aec157d
permissions
-rw-r--r--

Automated merge

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 #include "cx/utils.h"
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31
530
e866516cac17 #170 first buffer tests
Mike Becker <universe@uap-core.de>
parents: 529
diff changeset
32 #include <stdio.h>
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 #include <string.h>
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34
501
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
35 int cxBufferInit(
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
36 CxBuffer *buffer,
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 void *space,
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 size_t capacity,
529
814d51173f20 #171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents: 501
diff changeset
39 CxAllocator const *allocator,
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 int flags
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 ) {
501
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
42 buffer->allocator = allocator;
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
43 buffer->flags = flags;
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
44 if (!space) {
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
45 buffer->bytes = cxMalloc(allocator, capacity);
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
46 if (buffer->bytes == NULL) {
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
47 return 1;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 }
501
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
49 buffer->flags |= CX_BUFFER_FREE_CONTENTS;
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
50 } else {
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
51 buffer->bytes = space;
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
52 }
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
53 buffer->capacity = capacity;
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
54 buffer->size = 0;
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
55 buffer->pos = 0;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
57 buffer->flush_func = NULL;
541
67e078518935 #170 complete write tests for status quo
Mike Becker <universe@uap-core.de>
parents: 540
diff changeset
58 buffer->flush_target = NULL;
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
59 buffer->flush_blkmax = 0;
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
60 buffer->flush_blksize = 4096;
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
61 buffer->flush_threshold = SIZE_MAX;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62
501
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
63 return 0;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
66 void cxBufferDestroy(CxBuffer *buffer) {
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 if ((buffer->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS) {
501
9a08f5e515cc add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
68 cxFree(buffer->allocator, buffer->bytes);
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 int cxBufferSeek(
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
73 CxBuffer *buffer,
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 off_t offset,
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 int whence
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 ) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 size_t npos;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 switch (whence) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 case SEEK_CUR:
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 npos = buffer->pos;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 break;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 case SEEK_END:
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 npos = buffer->size;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 break;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 case SEEK_SET:
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 npos = 0;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 break;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 default:
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 return -1;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 size_t opos = npos;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 npos += offset;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 return -1;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 if (npos >= buffer->size) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 return -1;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 } else {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 buffer->pos = npos;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 return 0;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107
529
814d51173f20 #171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents: 501
diff changeset
108 void cxBufferClear(CxBuffer *buffer) {
814d51173f20 #171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents: 501
diff changeset
109 memset(buffer->bytes, 0, buffer->size);
814d51173f20 #171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents: 501
diff changeset
110 buffer->size = 0;
814d51173f20 #171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents: 501
diff changeset
111 buffer->pos = 0;
814d51173f20 #171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents: 501
diff changeset
112 }
814d51173f20 #171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents: 501
diff changeset
113
814d51173f20 #171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents: 501
diff changeset
114 int cxBufferEof(CxBuffer const *buffer) {
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 return buffer->pos >= buffer->size;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 int cxBufferMinimumCapacity(
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
119 CxBuffer *buffer,
532
080c304ec176 fix cxBufferMinimumCapacity implementation still using additional_bytes
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
120 size_t newcap
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 ) {
532
080c304ec176 fix cxBufferMinimumCapacity implementation still using additional_bytes
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
122 if (newcap <= buffer->capacity) {
080c304ec176 fix cxBufferMinimumCapacity implementation still using additional_bytes
Mike Becker <universe@uap-core.de>
parents: 530
diff changeset
123 return 0;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125
536
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 534
diff changeset
126 if (cxReallocate(buffer->allocator,
cb9b9739055e #170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents: 534
diff changeset
127 (void **) &buffer->bytes, newcap) == 0) {
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 buffer->capacity = newcap;
533
8d70ad8da899 do not implicitly zero freshly allocated memory
Mike Becker <universe@uap-core.de>
parents: 532
diff changeset
129 return 0;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 } else {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 return -1;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134
544
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
135 /**
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
136 * Helps flushing data to the flush target of a buffer.
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
137 *
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
138 * @param buffer the buffer containing the config
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
139 * @param space the data to flush
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
140 * @param size the element size
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
141 * @param nitems the number of items
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
142 * @return the number of items flushed
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
143 */
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
144 static size_t cx_buffer_write_flush_helper(
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
145 CxBuffer *buffer,
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
146 unsigned char const *space,
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
147 size_t size,
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
148 size_t nitems
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
149 ) {
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
150 size_t pos = 0;
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
151 size_t remaining = nitems;
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
152 size_t max_items = buffer->flush_blksize / size;
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
153 while (remaining > 0) {
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
154 size_t items = remaining > max_items ? max_items : remaining;
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
155 size_t flushed = buffer->flush_func(
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
156 space + pos,
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
157 size, items,
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
158 buffer->flush_target);
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
159 if (flushed > 0) {
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
160 pos += (flushed * size);
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
161 remaining -= flushed;
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
162 } else {
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
163 // 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
164 break;
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
165 }
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
166 }
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
167 return nitems - remaining;
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
168 }
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
169
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 size_t cxBufferWrite(
489
af6be1e123aa add some const qualifiers
Mike Becker <universe@uap-core.de>
parents: 483
diff changeset
171 void const *ptr,
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 size_t size,
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 size_t nitems,
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
174 CxBuffer *buffer
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 ) {
543
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
176 // optimize for easy case
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
177 if (size == 1 && (buffer->capacity - buffer->pos) >= nitems) {
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
178 memcpy(buffer->bytes + buffer->pos, ptr, nitems);
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
179 buffer->pos += nitems;
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
180 if (buffer->pos > buffer->size) {
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
181 buffer->size = buffer->pos;
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
182 }
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
183 return nitems;
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
184 }
7b9114030ca4 optimization for buffer write easy cases
Mike Becker <universe@uap-core.de>
parents: 542
diff changeset
185
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186 size_t len;
544
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
187 size_t nitems_out = nitems;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 if (cx_szmul(size, nitems, &len)) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 return 0;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 size_t required = buffer->pos + len;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 if (buffer->pos > required) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 return 0;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
196 bool perform_flush = false;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 if (required > buffer->capacity) {
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
198 if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND && required) {
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
199 if (buffer->flush_blkmax > 0 && required > buffer->flush_threshold) {
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
200 perform_flush = true;
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
201 } else {
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
202 if (cxBufferMinimumCapacity(buffer, required)) {
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
203 return 0;
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
204 }
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 } else {
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
207 if (buffer->flush_blkmax > 0) {
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
208 perform_flush = true;
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
209 } else {
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
210 // truncate data to be written, if we can neither extend nor flush
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
211 len = buffer->capacity - buffer->pos;
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
212 if (size > 1) {
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
213 len -= len % size;
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
214 }
544
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
215 nitems_out = len / size;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
220 if (len == 0) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 return len;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
224 if (perform_flush) {
544
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
225 size_t flush_max;
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
226 if (cx_szmul(buffer->flush_blkmax, buffer->flush_blksize, &flush_max)) {
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
227 return 0;
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
228 }
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
229 size_t flush_pos = buffer->flush_func == NULL || buffer->flush_target == NULL
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
230 ? buffer->pos
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
231 : cx_buffer_write_flush_helper(buffer, buffer->bytes, 1, buffer->pos);
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
232 if (flush_pos == buffer->pos) {
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
233 // entire buffer has been flushed, we can reset
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
234 buffer->size = buffer->pos = 0;
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
235
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
236 size_t items_flush; // how many items can also be directly flushed
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
237 size_t items_keep; // how many items have to be written to the buffer
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
238
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
239 items_flush = flush_max >= required ? nitems : (flush_max - flush_pos) / size;
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
240 if (items_flush > 0) {
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
241 items_flush = cx_buffer_write_flush_helper(buffer, ptr, size, items_flush / size);
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
242 // in case we could not flush everything, keep the rest
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
243 }
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
244 items_keep = nitems - items_flush;
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
245 if (items_keep > 0) {
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
246 // try again with the remaining stuff
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
247 unsigned char const *new_ptr = ptr;
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
248 new_ptr += items_flush * size;
567
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 544
diff changeset
249 // report the directly flushed items as written plus the remaining stuff
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 544
diff changeset
250 return items_flush + cxBufferWrite(new_ptr, size, items_keep, buffer);
544
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
251 } else {
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
252 // all items have been flushed - report them as written
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
253 return nitems;
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
254 }
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
255 } else if (flush_pos == 0) {
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
256 // nothing could be flushed at all, we immediately give up without writing any data
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
257 return 0;
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
258 } else {
567
f90a7cfe2480 #184 #170 more thorough flush testing
Mike Becker <universe@uap-core.de>
parents: 544
diff changeset
259 // we were partially successful, we shift left and try again
544
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
260 cxBufferShiftLeft(buffer, flush_pos);
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
261 return cxBufferWrite(ptr, size, nitems, buffer);
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
262 }
539
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
263 } else {
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
264 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
265 buffer->pos += len;
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
266 if (buffer->pos > buffer->size) {
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
267 buffer->size = buffer->pos;
9cd98da9ee17 #184 start implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 538
diff changeset
268 }
544
2e73456e5f84 #184 untested implementation of the flush feature
Mike Becker <universe@uap-core.de>
parents: 543
diff changeset
269 return nitems_out;
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
270 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
271
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
272 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
273
538
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
274 int cxBufferPut(
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
275 CxBuffer *buffer,
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
276 int c
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
277 ) {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
278 c &= 0xFF;
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
279 unsigned char const ch = c;
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
280 if (cxBufferWrite(&ch, 1, 1, buffer) == 1) {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
281 return c;
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
282 } else {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
283 return EOF;
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
284 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
285 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
286
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
287 size_t cxBufferPutString(
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
288 CxBuffer *buffer,
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
289 const char *str
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
290 ) {
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
291 return cxBufferWrite(str, 1, strlen(str), buffer);
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
292 }
2cfbcbe86a7c #170 first basic write tests
Mike Becker <universe@uap-core.de>
parents: 536
diff changeset
293
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
294 size_t cxBufferRead(
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
295 void *ptr,
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
296 size_t size,
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
297 size_t nitems,
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
298 CxBuffer *buffer
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
299 ) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
300 size_t len;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
301 if (cx_szmul(size, nitems, &len)) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
302 return 0;
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 if (buffer->pos + len > buffer->size) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
305 len = buffer->size - buffer->pos;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
306 if (size > 1) len -= len % size;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
307 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
308
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
309 if (len <= 0) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
310 return len;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
311 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
312
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
313 memcpy(ptr, buffer->bytes + buffer->pos, len);
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
314 buffer->pos += len;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
315
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
316 return len / size;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
317 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
318
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
319 int cxBufferGet(CxBuffer *buffer) {
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
320 if (cxBufferEof(buffer)) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
321 return EOF;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
322 } else {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
323 int c = buffer->bytes[buffer->pos];
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
324 buffer->pos++;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
325 return c;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
326 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
327 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
328
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
329 int cxBufferShiftLeft(
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
330 CxBuffer *buffer,
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
331 size_t shift
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
332 ) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
333 if (shift >= buffer->size) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
334 buffer->pos = buffer->size = 0;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
335 } else {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
336 memmove(buffer->bytes, buffer->bytes + shift, buffer->size - shift);
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
337 buffer->size -= shift;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
338
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
339 if (buffer->pos >= shift) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
340 buffer->pos -= shift;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
341 } else {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
342 buffer->pos = 0;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
343 }
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 return 0;
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
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
348 int cxBufferShiftRight(
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
349 CxBuffer *buffer,
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
350 size_t shift
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
351 ) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
352 size_t req_capacity = buffer->size + shift;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
353 size_t movebytes;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
354
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
355 // auto extend buffer, if required and enabled
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
356 if (buffer->capacity < req_capacity) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
357 if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
358 if (cxBufferMinimumCapacity(buffer, req_capacity)) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
359 return 1;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
360 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
361 movebytes = buffer->size;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
362 } else {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
363 movebytes = buffer->capacity - shift;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
364 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
365 } else {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
366 movebytes = buffer->size;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
367 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
368
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
369 memmove(buffer->bytes + shift, buffer->bytes, movebytes);
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
370 buffer->size = shift + movebytes;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
371
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
372 buffer->pos += shift;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
373 if (buffer->pos > buffer->size) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
374 buffer->pos = buffer->size;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
375 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
376
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
377 return 0;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
378 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
379
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
380 int cxBufferShift(
500
eb9e7bd40a8e do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
381 CxBuffer *buffer,
483
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
382 off_t shift
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
383 ) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
384 if (shift < 0) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
385 return cxBufferShiftLeft(buffer, (size_t) (-shift));
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
386 } else if (shift > 0) {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
387 return cxBufferShiftRight(buffer, (size_t) shift);
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
388 } else {
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
389 return 0;
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
390 }
929016224c3c add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff changeset
391 }

mercurial