Ignore:
File:
1 edited

Legend:

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

    rffccdff0 rdd50aa19  
    3434 */
    3535
    36 #include <malloc.h>
     36#include <stdlib.h>
    3737#include <stdalign.h>
    3838#include <stdbool.h>
    3939#include <stddef.h>
     40#include <stdio.h>
    4041#include <as.h>
    4142#include <align.h>
     
    4748#include <stdlib.h>
    4849#include <adt/gcdlcm.h>
     50#include <malloc.h>
    4951
    5052#include "private/malloc.h"
     
    724726 *
    725727 */
    726 static void *malloc_internal(const size_t size, const size_t align)
     728static void *malloc_internal(size_t size, size_t align)
    727729{
    728730        malloc_assert(first_heap_area != NULL);
    729731
     732        if (size == 0)
     733                size = 1;
     734
    730735        if (align == 0)
    731                 return NULL;
     736                align = BASE_ALIGN;
    732737
    733738        size_t falign = lcm(align, BASE_ALIGN);
     
    773778}
    774779
    775 /** Allocate memory by number of elements
    776  *
    777  * @param nmemb Number of members to allocate.
    778  * @param size  Size of one member in bytes.
    779  *
    780  * @return Allocated memory or NULL.
    781  *
    782  */
    783 void *calloc(const size_t nmemb, const size_t size)
    784 {
    785         // FIXME: Check for overflow
    786 
    787         void *block = malloc(nmemb * size);
    788         if (block == NULL)
    789                 return NULL;
    790 
    791         memset(block, 0, nmemb * size);
    792         return block;
    793 }
    794 
    795780/** Allocate memory
    796781 *
     
    840825 *
    841826 */
    842 void *realloc(void *const addr, const size_t size)
     827void *realloc(void *const addr, size_t size)
    843828{
    844829        if (size == 0) {
    845                 free(addr);
    846                 return NULL;
     830                fprintf(stderr, "realloc() called with size 0\n");
     831                size = 1;
    847832        }
    848833
     
    949934}
    950935
     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
    951955/** Free a memory block
    952956 *
Note: See TracChangeset for help on using the changeset viewer.