22 lines
472 B
C
22 lines
472 B
C
#include "libft.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
void *ft_realloc(void *ptr, size_t old_size, size_t new_size)
|
|
{
|
|
if (new_size == 0) {
|
|
free(ptr);
|
|
return (NULL);
|
|
}
|
|
if (!ptr)
|
|
return (malloc(new_size));
|
|
|
|
void *new_ptr = malloc(new_size);
|
|
if (!new_ptr)
|
|
return (NULL);
|
|
|
|
size_t copy_size = old_size < new_size ? old_size : new_size;
|
|
ft_memcpy(new_ptr, ptr, copy_size);
|
|
free(ptr);
|
|
return (new_ptr);
|
|
}
|