-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf_memory.c
More file actions
70 lines (58 loc) · 1.37 KB
/
f_memory.c
File metadata and controls
70 lines (58 loc) · 1.37 KB
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
#include "shell.h"
/**
* allocate_memory - Manages the allocation of memory
* @bytes: Number representing the size in bytes to allocate
*
* Return: Pointer to the newly allocated memory
*/
void *allocate_memory(unsigned int bytes)
{
char *new_mem = malloc(bytes);
if (new_mem == NULL)
dispatch_error("Error while allocating memory\n");
return (new_mem);
}
/**
* duplicate_string - Duplicates a given string
* @str: String to dulicate
*
* Return: Pointer to the address of the newly duplicated string
*/
char *duplicate_string(char *str)
{
char *str_copy = _strdup(str);
if (str_copy == NULL)
dispatch_error("Error while making copy of string");
return (str_copy);
}
/**
* free_dbl_ptr - Frees the memory pointed to by a double pointer
* @dbl_ptr: Double pointer
*/
void free_dbl_ptr(char **dbl_ptr)
{
int i;
if (dbl_ptr == NULL)
return;
for (i = 0; dbl_ptr[i]; i++)
free(dbl_ptr[i]);
free(dbl_ptr);
}
/**
* free_allocs - Frees allocated memory
* @buff: Main buffer
* @cmds_list: List of commands
* @commands: Command as an array of arguments
* @flags: Number indicating which memory to free
*/
void free_allocs(char *buff, char **cmds_list, char **commands, int flags)
{
if (flags & F_BUFF)
free(buff);
if (flags & F_CMD_L)
free_dbl_ptr(cmds_list);
if (flags & F_CMDS)
free_dbl_ptr(commands);
free_list(*(get_alias_head()));
free_history();
}