summaryrefslogtreecommitdiff
path: root/mpi/mpiutil.c
blob: 752ce7f84124920c84cc4f18ac497ca53cc62ff7 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* mpiutil.c  -  Utility functions for MPI
 *	Copyright (c) 1997 by Werner Koch (dd9jn)
 *
 * This file is part of G10.
 *
 * G10 is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * G10 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "mpi.h"
#include "mpi-internal.h"
#include "memory.h"
#include "util.h"


#ifdef M_DEBUG
  #undef mpi_alloc
  #undef mpi_alloc_secure
  #undef mpi_free
#endif

typedef struct unused_obj {
    struct unused_obj *next;
    unsigned length;
    union {
	MPI mpi;
	mpi_limb_t *limb;
    } u;
} *unused_obj_t;

static unused_obj_t unused_objs;
static unused_obj_t unused_mpis;
static unused_obj_t unused_limbs;


MPI
#ifdef M_DEBUG
mpi_debug_alloc( unsigned nlimbs, const char *info )
#else
mpi_alloc( unsigned nlimbs )
#endif
{
    MPI a;

    if( unused_mpis ) {
	unused_obj_t u;

	if( DBG_MEMORY )
	    log_debug("mpi_alloc(%lu) reusing\n", nlimbs*BITS_PER_MPI_LIMB );
	a = unused_mpis->u.mpi;
	u = unused_mpis;
	unused_mpis = unused_mpis->next;
	u->next = unused_objs;
	unused_objs = u;
    }
    else {
	if( DBG_MEMORY )
	    log_debug("mpi_alloc(%lu) new\n", nlimbs*BITS_PER_MPI_LIMB );
      #ifdef M_DEBUG
	a = m_debug_alloc( sizeof *a, info );
      #else
	a = m_alloc( sizeof *a );
      #endif
    }
  #ifdef M_DEBUG
    a->d = mpi_debug_alloc_limb_space( nlimbs, info );
  #else
    a->d = mpi_alloc_limb_space( nlimbs );
  #endif
    a->alloced = nlimbs;
    a->nlimbs = 0;
    a->sign = 0;
    return a;
}

void
mpi_m_check( MPI a )
{
    m_check(a);
    m_check(a->d);
}

MPI
#ifdef M_DEBUG
mpi_debug_alloc_secure( unsigned nlimbs, const char *info )
#else
mpi_alloc_secure( unsigned nlimbs )
#endif
{
    MPI a;

    a = m_alloc( sizeof *a );
  #ifdef M_DEBUG
    a->d = m_debug_alloc_secure( nlimbs * sizeof(mpi_limb_t), info );
  #else
    a->d = m_alloc_secure( nlimbs * sizeof(mpi_limb_t) );
  #endif
    a->alloced = nlimbs;
    a->nlimbs = 0;
    a->sign = 0;
    return a;
}


mpi_ptr_t
#ifdef M_DEBUG
mpi_debug_alloc_limb_space( unsigned nlimbs, const char *info )
#else
mpi_alloc_limb_space( unsigned nlimbs )
#endif
{
    unused_obj_t u;
    size_t len = nlimbs * sizeof(mpi_limb_t);

    for(u=unused_limbs; u; u = u->next )
	if( u->length >= len ) {
	    u->length = 0;
	    if( DBG_MEMORY )
		log_debug("mpi_alloc_limb_space(%lu) reusing\n", len*8 );
	    return u->u.limb;
	}
    if( DBG_MEMORY )
	log_debug("mpi_alloc_limb_space(%u) new\n", len*8 );
  #ifdef M_DEBUG
    return m_debug_alloc( len, info );
  #else
    return m_alloc( len );
  #endif
}

