My new distro "Glacies" - GNU/Linux

Users browsing this thread: 5 Guest(s)
josuah
Long time nixers
(15-01-2020, 04:31 PM)eadwardus Wrote: calling realloc with just the required space is not optimal

I think I express poorly. So I will express myself in term of other people's code. :)

Here is the mechanism I am talking about in DJB code (skalibs):

Code:
int stralloc_ready_tuned (stralloc *sa, size_t n, size_t base, size_t a, size_t b)
{
  [...]
  t = n + base + a * n / b ;
  [...]
}

#define stralloc_ready(sa, n) stralloc_ready_tuned(sa, (n), 8, 1, 8)

int stralloc_catb (stralloc *sa, char const *s, size_t n)
{
  if (!stralloc_readyplus(sa, n)) return 0 ;
  [...]
}
https://github.com/skarnet/skalibs/blob/...uned.c#L12
https://github.com/skarnet/skalibs/blob/...lloc.h#L21
https://github.com/skarnet/skalibs/blob/...tb.c#L6-L8

Here is how it is already done in a few libcs.

Musl:
Code:
static int adjust_size(size_t *n)
{
    /* Result of pointer difference must fit in ptrdiff_t. */
    if (*n-1 > PTRDIFF_MAX - SIZE_ALIGN - PAGE_SIZE) {
        if (*n) {
            errno = ENOMEM;
            return -1;
        } else {
            *n = SIZE_ALIGN;
            return 0;
        }
    }
    *n = (*n + OVERHEAD + SIZE_ALIGN - 1) & SIZE_MASK;
    return 0;
}

void *realloc(void *p, size_t n)
{
    [...]
    if (adjust_size(&n) < 0) return 0;
    [...]
}
http://git.musl-libc.org/cgit/musl/tree/...loc.c#n377

OpenBSD:
Code:
#define PAGEROUND(x)  (((x) + (MALLOC_PAGEMASK)) & ~MALLOC_PAGEMASK)

static void *
orealloc(struct dir_info **argpool, void *p, size_t newsz, void *f)
{
    [...]
        size_t rnewsz = PAGEROUND(gnewsz);
    [...]
}
https://cvsweb.openbsd.org/src/lib/libc/...web-markup

Glibc:
Code:
/* pad request bytes into a usable size -- internal version */

#define request2size(req)                                         \
  (((req) + SIZE_SZ + MALLOC_ALIGN_MASK < MINSIZE)  ?             \
   MINSIZE :                                                      \
   ((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)

void *
__libc_realloc (void *oldmem, size_t bytes)
{
  [...]
    if (!checked_request2size (bytes, &nb))
    {
      __set_errno (ENOMEM);
      return NULL;
    }
  [...]
}
https://sourceware.org/git/?p=glibc.git;...HEAD#l3130


But then, there still might be something I missed about that, which makes stralloc's (->a) size_t still useful. This happen often to me. I read some other code, and then I get what I read a day ago was there...

In any way, there is not much benefit besides a single function removed (in case of skarnet's code), and no performance benefit! Just me choosing the color of the bicycle shedding...


Messages In This Thread
My new distro "Glacies" - by eadwardus - 08-12-2019, 03:55 PM
RE: My new distro "Glacies" - by bouncepaw - 09-12-2019, 01:54 PM
RE: My new distro "Glacies" - by z3bra - 09-12-2019, 08:08 PM
RE: My new distro "Glacies" - by eadwardus - 09-12-2019, 11:59 PM
RE: My new distro "Glacies" - by z3bra - 10-12-2019, 04:25 AM
RE: My new distro "Glacies" - by eadwardus - 10-12-2019, 11:52 AM
RE: My new distro "Glacies" - by jkl - 10-12-2019, 07:40 PM
RE: My new distro "Glacies" - by josuah - 10-12-2019, 08:48 PM
RE: My new distro "Glacies" - by josuah - 10-12-2019, 08:50 PM
RE: My new distro "Glacies" - by josuah - 10-12-2019, 08:52 PM
RE: My new distro "Glacies" - by eadwardus - 11-12-2019, 03:28 AM
RE: My new distro "Glacies" - by eadwardus - 31-12-2019, 11:52 PM
RE: My new distro "Glacies" - by josuah - 13-01-2020, 08:42 PM
RE: My new distro "Glacies" - by josuah - 13-01-2020, 09:12 PM
RE: My new distro "Glacies" - by eadwardus - 15-01-2020, 04:31 PM
RE: My new distro "Glacies" - by josuah - 18-01-2020, 12:12 PM
RE: My new distro "Glacies" - by josuah - 18-01-2020, 12:16 PM
RE: My new distro "Glacies" - by eadwardus - 19-01-2020, 02:20 PM
RE: My new distro "Glacies" - by josuah - 25-01-2020, 02:01 PM