-
Notifications
You must be signed in to change notification settings - Fork 0
/
com_mem.c
59 lines (50 loc) · 1002 Bytes
/
com_mem.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
#include "main.h"
/**
* _memcpy - copies n bytes from src to dest
* @dest: destination memory area
* @src: source memory area
* @n: number of bytes to copy
* Return: a pointer to dest
*/
char *_memcpy(char *dest, char *src, unsigned int n)
{
unsigned int i = 0;
while (i < n)
{
*(dest + i) = *(src + i);
i++;
}
return (dest);
}
/**
* _realloc - memory reallocation using malloc
* @ptr: pointer to the previous block
* @old_size: old size in bytes
* @new_size: new size in bytes
* Return: pointer to the new memory (or old one if no change)
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
char *nptr;
unsigned int n;
if (new_size == old_size)
return (ptr);
if (new_size == 0 && ptr != NULL)
{
free(ptr);
return (NULL);
}
nptr = malloc(new_size);
if (nptr == NULL)
{
free(ptr);
return (NULL);
}
if (ptr != NULL)
{
n = (new_size < old_size) ? new_size : old_size;
_memcpy(nptr, ptr, n);
}
free(ptr);
return (nptr);
}