@@ -14,15 +14,16 @@ static secp256k1_scratch* secp256k1_scratch_create(const secp256k1_callback* err
1414 secp256k1_scratch * ret = (secp256k1_scratch * )checked_malloc (error_callback , sizeof (* ret ));
1515 if (ret != NULL ) {
1616 memset (ret , 0 , sizeof (* ret ));
17+ ret -> data = (secp256k1_scratch * )checked_malloc (error_callback , max_size );
1718 ret -> max_size = max_size ;
18- ret -> error_callback = error_callback ;
1919 }
2020 return ret ;
2121}
2222
2323static void secp256k1_scratch_destroy (secp256k1_scratch * scratch ) {
2424 if (scratch != NULL ) {
2525 VERIFY_CHECK (scratch -> frame == 0 );
26+ free (scratch -> data );
2627 free (scratch );
2728 }
2829}
@@ -44,10 +45,8 @@ static int secp256k1_scratch_allocate_frame(secp256k1_scratch* scratch, size_t n
4445
4546 if (n <= secp256k1_scratch_max_allocation (scratch , objects )) {
4647 n += objects * ALIGNMENT ;
47- scratch -> data [scratch -> frame ] = checked_malloc (scratch -> error_callback , n );
48- if (scratch -> data [scratch -> frame ] == NULL ) {
49- return 0 ;
50- }
48+ scratch -> current_frame = scratch -> data ;
49+ scratch -> data = (void * ) ((char * ) scratch -> data + n );
5150 scratch -> frame_size [scratch -> frame ] = n ;
5251 scratch -> offset [scratch -> frame ] = 0 ;
5352 scratch -> frame ++ ;
@@ -59,8 +58,8 @@ static int secp256k1_scratch_allocate_frame(secp256k1_scratch* scratch, size_t n
5958
6059static void secp256k1_scratch_deallocate_frame (secp256k1_scratch * scratch ) {
6160 VERIFY_CHECK (scratch -> frame > 0 );
62- scratch -> frame -= 1 ;
63- free ( scratch -> data [scratch -> frame ]);
61+ scratch -> frame -- ;
62+ scratch -> data = ( void * ) (( char * ) scratch -> data - scratch -> frame_size [scratch -> frame ]);
6463}
6564
6665static void * secp256k1_scratch_alloc (secp256k1_scratch * scratch , size_t size ) {
@@ -71,7 +70,7 @@ static void *secp256k1_scratch_alloc(secp256k1_scratch* scratch, size_t size) {
7170 if (scratch -> frame == 0 || size + scratch -> offset [frame ] > scratch -> frame_size [frame ]) {
7271 return NULL ;
7372 }
74- ret = (void * ) ((unsigned char * ) scratch -> data [ frame ] + scratch -> offset [frame ]);
73+ ret = (void * ) ((char * ) scratch -> current_frame + scratch -> offset [frame ]);
7574 memset (ret , 0 , size );
7675 scratch -> offset [frame ] += size ;
7776
0 commit comments