Sat, 30 Apr 2022 08:40:40 +0200
fix doc error for cxBufferWrite
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 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | #include <stdlib.h> |
530 | 33 | #include <stdio.h> |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
34 | #include <string.h> |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
35 | |
501
9a08f5e515cc
add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
36 | int cxBufferInit( |
9a08f5e515cc
add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
37 | CxBuffer *buffer, |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
38 | void *space, |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
39 | size_t capacity, |
529
814d51173f20
#171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents:
501
diff
changeset
|
40 | CxAllocator const *allocator, |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
41 | int flags |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | ) { |
501
9a08f5e515cc
add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
43 | buffer->allocator = allocator; |
9a08f5e515cc
add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
44 | buffer->flags = flags; |
9a08f5e515cc
add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
45 | if (!space) { |
9a08f5e515cc
add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
46 | buffer->bytes = cxMalloc(allocator, capacity); |
9a08f5e515cc
add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
47 | if (buffer->bytes == NULL) { |
9a08f5e515cc
add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
48 | return 1; |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | } |
501
9a08f5e515cc
add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
50 | buffer->flags |= CX_BUFFER_FREE_CONTENTS; |
9a08f5e515cc
add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
51 | } else { |
9a08f5e515cc
add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
52 | buffer->bytes = space; |
9a08f5e515cc
add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
53 | } |
9a08f5e515cc
add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
54 | buffer->capacity = capacity; |
9a08f5e515cc
add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
55 | buffer->size = 0; |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | |
501
9a08f5e515cc
add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
57 | buffer->pos = 0; |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | |
501
9a08f5e515cc
add allocator support to CxBuffer
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
59 | return 0; |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
61 | |
500
eb9e7bd40a8e
do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents:
489
diff
changeset
|
62 | void cxBufferDestroy(CxBuffer *buffer) { |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
63 | 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
|
64 | cxFree(buffer->allocator, buffer->bytes); |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
67 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
68 | int cxBufferSeek( |
500
eb9e7bd40a8e
do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents:
489
diff
changeset
|
69 | CxBuffer *buffer, |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | off_t offset, |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
71 | int whence |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
72 | ) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
73 | size_t npos; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
74 | switch (whence) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | case SEEK_CUR: |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
76 | npos = buffer->pos; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | break; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | case SEEK_END: |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | npos = buffer->size; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | break; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 | case SEEK_SET: |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | npos = 0; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | break; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
84 | default: |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
85 | return -1; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | } |
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 | size_t opos = npos; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
89 | npos += offset; |
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 | if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
92 | return -1; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
93 | } |
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 (npos >= buffer->size) { |
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 | } else { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
98 | buffer->pos = npos; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
99 | return 0; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
100 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
101 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
102 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
103 | |
529
814d51173f20
#171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents:
501
diff
changeset
|
104 | void cxBufferClear(CxBuffer *buffer) { |
814d51173f20
#171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents:
501
diff
changeset
|
105 | memset(buffer->bytes, 0, buffer->size); |
814d51173f20
#171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents:
501
diff
changeset
|
106 | buffer->size = 0; |
814d51173f20
#171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents:
501
diff
changeset
|
107 | buffer->pos = 0; |
814d51173f20
#171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents:
501
diff
changeset
|
108 | } |
814d51173f20
#171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents:
501
diff
changeset
|
109 | |
814d51173f20
#171 const qualifier and nonnull attributes
Mike Becker <universe@uap-core.de>
parents:
501
diff
changeset
|
110 | int cxBufferEof(CxBuffer const *buffer) { |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
111 | return buffer->pos >= buffer->size; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
112 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
113 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
114 | int cxBufferMinimumCapacity( |
500
eb9e7bd40a8e
do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents:
489
diff
changeset
|
115 | CxBuffer *buffer, |
532
080c304ec176
fix cxBufferMinimumCapacity implementation still using additional_bytes
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
116 | size_t newcap |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
117 | ) { |
532
080c304ec176
fix cxBufferMinimumCapacity implementation still using additional_bytes
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
118 | if (newcap <= buffer->capacity) { |
080c304ec176
fix cxBufferMinimumCapacity implementation still using additional_bytes
Mike Becker <universe@uap-core.de>
parents:
530
diff
changeset
|
119 | return 0; |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
120 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
121 | |
536
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
534
diff
changeset
|
122 | if (cxReallocate(buffer->allocator, |
cb9b9739055e
#170 add cxBufferClear test
Mike Becker <universe@uap-core.de>
parents:
534
diff
changeset
|
123 | (void **) &buffer->bytes, newcap) == 0) { |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
124 | buffer->capacity = newcap; |
533
8d70ad8da899
do not implicitly zero freshly allocated memory
Mike Becker <universe@uap-core.de>
parents:
532
diff
changeset
|
125 | return 0; |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
126 | } else { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
127 | return -1; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
128 | } |
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 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
131 | size_t cxBufferWrite( |
489
af6be1e123aa
add some const qualifiers
Mike Becker <universe@uap-core.de>
parents:
483
diff
changeset
|
132 | void const *ptr, |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
133 | size_t size, |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
134 | size_t nitems, |
500
eb9e7bd40a8e
do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents:
489
diff
changeset
|
135 | CxBuffer *buffer |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
136 | ) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
137 | size_t len; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
138 | if (cx_szmul(size, nitems, &len)) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
139 | return 0; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
140 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
141 | size_t required = buffer->pos + len; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
142 | if (buffer->pos > required) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
143 | return 0; |
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 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
146 | if (required > buffer->capacity) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
147 | 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
|
148 | if (cxBufferMinimumCapacity(buffer, required)) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
149 | return 0; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
150 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
151 | } else { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
152 | len = buffer->capacity - buffer->pos; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
153 | if (size > 1) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
154 | len -= len % size; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
155 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
156 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
157 | } |
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 | if (len == 0) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
160 | return len; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
161 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
162 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
163 | memcpy(buffer->bytes + buffer->pos, ptr, len); |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
164 | buffer->pos += len; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
165 | if (buffer->pos > buffer->size) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
166 | buffer->size = buffer->pos; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
167 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
168 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
169 | return len / size; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
170 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
171 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
172 | size_t cxBufferRead( |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
173 | void *ptr, |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
174 | size_t size, |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
175 | size_t nitems, |
500
eb9e7bd40a8e
do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents:
489
diff
changeset
|
176 | CxBuffer *buffer |
483
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 | size_t len; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
179 | if (cx_szmul(size, nitems, &len)) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
180 | return 0; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
181 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
182 | if (buffer->pos + len > buffer->size) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
183 | len = buffer->size - buffer->pos; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
184 | if (size > 1) len -= len % size; |
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 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
187 | if (len <= 0) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
188 | return len; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
189 | } |
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 | memcpy(ptr, buffer->bytes + buffer->pos, len); |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
192 | buffer->pos += len; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
193 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
194 | return len / size; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
195 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
196 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
197 | int cxBufferPut( |
500
eb9e7bd40a8e
do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents:
489
diff
changeset
|
198 | CxBuffer *buffer, |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
199 | int c |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
200 | ) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
201 | if (buffer->pos >= buffer->capacity) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
202 | 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
|
203 | if (cxBufferMinimumCapacity(buffer, buffer->capacity + 1)) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
204 | return EOF; |
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 { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
207 | return EOF; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
208 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
209 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
210 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
211 | c &= 0xFF; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
212 | buffer->bytes[buffer->pos] = (unsigned char) c; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
213 | buffer->pos++; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
214 | if (buffer->pos > buffer->size) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
215 | buffer->size = buffer->pos; |
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 | return c; |
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 | |
500
eb9e7bd40a8e
do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents:
489
diff
changeset
|
220 | int cxBufferGet(CxBuffer *buffer) { |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
221 | if (cxBufferEof(buffer)) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
222 | return EOF; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
223 | } else { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
224 | int c = buffer->bytes[buffer->pos]; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
225 | buffer->pos++; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
226 | return c; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
227 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
228 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
229 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
230 | size_t cxBufferPutString( |
500
eb9e7bd40a8e
do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents:
489
diff
changeset
|
231 | CxBuffer *buffer, |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
232 | const char *str |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
233 | ) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
234 | return cxBufferWrite(str, 1, strlen(str), buffer); |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
235 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
236 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
237 | int cxBufferShiftLeft( |
500
eb9e7bd40a8e
do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents:
489
diff
changeset
|
238 | CxBuffer *buffer, |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
239 | size_t shift |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
240 | ) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
241 | if (shift >= buffer->size) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
242 | buffer->pos = buffer->size = 0; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
243 | } else { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
244 | memmove(buffer->bytes, buffer->bytes + shift, buffer->size - shift); |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
245 | buffer->size -= shift; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
246 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
247 | if (buffer->pos >= shift) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
248 | buffer->pos -= shift; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
249 | } else { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
250 | buffer->pos = 0; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
251 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
252 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
253 | return 0; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
254 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
255 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
256 | int cxBufferShiftRight( |
500
eb9e7bd40a8e
do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents:
489
diff
changeset
|
257 | CxBuffer *buffer, |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
258 | size_t shift |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
259 | ) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
260 | size_t req_capacity = buffer->size + shift; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
261 | size_t movebytes; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
262 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
263 | // auto extend buffer, if required and enabled |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
264 | if (buffer->capacity < req_capacity) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
265 | 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
|
266 | if (cxBufferMinimumCapacity(buffer, req_capacity)) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
267 | return 1; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
268 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
269 | movebytes = buffer->size; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
270 | } else { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
271 | movebytes = buffer->capacity - shift; |
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 | } else { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
274 | movebytes = buffer->size; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
275 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
276 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
277 | memmove(buffer->bytes + shift, buffer->bytes, movebytes); |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
278 | buffer->size = shift + movebytes; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
279 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
280 | buffer->pos += shift; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
281 | if (buffer->pos > buffer->size) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
282 | buffer->pos = buffer->size; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
283 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
284 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
285 | return 0; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
286 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
287 | |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
288 | int cxBufferShift( |
500
eb9e7bd40a8e
do not hide pointers behind typedefs
Mike Becker <universe@uap-core.de>
parents:
489
diff
changeset
|
289 | CxBuffer *buffer, |
483
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
290 | off_t shift |
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 | if (shift < 0) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
293 | return cxBufferShiftLeft(buffer, (size_t) (-shift)); |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
294 | } else if (shift > 0) { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
295 | return cxBufferShiftRight(buffer, (size_t) shift); |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
296 | } else { |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
297 | return 0; |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
298 | } |
929016224c3c
add ported UCX buffer implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
299 | } |