void
#ifdef M_DEBUG
mpi_debug_free_limb_space( mpi_ptr_t a, const char *info )
#else
mpi_free_limb_space( mpi_ptr_t a )
#endif
{
    unused_obj_t u;

    if( !a )
	return;
    if( DBG_MEMORY )
	log_debug("mpi_free_limb_space of size %lu\n", (ulong)m_size(a)*8 );
    for(u=unused_limbs; u; u = u->next )
	if( !u->length ) {
	    u->length = m_size(a);
	    u->u.limb = a;
	    return;
	}

    if( (u=unused_objs) )
	unused_objs = unused_objs->next;
    else
	u = m_alloc( sizeof *u );
    u->length = m_size(a);
    u->u.limb = a;
    u->next = unused_limbs;
    unused_limbs = u;
}


void
mpi_assign_limb_space( MPI a, mpi_ptr_t ap, unsigned nlimbs )
{
    mpi_free_limb_space(a->d);
    a->d = ap;
    a->alloced = nlimbs;
}



/****************
 * Resize the array of A to NLIMBS. the additional space is cleared
 * (set to 0) [done by m_realloc()]
 */
void
#ifdef M_DEBUG
mpi_debug_resize( MPI a, unsigned nlimbs, const char *info )
#else
mpi_resize( MPI a, unsigned nlimbs )
#endif
{
    if( nlimbs <= a->alloced )
	return; /* no need to do it */
  #ifdef M_DEBUG
    if( a->d )
	a->d = m_debug_realloc(a->d, nlimbs * sizeof(mpi_limb_t), info );
    else
	a->d = m_debug_alloc_clear( nlimbs * sizeof(mpi_limb_t), info );
  #else
    if( a->d )
	a->d = m_realloc(a->d, nlimbs * sizeof(mpi_limb_t) );
    else
	a->d = m_alloc_clear( nlimbs * sizeof(mpi_limb_t) );
  #endif
    a->alloced = nlimbs;
}

void
mpi_clear( MPI a )
{
    a->nlimbs = 0;
}


void
#ifdef M_DEBUG
mpi_debug_free( MPI a, const char *info )
#else
mpi_free( MPI a )
#endif
{
    unused_obj_t u;

    if( !a )
	return;
    if( DBG_MEMORY )
	log_debug("mpi_free\n" );
  #ifdef M_DEBUG
    mpi_debug_free_limb_space(a->d, info);
  #else
    mpi_free_limb_space(a->d);
  #endif

    if( (u=unused_objs) )
	unused_objs = unused_objs->next;
    else
	u = m_alloc( sizeof *u );
    u->u.mpi = a;
    u->next = unused_mpis;
    unused_mpis = u;
}


MPI
#ifdef M_DEBUG
mpi_debug_copy( MPI a, const char *info )
#else
mpi_copy( MPI a )
#endif
{
    int i;
    MPI b;

    if( a ) {
      #ifdef M_DEBUG
	b = mpi_debug_alloc( a->nlimbs, info );
      #else
	b = mpi_alloc( a->nlimbs );
      #endif
	b->nlimbs = a->nlimbs;
	for(i=0; i < b->nlimbs; i++ )
	    b->d[i] = a->d[i];
    }
    else
	b = NULL;
    return b;
}


void
mpi_set( MPI w, MPI u)
{
    mpi_ptr_t wp, up;
    mpi_size_t usize = u->nlimbs;
    int usign = u->sign;

    RESIZE_IF_NEEDED(w, usize);
    wp = w->d;
    up = u->d;
    MPN_COPY( wp, up, usize );
    w->nlimbs = usize;
    w->sign = usign;
}


void
mpi_set_ui( MPI w, unsigned long u)
{
    RESIZE_IF_NEEDED(w, 1);
    w->d[0] = u;
    w->nlimbs = u? 1:0;
    w->sign = 0;
}


MPI
mpi_alloc_set_ui( unsigned long u)
{
  #ifdef M_DEBUG
    MPI w = mpi_debug_alloc(1,"alloc_set_ui");
  #else
    MPI w = mpi_alloc(1);
  #endif
    w->d[0] = u;
    w->nlimbs = u? 1:0;
    w->sign = 0;
    return w;
}


void
mpi_swap( MPI a, MPI b)
{
    MPI x;

    x = a; a = b; b = x;
}