-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_mangment.c
108 lines (101 loc) · 1.74 KB
/
memory_mangment.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "shell.h"
/**
**_realloc - Reallocates A Memory Block Using Malloc And Free
*@ptr: Pointer
*@old_size: Previous Size Of The Pointer
*@new_size: New Size Of The Pointer
*Return: Void Pointer Rellocated Memory
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
void *result;
if (new_size == old_size)
return (ptr);
if (new_size == 0 && ptr)
{
free(ptr);
return (NULL);
}
result = malloc(new_size);
if (result == NULL)
return (NULL);
if (ptr == NULL)
{
fill_an_array(result, '\0', new_size);
free(ptr);
}
else
{
_memcpy(result, ptr, old_size);
free(ptr);
}
return (result);
}
/**
* free_all - Free Array Of Char Pointer And Char Pointer
* @cmd:Array Pointer
* @line:Char Pointer
* Return: Void
*/
void free_all(char **cmd, char *line)
{
free(cmd);
free(line);
cmd = NULL;
line = NULL;
}
/**
* _memcpy - Copy Byte From Source To Destination
* @dest: Destination Pointer
* @src: Source Pointer
* @n: Size (How Much You Will Copy)
*Return: Void Pointer
*/
char *_memcpy(char *dest, char *src, unsigned int n)
{
unsigned int i;
for (i = 0; i < n; i++)
{
dest[i] = src[i];
}
return (dest);
}
/**
* fill_an_array - Fill An Array By Constant Byte
* @a:Void Pointer
* @el: Int
* @len:Length Int
*Return: Void Pointer
*/
void *fill_an_array(void *a, int el, unsigned int len)
{
char *p = a;
unsigned int i = 0;
while (i < len)
{
*p = el;
p++;
i++;
}
return (a);
}
/**
* _calloc - Allocates Memory For An Array, Using Malloc.
* @size: Size
* Return: Void Pointer
*/
void *_calloc(unsigned int size)
{
char *a;
unsigned int i;
if (size == 0)
return (NULL);
a = malloc(size);
if (a == NULL)
return (NULL);
for (i = 0; i < size; i++)
{
a[i] = '\0';
}
return (a);
}