This source file includes following definitions.
- mpi_alloc
- mpi_alloc_limb_space
- mpi_free_limb_space
- mpi_assign_limb_space
- mpi_resize
- mpi_free
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #include "mpi-internal.h"
22
23
24
25
26
27
28
29
30 MPI mpi_alloc(unsigned nlimbs)
31 {
32 MPI a;
33
34 a = kmalloc(sizeof *a, GFP_KERNEL);
35 if (!a)
36 return a;
37
38 if (nlimbs) {
39 a->d = mpi_alloc_limb_space(nlimbs);
40 if (!a->d) {
41 kfree(a);
42 return NULL;
43 }
44 } else {
45 a->d = NULL;
46 }
47
48 a->alloced = nlimbs;
49 a->nlimbs = 0;
50 a->sign = 0;
51 a->flags = 0;
52 a->nbits = 0;
53 return a;
54 }
55 EXPORT_SYMBOL_GPL(mpi_alloc);
56
57 mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs)
58 {
59 size_t len = nlimbs * sizeof(mpi_limb_t);
60
61 if (!len)
62 return NULL;
63
64 return kmalloc(len, GFP_KERNEL);
65 }
66
67 void mpi_free_limb_space(mpi_ptr_t a)
68 {
69 if (!a)
70 return;
71
72 kzfree(a);
73 }
74
75 void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs)
76 {
77 mpi_free_limb_space(a->d);
78 a->d = ap;
79 a->alloced = nlimbs;
80 }
81
82
83
84
85
86 int mpi_resize(MPI a, unsigned nlimbs)
87 {
88 void *p;
89
90 if (nlimbs <= a->alloced)
91 return 0;
92
93 if (a->d) {
94 p = kmalloc_array(nlimbs, sizeof(mpi_limb_t), GFP_KERNEL);
95 if (!p)
96 return -ENOMEM;
97 memcpy(p, a->d, a->alloced * sizeof(mpi_limb_t));
98 kzfree(a->d);
99 a->d = p;
100 } else {
101 a->d = kcalloc(nlimbs, sizeof(mpi_limb_t), GFP_KERNEL);
102 if (!a->d)
103 return -ENOMEM;
104 }
105 a->alloced = nlimbs;
106 return 0;
107 }
108
109 void mpi_free(MPI a)
110 {
111 if (!a)
112 return;
113
114 if (a->flags & 4)
115 kzfree(a->d);
116 else
117 mpi_free_limb_space(a->d);
118
119 if (a->flags & ~7)
120 pr_info("invalid flag value in mpi\n");
121 kfree(a);
122 }
123 EXPORT_SYMBOL_GPL(mpi_free);
124
125 MODULE_DESCRIPTION("Multiprecision maths library");
126 MODULE_LICENSE("GPL");