#228 make buffer sizes adjustable at compile time

Wed, 01 Feb 2023 18:06:50 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 01 Feb 2023 18:06:50 +0100
changeset 643
5700ba9154ab
parent 642
98c90759f69e
child 644
fcaa0891ef28

#228 make buffer sizes adjustable at compile time

src/array_list.c file | annotate | diff | comparison | revisions
src/printf.c file | annotate | diff | comparison | revisions
src/string.c file | annotate | diff | comparison | revisions
--- a/src/array_list.c	Wed Feb 01 17:14:17 2023 +0100
+++ b/src/array_list.c	Wed Feb 01 18:06:50 2023 +0100
@@ -103,7 +103,9 @@
     return CX_ARRAY_COPY_SUCCESS;
 }
 
+#ifndef CX_ARRAY_SWAP_SBO_SIZE
 #define CX_ARRAY_SWAP_SBO_SIZE 512
+#endif
 
 void cx_array_swap(
         void *arr,
--- a/src/printf.c	Wed Feb 01 17:14:17 2023 +0100
+++ b/src/printf.c	Wed Feb 01 18:06:50 2023 +0100
@@ -31,9 +31,16 @@
 #include <stdio.h>
 #include <string.h>
 
-#define CX_PRINTF_BUFSIZE 256
+#ifndef CX_PRINTF_SBO_SIZE
+#define CX_PRINTF_SBO_SIZE 512
+#endif
 
-int cx_fprintf(void *stream, cx_write_func wfc, char const *fmt, ...) {
+int cx_fprintf(
+        void *stream,
+        cx_write_func wfc,
+        char const *fmt,
+        ...
+) {
     int ret;
     va_list ap;
     va_start(ap, fmt);
@@ -43,13 +50,13 @@
 }
 
 int cx_vfprintf(void *stream, cx_write_func wfc, char const *fmt, va_list ap) {
-    char buf[CX_PRINTF_BUFSIZE];
+    char buf[CX_PRINTF_SBO_SIZE];
     va_list ap2;
     va_copy(ap2, ap);
-    int ret = vsnprintf(buf, CX_PRINTF_BUFSIZE, fmt, ap);
+    int ret = vsnprintf(buf, CX_PRINTF_SBO_SIZE, fmt, ap);
     if (ret < 0) {
         return ret;
-    } else if (ret < CX_PRINTF_BUFSIZE) {
+    } else if (ret < CX_PRINTF_SBO_SIZE) {
         return (int) wfc(buf, 1, ret, stream);
     } else {
         int len = ret + 1;
@@ -80,11 +87,11 @@
     cxmutstr s;
     s.ptr = NULL;
     s.length = 0;
-    char buf[CX_PRINTF_BUFSIZE];
+    char buf[CX_PRINTF_SBO_SIZE];
     va_list ap2;
     va_copy(ap2, ap);
-    int ret = vsnprintf(buf, CX_PRINTF_BUFSIZE, fmt, ap);
-    if (ret > 0 && ret < CX_PRINTF_BUFSIZE) {
+    int ret = vsnprintf(buf, CX_PRINTF_SBO_SIZE, fmt, ap);
+    if (ret > 0 && ret < CX_PRINTF_SBO_SIZE) {
         s.ptr = cxMalloc(a, ret + 1);
         if (s.ptr) {
             s.length = (size_t) ret;
--- a/src/string.c	Wed Feb 01 17:14:17 2023 +0100
+++ b/src/string.c	Wed Feb 01 18:06:50 2023 +0100
@@ -226,7 +226,9 @@
     return (cxmutstr) {(char *) result.ptr, result.length};
 }
 
-#define STRSTR_SBO_BUFLEN 512
+#ifndef CX_STRSTR_SBO_SIZE
+#define CX_STRSTR_SBO_SIZE 512
+#endif
 
 cxstring cx_strstr(
         cxstring haystack,
@@ -250,11 +252,11 @@
      */
 
     // local prefix table
-    size_t s_prefix_table[STRSTR_SBO_BUFLEN];
+    size_t s_prefix_table[CX_STRSTR_SBO_SIZE];
 
     // check needle length and use appropriate prefix table
     // if the pattern exceeds static prefix table, allocate on the heap
-    bool useheap = needle.length >= STRSTR_SBO_BUFLEN;
+    bool useheap = needle.length >= CX_STRSTR_SBO_SIZE;
     register size_t *ptable = useheap ? calloc(needle.length + 1,
                                                sizeof(size_t)) : s_prefix_table;
 
@@ -539,7 +541,9 @@
     }
 }
 
-#define REPLACE_INDEX_BUFFER_MAX 100
+#ifndef CX_STRREPLACE_INDEX_BUFFER_SIZE
+#define CX_STRREPLACE_INDEX_BUFFER_SIZE 64
+#endif
 
 struct cx_strreplace_ibuf {
     size_t *buf;
@@ -570,8 +574,8 @@
     // Compute expected buffer length
     size_t ibufmax = str.length / pattern.length;
     size_t ibuflen = replmax < ibufmax ? replmax : ibufmax;
-    if (ibuflen > REPLACE_INDEX_BUFFER_MAX) {
-        ibuflen = REPLACE_INDEX_BUFFER_MAX;
+    if (ibuflen > CX_STRREPLACE_INDEX_BUFFER_SIZE) {
+        ibuflen = CX_STRREPLACE_INDEX_BUFFER_SIZE;
     }
 
     // Allocate first index buffer

mercurial