-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_mng_ops.c
85 lines (79 loc) · 2.15 KB
/
memory_mng_ops.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* memory_mng_ops.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dfurneau <dfurneau@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/15 20:46:38 by dfurneau #+# #+# */
/* Updated: 2021/12/10 05:07:23 by dfurneau ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/push_swap.h"
/**
* Free memory allocation of operations array
**/
void free_ops(t_ops *ops)
{
int i;
if (ops->ops)
{
i = -1;
while (++i < 2)
free(ops->ops[i]);
free(ops->ops);
ops->ops = NULL;
}
if (ops->sort)
{
free(ops->sort);
ops->sort = NULL;
}
}
/**
* Malloc the Operations Array
* array[0] holds the stack values in B
* array[1] holds the operations to the top for B
* array[2] holds the operations to the top for A
* either - values for rra and + values for ra
**/
static void alloc_ops(t_ops *o)
{
int i;
o->ops = (int **)malloc(sizeof(int *) * 2);
if (!o->ops)
error(o->a, o->b);
i = -1;
while (++i < 2)
{
o->ops[i] = (int *)malloc(sizeof(int) * (o->a->len + o->b->len));
if (!o->ops[i])
{
while (i >= 0)
free(o->ops[--i]);
free(o->ops);
error(o->a, o->b);
}
}
i = -1;
while (++i <= o->b->len - 1)
{
o->ops[0][i] = 0;
o->ops[1][i] = 0;
}
}
/**
* Initialize & Malloc Operations Array
* array[0] holds the operations to the top for B
* array[2] holds the operations to the top for A
* either - values for rra and + values for ra
* REQ FREE
**/
void init_ops(t_ops *ops, t_stack *a, t_stack *b)
{
ops->a = a;
ops->b = b;
ops->len = (a->len + b->len);
ops->sort = sort_array(a);
alloc_ops(ops);
}