src/buffer.c

changeset 1135
f79415d974d3
parent 1131
644f77f903b1
--- a/src/buffer.c	Sat Jan 18 14:10:51 2025 +0100
+++ b/src/buffer.c	Sun Jan 19 17:17:01 2025 +0100
@@ -319,8 +319,8 @@
             // to flush, it means that we are supposed to relay the data
             items_flush = cx_buffer_flush_helper(buffer, ptr, size, nitems);
             if (items_flush == 0) {
-                // we needed to flush, but could not flush anything
-                // give up and avoid endless trying
+                // we needed to relay data, but could not flush anything
+                // i.e. we have to give up to avoid endless trying
                 return 0;
             }
             size_t ritems = nitems - items_flush;
@@ -334,7 +334,21 @@
         } else {
             items_flush = cx_buffer_flush_impl(buffer, size);
             if (items_flush == 0) {
-                return 0;
+                // flush target is full, let's try to truncate
+                size_t remaining_space;
+                if (buffer->flags & CX_BUFFER_AUTO_EXTEND) {
+                    remaining_space = buffer->flush->threshold > buffer->pos
+                                          ? buffer->flush->threshold - buffer->pos
+                                          : 0;
+                } else {
+                    remaining_space = buffer->capacity > buffer->pos
+                                          ? buffer->capacity - buffer->pos
+                                          : 0;
+                }
+                nitems = remaining_space / size;
+                if (nitems == 0) {
+                    return 0;
+                }
             }
             return cxBufferWrite(ptr, size, nitems, buffer);
         }

mercurial