Project

General

Profile

Feature #11680

want reallocf(3C)

Added by Andy Fiddaman 11 months ago. Updated 11 months ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
lib - userland libraries
Start date:
Due date:
% Done:

100%

Estimated time:
Difficulty:
Bite-size
Tags:
Gerrit CR:

Description

Another BSD compatibility function which is also in Linux and was recently added to Solaris.

It behaves just like realloc(3C) except that if the allocation fails, it will free the memory referenced by the original pointer. This helps simplify code that uses realloc and helps to avoid memory leaks.

History

#1

Updated by Andy Fiddaman 11 months ago

  • Subject changed from want frealloc(3C) to want reallocf(3C)
#2

Updated by Andy Fiddaman 11 months ago

I tested this using the following small program, once using realloc() and once using reallocf() and checking for memory leaks. This uses realloc[f] to allocate memory, resize memory and free memory. With realloc there was a resulting memory leak from the failed realloc case, with reallocf there was none, see below.

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int
main()
{
        void *p;

        p = reallocf(NULL, 73);
        assert(p != NULL);

        p = reallocf(p, 1024);
        assert(p != NULL);

        // realloc will fail
        p = reallocf(p, SIZE_MAX - 4096);
        assert(p == NULL);

        p = reallocf(NULL, 81);
        assert(p != NULL);

        // realloc will free memory
        p = reallocf(p, 0);
        assert(p == NULL);
}

With realloc()

bloody% UMEM_DEBUG=default mdb ./realloc
> ::load libumem.so.1
> ::bp exit
> ::run
mdb: stop at exit
mdb: target stopped at:
libc_hwcap1.so.1`exit:  pushl  %ebx
> ::findleaks -d
CACHE     LEAKED   BUFCTL CALLER
08192010       1 081bc970 main+0x50
------------------------------------------------------------------------
   Total       1 buffer, 1152 bytes

umem_alloc_1152 leak: 1 buffer, 1152 bytes
            ADDR          BUFADDR        TIMESTAMP           THREAD
                            CACHE          LASTLOG         CONTENTS
         81bc970          81bfb00     31c37e8bc084                1
                          8192010                0                0
                 libumem.so.1`umem_cache_alloc_debug+0x216
                 libumem.so.1`umem_cache_alloc+0x1f5
                 libumem.so.1`umem_alloc+0x5c
                 libumem.so.1`umem_malloc+0x25
                 libumem.so.1`realloc+0x4a
                 main+0x50
                 _start_crt+0x96
                 _start+0x1a

>

With reallocf()

bloody% UMEM_DEBUG=default mdb ./reallocf
> ::load libumem.so.1
> ::bp exit
> ::run
mdb: stop at exit
mdb: target stopped at:
libc_hwcap1.so.1`exit:  pushl  %ebx
> ::findleaks -d
findleaks: no memory leaks detected

I also did a full build of OmniOS userland following this change and checked that, for example, the dma software was now using the new system function, and that it functions.

bloody:omnios.bloody:master% nm /usr/lib/smtp/dma/dma | grep reallocf
[289]   |             4251552|                  63|FUNC |GLOB |0    |14     |reallocf
bloody:omnios.bloody:master% nm build/dma/tmp/src/dma | grep reallocf
[289] |             4218520|                   0|FUNC |GLOB |0    |UNDEF  |reallocf
#3

Updated by Electric Monk 11 months ago

  • Status changed from In Progress to Closed
  • % Done changed from 0 to 100

git commit e7b66456775e45577294dfd79cab910ecb886cf2

commit  e7b66456775e45577294dfd79cab910ecb886cf2
Author: Andy Fiddaman <omnios@citrus-it.co.uk>
Date:   2019-09-16T16:32:55.000Z

    11680 want reallocf(3C)
    Reviewed by: Igor Kozhukhov <igor@dilos.org>
    Reviewed by: Toomas Soome <tsoome@me.com>
    Reviewed by: Matthias Scheler <matthias.scheler@wdc.com>
    Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
    Reviewed by: John Levon <john.levon@joyent.com>
    Approved by: Robert Mustacchi <rm@joyent.com>

Also available in: Atom PDF