@@ -318,90 +318,6 @@ BCinfo {
318
318
// struct Bigint is defined in pycore_dtoa.h.
319
319
typedef struct Bigint Bigint ;
320
320
321
- #ifndef Py_USING_MEMORY_DEBUGGER
322
-
323
- /* Memory management: memory is allocated from, and returned to, Kmax+1 pools
324
- of memory, where pool k (0 <= k <= Kmax) is for Bigints b with b->maxwds ==
325
- 1 << k. These pools are maintained as linked lists, with freelist[k]
326
- pointing to the head of the list for pool k.
327
-
328
- On allocation, if there's no free slot in the appropriate pool, MALLOC is
329
- called to get more memory. This memory is not returned to the system until
330
- Python quits. There's also a private memory pool that's allocated from
331
- in preference to using MALLOC.
332
-
333
- For Bigints with more than (1 << Kmax) digits (which implies at least 1233
334
- decimal digits), memory is directly allocated using MALLOC, and freed using
335
- FREE.
336
-
337
- XXX: it would be easy to bypass this memory-management system and
338
- translate each call to Balloc into a call to PyMem_Malloc, and each
339
- Bfree to PyMem_Free. Investigate whether this has any significant
340
- performance on impact. */
341
-
342
- #define freelist _PyRuntime.dtoa.freelist
343
- #define private_mem _PyRuntime.dtoa.preallocated
344
- #define pmem_next _PyRuntime.dtoa.preallocated_next
345
-
346
- /* Allocate space for a Bigint with up to 1<<k digits */
347
-
348
- static Bigint *
349
- Balloc (int k )
350
- {
351
- int x ;
352
- Bigint * rv ;
353
- unsigned int len ;
354
-
355
- if (k <= Bigint_Kmax && (rv = freelist [k ]))
356
- freelist [k ] = rv -> next ;
357
- else {
358
- x = 1 << k ;
359
- len = (sizeof (Bigint ) + (x - 1 )* sizeof (ULong ) + sizeof (double ) - 1 )
360
- /sizeof (double );
361
- if (k <= Bigint_Kmax &&
362
- pmem_next - private_mem + len <= (Py_ssize_t )Bigint_PREALLOC_SIZE
363
- ) {
364
- rv = (Bigint * )pmem_next ;
365
- pmem_next += len ;
366
- }
367
- else {
368
- rv = (Bigint * )MALLOC (len * sizeof (double ));
369
- if (rv == NULL )
370
- return NULL ;
371
- }
372
- rv -> k = k ;
373
- rv -> maxwds = x ;
374
- }
375
- rv -> sign = rv -> wds = 0 ;
376
- return rv ;
377
- }
378
-
379
- /* Free a Bigint allocated with Balloc */
380
-
381
- static void
382
- Bfree (Bigint * v )
383
- {
384
- if (v ) {
385
- if (v -> k > Bigint_Kmax )
386
- FREE ((void * )v );
387
- else {
388
- v -> next = freelist [v -> k ];
389
- freelist [v -> k ] = v ;
390
- }
391
- }
392
- }
393
-
394
- #undef pmem_next
395
- #undef private_mem
396
- #undef freelist
397
-
398
- #else
399
-
400
- /* Alternative versions of Balloc and Bfree that use PyMem_Malloc and
401
- PyMem_Free directly in place of the custom memory allocation scheme above.
402
- These are provided for the benefit of memory debugging tools like
403
- Valgrind. */
404
-
405
321
/* Allocate space for a Bigint with up to 1<<k digits */
406
322
407
323
static Bigint *
@@ -412,13 +328,10 @@ Balloc(int k)
412
328
unsigned int len ;
413
329
414
330
x = 1 << k ;
415
- len = (sizeof (Bigint ) + (x - 1 )* sizeof (ULong ) + sizeof (double ) - 1 )
416
- /sizeof (double );
417
-
331
+ len = (sizeof (Bigint ) + (x - 1 )* sizeof (ULong ) + sizeof (double ) - 1 )/sizeof (double );
418
332
rv = (Bigint * )MALLOC (len * sizeof (double ));
419
333
if (rv == NULL )
420
334
return NULL ;
421
-
422
335
rv -> k = k ;
423
336
rv -> maxwds = x ;
424
337
rv -> sign = rv -> wds = 0 ;
@@ -435,8 +348,6 @@ Bfree(Bigint *v)
435
348
}
436
349
}
437
350
438
- #endif /* Py_USING_MEMORY_DEBUGGER */
439
-
440
351
#define Bcopy (x ,y ) memcpy((char *)&x->sign, (char *)&y->sign, \
441
352
y->wds*sizeof(Long) + 2*sizeof(int))
442
353
@@ -692,13 +603,14 @@ pow5mult(Bigint *b, int k)
692
603
693
604
if (!(k >>= 2 ))
694
605
return b ;
606
+ _PyMutex_lock (& _PyRuntime .dtoa .mutex );
695
607
p5 = _PyRuntime .dtoa .p5s ;
696
608
if (!p5 ) {
697
609
/* first time */
698
610
p5 = i2b (625 );
699
611
if (p5 == NULL ) {
700
612
Bfree (b );
701
- return NULL ;
613
+ goto err ;
702
614
}
703
615
_PyRuntime .dtoa .p5s = p5 ;
704
616
p5 -> next = 0 ;
@@ -709,7 +621,7 @@ pow5mult(Bigint *b, int k)
709
621
Bfree (b );
710
622
b = b1 ;
711
623
if (b == NULL )
712
- return NULL ;
624
+ goto err ;
713
625
}
714
626
if (!(k >>= 1 ))
715
627
break ;
@@ -718,14 +630,20 @@ pow5mult(Bigint *b, int k)
718
630
p51 = mult (p5 ,p5 );
719
631
if (p51 == NULL ) {
720
632
Bfree (b );
721
- return NULL ;
633
+ goto err ;
722
634
}
723
635
p51 -> next = 0 ;
724
636
p5 -> next = p51 ;
725
637
}
726
638
p5 = p51 ;
727
639
}
640
+
641
+ _PyMutex_unlock (& _PyRuntime .dtoa .mutex );
728
642
return b ;
643
+
644
+ err :
645
+ _PyMutex_unlock (& _PyRuntime .dtoa .mutex );
646
+ return NULL ;
729
647
}
730
648
731
649
#else
0 commit comments