-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMy_malloc.c
47 lines (43 loc) · 936 Bytes
/
My_malloc.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
#include<stdlib.h>
#include<unistd.h>
#include <stdio.h>
#include <sys/_types/_size_t.h>
typedef struct s_data
{
void *data;
struct s_data *next;
}t_data;
void dataclear(t_data **head)
{
t_data *tmp;
t_data *ptr;
tmp = *head;
while(tmp)
{
ptr = tmp;
tmp = tmp->next;
free(ptr->data);
free(ptr);
}
head = NULL;
}
void *my_malloc(size_t size, int mode)
{
static t_data *head;
t_data *node = NULL;
void *data = NULL;
if(mode == 1)
{
data = malloc(size);
if(!data)
return (dataclear(&head), NULL);
node = malloc(sizeof(t_data));
if(!node)
return (dataclear(&head), free(data), NULL);
(1) && (node->data = data, node->next = head, head = node);
return (data);
}
else if(!mode)
return(dataclear(&head), NULL);
return (write(2, "INVALID MODE\n", 13) ,NULL);
}