-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealloc.c
66 lines (55 loc) · 1.11 KB
/
realloc.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
62
63
64
65
66
#include "alxse.h"
/**
* _memset - func that fills memory with a constant byte
* @s: ptr to the memory area
* @b: byte to fill *s with
* @n: amount of bytes to be filled
*
* Return: (s) a ptr to the memory area s
*/
char *_memset(char *s, char b, unsigned int n)
{
unsigned int x;
for (x = 0; x < n; x++)
s[x] = b;
return (s);
}
/**
* ffree - func that frees a strn of strns
* @pp: strn of strns
*/
void ffree(char **pp)
{
char **w = pp;
if (!pp)
return;
while (*pp)
free(*pp++);
free(w);
}
/**
* _realloc - func that reallocates a block of memory
* @ptr: ptr to previous malloc'ated block
* @old_size: the byte size of previous block
* @new_size: the byte size of new block
*
* Return: ptr to da ol'block nameen.
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
char *m;
if (!ptr)
return (malloc(new_size));
if (!new_size)
return (free(ptr), NULL);
if (new_size == old_size)
return (ptr);
m = malloc(new_size);
if (!m)
return (NULL);
old_size = old_size < new_size ? old_size : new_size;
while (old_size--)
m[old_size] = ((char *)ptr)[old_size];
free(ptr);
return (m);
}