99 * University of Stuttgart. All rights reserved.
1010 * Copyright (c) 2004-2005 The Regents of the University of California.
1111 * All rights reserved.
12+ * Copyright (c) 2017 Intel, Inc. All rights reserved.
1213 * $COPYRIGHT$
1314 *
1415 * Additional copyrights may follow
@@ -35,6 +36,7 @@ char* opal_dss_buffer_extend(opal_buffer_t *buffer, size_t bytes_to_add)
3536{
3637 size_t required , to_alloc ;
3738 size_t pack_offset , unpack_offset ;
39+ char * tmp ;
3840
3941 /* Check to see if we have enough space already */
4042
@@ -43,34 +45,19 @@ char* opal_dss_buffer_extend(opal_buffer_t *buffer, size_t bytes_to_add)
4345 }
4446
4547 required = buffer -> bytes_used + bytes_to_add ;
46- if (required >= (size_t )opal_dss_threshold_size ) {
47- to_alloc = ((required + opal_dss_threshold_size - 1 )
48- / opal_dss_threshold_size ) * opal_dss_threshold_size ;
48+ if (required >= (size_t )opal_dss_threshold_size ) {
49+ to_alloc = (required + opal_dss_threshold_size - 1 ) & ~(opal_dss_threshold_size - 1 );
4950 } else {
50- to_alloc = buffer -> bytes_allocated ;
51- if (0 == to_alloc ) {
52- to_alloc = opal_dss_initial_size ;
53- }
54- while (to_alloc < required ) {
55- to_alloc <<= 1 ;
56- }
51+ to_alloc = buffer -> bytes_allocated ? buffer -> bytes_allocated : (size_t )opal_dss_initial_size ;
5752 }
5853
59- if (NULL != buffer -> base_ptr ) {
60- pack_offset = ((char * ) buffer -> pack_ptr ) - ((char * ) buffer -> base_ptr );
61- unpack_offset = ((char * ) buffer -> unpack_ptr ) -
62- ((char * ) buffer -> base_ptr );
63- buffer -> base_ptr = (char * )realloc (buffer -> base_ptr , to_alloc );
64- } else {
65- pack_offset = 0 ;
66- unpack_offset = 0 ;
67- buffer -> bytes_used = 0 ;
68- buffer -> base_ptr = (char * )malloc (to_alloc );
69- }
70-
71- if (NULL == buffer -> base_ptr ) {
54+ pack_offset = ((char * ) buffer -> pack_ptr ) - ((char * ) buffer -> base_ptr );
55+ unpack_offset = ((char * ) buffer -> unpack_ptr ) - ((char * ) buffer -> base_ptr );
56+ tmp = (char * )realloc (buffer -> base_ptr , to_alloc );
57+ if (NULL == tmp ) {
7258 return NULL ;
7359 }
60+ buffer -> base_ptr = tmp ;
7461 buffer -> pack_ptr = ((char * ) buffer -> base_ptr ) + pack_offset ;
7562 buffer -> unpack_ptr = ((char * ) buffer -> base_ptr ) + unpack_offset ;
7663 buffer -> bytes_allocated = to_alloc ;
0 commit comments