-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmem_func.c
More file actions
80 lines (70 loc) · 1.43 KB
/
mem_func.c
File metadata and controls
80 lines (70 loc) · 1.43 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
71
72
73
74
75
76
77
78
79
#include "s_shell.h"
/**
* freePointer - frees a pointer
* @ptr: address of the pointer to be free
*
* Return: 1 on freed, otherwise 0.
*/
int freePointer(void **ptr)
{
if (ptr && *ptr)
{
free(*ptr);
*ptr = NULL;
return (1);
}
return (0);
}
/**
* memSet - fills memory with a constant byte
* @memory: the pointer to the memory area
* @byte: the byte to fill *memory with
* @size: the amount of bytes to be filled
* Return: pointer to the memory area memory
*/
char *memSet(char *memory, char byte, unsigned int size)
{
unsigned int i;
for (i = 0; i < size; i++)
memory[i] = byte;
return (memory);
}
/**
* freeStringArray - frees array of strings
* @stringArray: string of strings
*/
void freeStringArray(char **stringArray)
{
char **temp = stringArray;
if (!stringArray)
return;
while (*stringArray)
free(*stringArray++);
free(temp);
}
/**
* _realloc - reallocates a block of memory
* @prePtr: pointer to previous malloc'ated block
* @os: byte size of the previous block
* @ns: byte size of the new block
*
* Return: pointer to da ol'block nameen.
*/
void *_realloc(void *prePtr, unsigned int os, unsigned int ns)
{
char *newPtr;
if (!prePtr)
return (malloc(ns));
if (!ns)
return (free(prePtr), NULL);
if (ns == os)
return (prePtr);
newPtr = malloc(ns);
if (!newPtr)
return (NULL);
os = os < ns ? os : ns;
while (os--)
newPtr[os] = ((char *)prePtr)[os];
free(prePtr);
return (newPtr);
}