Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
realloc.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <string.h>
3 
4 void* realloc(void* ptr, size_t size)
5 {
6  void* new_data = NULL;
7 
8  if(size)
9  {
10  if(!ptr)
11  {
12  return malloc(size);
13  }
14 
15  new_data = malloc(size);
16  if(new_data)
17  {
18  memcpy(new_data, ptr, size); // TODO: unsafe copy...
19  free(ptr); // we always move the data. free.
20  }
21  }
22 
23  return new_data;
24 }
25 
26 void* reallocf(void* ptr, size_t size)
27 {
28  void* p = realloc(ptr, size);
29 
30  if((p == NULL) && (ptr != NULL))
31  {
32  free(ptr);
33  }
34 
35  return p;
36 }
void free(void *ptr)
Deallocates allocated memory space.
#define NULL
Definition: stddef.h:15
void * realloc(void *ptr, size_t size)
Reallocates the given area of memory.
Definition: realloc.c:4
void * reallocf(void *ptr, size_t size)
Reallocates the given area of memory.
Definition: realloc.c:26
void * malloc(size_t size)
Allocates size bytes of uninitialized storage.
void * memcpy(void *__restrict dest, const void *__restrict src, size_t n)
Copies n characters from the object pointed to by src to the object pointed to by dest.