isl_vec_dump: handle NULL vec
[platform/upstream/isl.git] / isl_vec.c
1 #include "isl_vec.h"
2
3 struct isl_vec *isl_vec_alloc(struct isl_ctx *ctx, unsigned size)
4 {
5         struct isl_vec *vec;
6
7         vec = isl_alloc_type(ctx, struct isl_vec);
8         if (!vec)
9                 return NULL;
10
11         vec->block = isl_blk_alloc(ctx, size);
12         if (isl_blk_is_error(vec->block))
13                 goto error;
14
15         vec->ref = 1;
16         vec->size = size;
17
18         return vec;
19 error:
20         isl_blk_free(ctx, vec->block);
21         return NULL;
22 }
23
24 struct isl_vec *isl_vec_copy(struct isl_ctx *ctx, struct isl_vec *vec)
25 {
26         if (!vec)
27                 return NULL;
28
29         vec->ref++;
30         return vec;
31 }
32
33 struct isl_vec *isl_vec_dup(struct isl_ctx *ctx, struct isl_vec *vec)
34 {
35         struct isl_vec *vec2;
36
37         if (!vec)
38                 return NULL;
39         vec2 = isl_vec_alloc(ctx, vec->size);
40         isl_seq_cpy(vec2->block.data, vec->block.data, vec->size);
41         return vec2;
42 }
43
44 struct isl_vec *isl_vec_cow(struct isl_ctx *ctx, struct isl_vec *vec)
45 {
46         struct isl_vec *vec2;
47         if (!vec)
48                 return NULL;
49
50         if (vec->ref == 1)
51                 return vec;
52
53         vec2 = isl_vec_dup(ctx, vec);
54         isl_vec_free(ctx, vec);
55         return vec2;
56 }
57
58 void isl_vec_free(struct isl_ctx *ctx, struct isl_vec *vec)
59 {
60         if (!vec)
61                 return;
62
63         if (--vec->ref > 0)
64                 return;
65
66         isl_blk_free(ctx, vec->block);
67         free(vec);
68 }
69
70 void isl_vec_dump(struct isl_ctx *ctx, struct isl_vec *vec,
71                                 FILE *out, int indent)
72 {
73         int i;
74
75         if (!vec) {
76                 fprintf(out, "%*snull vec\n", indent, "");
77                 return;
78         }
79
80         fprintf(out, "%*s[", indent, "");
81         for (i = 0; i < vec->size; ++i) {
82                 if (i)
83                     fprintf(out, ",");
84                 isl_int_print(out, vec->block.data[i], 0);
85         }
86         fprintf(out, "]\n");
87 }
88
89 void isl_vec_lcm(struct isl_ctx *ctx, struct isl_vec *vec, isl_int *lcm)
90 {
91         isl_seq_lcm(vec->block.data, vec->size, lcm);
92 }