-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemory.c
101 lines (91 loc) · 2 KB
/
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
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
#include "shell.h"
/**
* _realloc - reallocates a memory block using malloc and free
* @ptr: void pointer
* @old_size: already allocated size
* @new_size: new size to allocate
* Return: pointer to newly allocated memory or null
**/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
unsigned char *np;
unsigned int i;
if (new_size == old_size)
return (ptr);
if (new_size == 0 && ptr != NULL)
{
_free(ptr);
return (NULL);
}
if (ptr == NULL)
{
ptr = _malloc(new_size * sizeof(void *));
if (ptr == NULL)
return (NULL);
return (ptr);
}
np = _malloc(new_size * sizeof(char));
if (np == NULL)
return (NULL);
i = 0;
if (new_size > old_size)
{
while (i < old_size)
{
np[i] = ((char *)ptr)[i];
i++;
}
_free(ptr);
return (np);
}
/* if new_size < old_size */
while (i < new_size)
{
np[i] = ((char *)ptr)[i];
i++;
}
_free(ptr);
return (np);
}
/**
* _ref_mem - Helper function that calls a memory add or remove function
* @p: pointer to save in node, or remove from linked list
* @action: string literal that determines which function to call
* Return: -1 on error, else return value returned from other function call
*/
int _ref_mem(void *p, char *action)
{
static save_mem *head = NULL;
if (_strcmp(action, "create") == 0)
return (add_mem(&p, &head));
else if (_strcmp(action, "remove child") == 0)
return (remove_child_mem(&p, &head));
else if (_strcmp(action, "remove") == 0)
return (remove_mem(&p, &head));
return (-1);
}
/**
* _malloc - Malloc and save a pointer to malloc'd space for memory linked list
* @size: size to malloc
* Return: pointer to malloc or NULL on error
*/
void *_malloc(unsigned int size)
{
void *p;
int status;
p = malloc(size);
if (p == NULL)
return (NULL);
status = _ref_mem(p, "create");
if (status < 0)
return (NULL);
return (p);
}
/**
* _free - Free based on pointer passed
* @ptr: pointer to malloc'd memory to free, or if NULL, free all memory nodes
*/
void _free(void *ptr)
{
_ref_mem(ptr, "remove");
}