#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
     1.1 --- a/src/array_list.c	Wed Feb 01 17:14:17 2023 +0100
     1.2 +++ b/src/array_list.c	Wed Feb 01 18:06:50 2023 +0100
     1.3 @@ -103,7 +103,9 @@
     1.4      return CX_ARRAY_COPY_SUCCESS;
     1.5  }
     1.6  
     1.7 +#ifndef CX_ARRAY_SWAP_SBO_SIZE
     1.8  #define CX_ARRAY_SWAP_SBO_SIZE 512
     1.9 +#endif
    1.10  
    1.11  void cx_array_swap(
    1.12          void *arr,
     2.1 --- a/src/printf.c	Wed Feb 01 17:14:17 2023 +0100
     2.2 +++ b/src/printf.c	Wed Feb 01 18:06:50 2023 +0100
     2.3 @@ -31,9 +31,16 @@
     2.4  #include <stdio.h>
     2.5  #include <string.h>
     2.6  
     2.7 -#define CX_PRINTF_BUFSIZE 256
     2.8 +#ifndef CX_PRINTF_SBO_SIZE
     2.9 +#define CX_PRINTF_SBO_SIZE 512
    2.10 +#endif
    2.11  
    2.12 -int cx_fprintf(void *stream, cx_write_func wfc, char const *fmt, ...) {
    2.13 +int cx_fprintf(
    2.14 +        void *stream,
    2.15 +        cx_write_func wfc,
    2.16 +        char const *fmt,
    2.17 +        ...
    2.18 +) {
    2.19      int ret;
    2.20      va_list ap;
    2.21      va_start(ap, fmt);
    2.22 @@ -43,13 +50,13 @@
    2.23  }
    2.24  
    2.25  int cx_vfprintf(void *stream, cx_write_func wfc, char const *fmt, va_list ap) {
    2.26 -    char buf[CX_PRINTF_BUFSIZE];
    2.27 +    char buf[CX_PRINTF_SBO_SIZE];
    2.28      va_list ap2;
    2.29      va_copy(ap2, ap);
    2.30 -    int ret = vsnprintf(buf, CX_PRINTF_BUFSIZE, fmt, ap);
    2.31 +    int ret = vsnprintf(buf, CX_PRINTF_SBO_SIZE, fmt, ap);
    2.32      if (ret < 0) {
    2.33          return ret;
    2.34 -    } else if (ret < CX_PRINTF_BUFSIZE) {
    2.35 +    } else if (ret < CX_PRINTF_SBO_SIZE) {
    2.36          return (int) wfc(buf, 1, ret, stream);
    2.37      } else {
    2.38          int len = ret + 1;
    2.39 @@ -80,11 +87,11 @@
    2.40      cxmutstr s;
    2.41      s.ptr = NULL;
    2.42      s.length = 0;
    2.43 -    char buf[CX_PRINTF_BUFSIZE];
    2.44 +    char buf[CX_PRINTF_SBO_SIZE];
    2.45      va_list ap2;
    2.46      va_copy(ap2, ap);
    2.47 -    int ret = vsnprintf(buf, CX_PRINTF_BUFSIZE, fmt, ap);
    2.48 -    if (ret > 0 && ret < CX_PRINTF_BUFSIZE) {
    2.49 +    int ret = vsnprintf(buf, CX_PRINTF_SBO_SIZE, fmt, ap);
    2.50 +    if (ret > 0 && ret < CX_PRINTF_SBO_SIZE) {
    2.51          s.ptr = cxMalloc(a, ret + 1);
    2.52          if (s.ptr) {
    2.53              s.length = (size_t) ret;
     3.1 --- a/src/string.c	Wed Feb 01 17:14:17 2023 +0100
     3.2 +++ b/src/string.c	Wed Feb 01 18:06:50 2023 +0100
     3.3 @@ -226,7 +226,9 @@
     3.4      return (cxmutstr) {(char *) result.ptr, result.length};
     3.5  }
     3.6  
     3.7 -#define STRSTR_SBO_BUFLEN 512
     3.8 +#ifndef CX_STRSTR_SBO_SIZE
     3.9 +#define CX_STRSTR_SBO_SIZE 512
    3.10 +#endif
    3.11  
    3.12  cxstring cx_strstr(
    3.13          cxstring haystack,
    3.14 @@ -250,11 +252,11 @@
    3.15       */
    3.16  
    3.17      // local prefix table
    3.18 -    size_t s_prefix_table[STRSTR_SBO_BUFLEN];
    3.19 +    size_t s_prefix_table[CX_STRSTR_SBO_SIZE];
    3.20  
    3.21      // check needle length and use appropriate prefix table
    3.22      // if the pattern exceeds static prefix table, allocate on the heap
    3.23 -    bool useheap = needle.length >= STRSTR_SBO_BUFLEN;
    3.24 +    bool useheap = needle.length >= CX_STRSTR_SBO_SIZE;
    3.25      register size_t *ptable = useheap ? calloc(needle.length + 1,
    3.26                                                 sizeof(size_t)) : s_prefix_table;
    3.27  
    3.28 @@ -539,7 +541,9 @@
    3.29      }
    3.30  }
    3.31  
    3.32 -#define REPLACE_INDEX_BUFFER_MAX 100
    3.33 +#ifndef CX_STRREPLACE_INDEX_BUFFER_SIZE
    3.34 +#define CX_STRREPLACE_INDEX_BUFFER_SIZE 64
    3.35 +#endif
    3.36  
    3.37  struct cx_strreplace_ibuf {
    3.38      size_t *buf;
    3.39 @@ -570,8 +574,8 @@
    3.40      // Compute expected buffer length
    3.41      size_t ibufmax = str.length / pattern.length;
    3.42      size_t ibuflen = replmax < ibufmax ? replmax : ibufmax;
    3.43 -    if (ibuflen > REPLACE_INDEX_BUFFER_MAX) {
    3.44 -        ibuflen = REPLACE_INDEX_BUFFER_MAX;
    3.45 +    if (ibuflen > CX_STRREPLACE_INDEX_BUFFER_SIZE) {
    3.46 +        ibuflen = CX_STRREPLACE_INDEX_BUFFER_SIZE;
    3.47      }
    3.48  
    3.49      // Allocate first index buffer

mercurial