-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemory.c
61 lines (48 loc) · 1020 Bytes
/
memory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "main.h"
/**
* _realloc - reallocates a memory block using malloc and free
* @buffer: pointer to the memory previously allocated
* @size: size in bytes of the allocated space for ptr
* Return: pointer to the newly allocated memory
*/
void *_realloc(void *buffer, size_t size)
{
size_t old_size = 64, cp_size;
void *new_buffer;
/* If the buffer is null, allocate memory of size new_size */
if (buffer == NULL)
{
return (malloc(size));
}
if (size == 0)
{
free(buffer);
return (NULL);
}
/* New buffer size */
new_buffer = malloc(size);
if (new_buffer == NULL)
{
return (NULL);
}
/* Copy the data from the old buffer to the new buffer */
cp_size = (old_size < size) ? old_size : size;
memcpy(new_buffer, buffer, cp_size);
free(buffer);
return (new_buffer);
}
/**
* free_buffers - free buffers
* @n: number of buffers
*/
void free_buffers(int n, ...)
{
int i = 0;
va_list args;
va_start(args, n);
for (i = 0; i < n; i++)
{
free(va_arg(args,void *));
}
va_end(args);
}