skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

stralloc_ready_tuned.c (522B)


      1 /* ISC license. */
      2 
      3 #include <string.h>
      4 #include <errno.h>
      5 #include <skalibs/alloc.h>
      6 #include <skalibs/stralloc.h>
      7 
      8 int stralloc_ready_tuned (stralloc *sa, size_t n, size_t base, size_t a, size_t b)
      9 {
     10   size_t t ;
     11   if (!b) return (errno = EINVAL, 0) ;
     12   t = n + base + a * n / b ;
     13   if (t < n) return (errno = ERANGE, 0) ;
     14   if (!sa->s)
     15   {
     16     sa->s = alloc(t) ;
     17     if (!sa->s) return 0 ;
     18     sa->a = t ;
     19   }
     20   else if (n > sa->a)
     21   {
     22     if (!alloc_re(&sa->s, sa->a, t)) return 0 ;
     23     sa->a = t ;
     24   }
     25   return 1 ;
     26 }