Changeset ebb1489 in mainline for uspace/lib/c/generic/malloc.c


Ignore:
Timestamp:
2024-10-13T08:23:40Z (8 weeks ago)
Author:
GitHub <noreply@…>
Children:
0472cf17
Parents:
2a0c827c (diff), b3b79981 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
boba-buba <120932204+boba-buba@…> (2024-10-13 08:23:40)
git-committer:
GitHub <noreply@…> (2024-10-13 08:23:40)
Message:

Merge branch 'HelenOS:master' into topic/packet-capture

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/malloc.c

    r2a0c827c rebb1489  
    3838#include <stdbool.h>
    3939#include <stddef.h>
     40#include <stdio.h>
    4041#include <as.h>
    4142#include <align.h>
     
    725726 *
    726727 */
    727 static void *malloc_internal(const size_t size, const size_t align)
     728static void *malloc_internal(size_t size, size_t align)
    728729{
    729730        malloc_assert(first_heap_area != NULL);
    730731
     732        if (size == 0)
     733                size = 1;
     734
    731735        if (align == 0)
    732                 return NULL;
     736                align = BASE_ALIGN;
    733737
    734738        size_t falign = lcm(align, BASE_ALIGN);
     
    821825 *
    822826 */
    823 void *realloc(void *const addr, const size_t size)
     827void *realloc(void *const addr, size_t size)
    824828{
    825829        if (size == 0) {
    826                 free(addr);
    827                 return NULL;
     830                fprintf(stderr, "realloc() called with size 0\n");
     831                size = 1;
    828832        }
    829833
     
    930934}
    931935
     936/** Reallocate memory for an array
     937 *
     938 * Same as realloc(ptr, nelem * elsize), except the multiplication is checked
     939 * for numerical overflow. Borrowed from POSIX 2024.
     940 *
     941 * @param ptr
     942 * @param nelem
     943 * @param elsize
     944 *
     945 * @return Reallocated memory or NULL.
     946 */
     947void *reallocarray(void *ptr, size_t nelem, size_t elsize)
     948{
     949        if (nelem > SIZE_MAX / elsize)
     950                return NULL;
     951
     952        return realloc(ptr, nelem * elsize);
     953}
     954
    932955/** Free a memory block
    933956 *
Note: See TracChangeset for help on using the changeset viewer.