-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory.c
executable file
·70 lines (63 loc) · 1.91 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
#include "holberton.h"
/**
* _alloc - Allocates memory for the buffer to be printed.
* @buffer: The pointer to the buffer to be printed.
* @size: The size of the buffer to be allocated.
*
* Return: The pointer to the buffer allocated
* or NULL if failed to allocate memory.
*/
void *_alloc(void *buffer, unsigned int size)
{
buffer = NULL;
buffer = malloc(size);
if (buffer == NULL)
return (NULL);
return (buffer);
}
/**
* _realloc - reallocates memory depending on its new size.
* @ptr: The array of memory originally allocated.
* @old_size: The array's allocated old size.
* @new_size: The array's new size to be allocated.
*
* Return: A pointer to the address if successful at allocating memory.
* Otherwise, returns NULL. NULL if new_size == 0.
*/
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size)
{
void *p_realloc;
unsigned int counter;
p_realloc = NULL;
counter = 0;
p_realloc = malloc(new_size + 1);
if (p_realloc == NULL)
return (NULL);
for (counter = 0; counter < old_size && counter < new_size; counter++)
((char *) p_realloc)[counter] = ((char *) ptr)[counter];
((char *) p_realloc)[counter] = '\0';
free(ptr);
return (p_realloc);
}
/**
* check_buffer - Checks the size of the buffer (buffer length).
* @buffer: The pointer to the buffer.
* @buffer_i: The current length of the buffer.
* @buff_len: The total length of the string to be printed.
* @c: The character to be added to the buffer.
*
* Description: Checks the current size of the buffer. If the buffer reaches
* a certain treshold (BUFFER size), then it will prints its
* contents and return to the first character.
* Return: The pointer to the buffer.
*/
void check_buffer(char *buffer, int *buffer_i, unsigned int *buff_len, char c)
{
if (*buffer_i == 1024)
{
write(1, buffer, *buffer_i);
*buffer_i = 0;
}
buffer[(*buffer_i)++] = c;
*buff_len += 1;
}