Ignore:
File:
1 edited

Legend:

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

    rdd50aa19 rffccdff0  
    3434 */
    3535
    36 #include <stdlib.h>
     36#include <malloc.h>
    3737#include <stdalign.h>
    3838#include <stdbool.h>
    3939#include <stddef.h>
    40 #include <stdio.h>
    4140#include <as.h>
    4241#include <align.h>
     
    4847#include <stdlib.h>
    4948#include <adt/gcdlcm.h>
    50 #include <malloc.h>
    5149
    5250#include "private/malloc.h"
     
    726724 *
    727725 */
    728 static void *malloc_internal(size_t size, size_t align)
     726static void *malloc_internal(const size_t size, const size_t align)
    729727{
    730728        malloc_assert(first_heap_area != NULL);
    731729
    732         if (size == 0)
    733                 size = 1;
    734 
    735730        if (align == 0)
    736                 align = BASE_ALIGN;
     731                return NULL;
    737732
    738733        size_t falign = lcm(align, BASE_ALIGN);
     
    778773}
    779774
     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 */
     783void *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
    780795/** Allocate memory
    781796 *
     
    825840 *
    826841 */
    827 void *realloc(void *const addr, size_t size)
     842void *realloc(void *const addr, const size_t size)
    828843{
    829844        if (size == 0) {
    830                 fprintf(stderr, "realloc() called with size 0\n");
    831                 size = 1;
     845                free(addr);
     846                return NULL;
    832847        }
    833848
     
    934949}
    935950
    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  */
    947 void *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 
    955951/** Free a memory block
    956952 *
Note: See TracChangeset for help on using the changeset viewer.