@@ -36,7 +36,6 @@ char* opal_dss_buffer_extend(opal_buffer_t *buffer, size_t bytes_to_add)
36
36
{
37
37
size_t required , to_alloc ;
38
38
size_t pack_offset , unpack_offset ;
39
- char * tmp ;
40
39
41
40
/* Check to see if we have enough space already */
42
41
@@ -45,19 +44,34 @@ char* opal_dss_buffer_extend(opal_buffer_t *buffer, size_t bytes_to_add)
45
44
}
46
45
47
46
required = buffer -> bytes_used + bytes_to_add ;
48
- if (required >= (size_t )opal_dss_threshold_size ) {
49
- to_alloc = (required + opal_dss_threshold_size - 1 ) & ~(opal_dss_threshold_size - 1 );
47
+ if (required >= (size_t )opal_dss_threshold_size ) {
48
+ to_alloc = ((required + opal_dss_threshold_size - 1 )
49
+ / opal_dss_threshold_size ) * opal_dss_threshold_size ;
50
50
} else {
51
- to_alloc = buffer -> bytes_allocated ? buffer -> bytes_allocated : (size_t )opal_dss_initial_size ;
51
+ to_alloc = buffer -> bytes_allocated ;
52
+ if (0 == to_alloc ) {
53
+ to_alloc = opal_dss_initial_size ;
54
+ }
55
+ while (to_alloc < required ) {
56
+ to_alloc <<= 1 ;
57
+ }
52
58
}
53
59
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 ) {
60
+ if (NULL != buffer -> base_ptr ) {
61
+ pack_offset = ((char * ) buffer -> pack_ptr ) - ((char * ) buffer -> base_ptr );
62
+ unpack_offset = ((char * ) buffer -> unpack_ptr ) -
63
+ ((char * ) buffer -> base_ptr );
64
+ buffer -> base_ptr = (char * )realloc (buffer -> base_ptr , to_alloc );
65
+ } else {
66
+ pack_offset = 0 ;
67
+ unpack_offset = 0 ;
68
+ buffer -> bytes_used = 0 ;
69
+ buffer -> base_ptr = (char * )malloc (to_alloc );
70
+ }
71
+
72
+ if (NULL == buffer -> base_ptr ) {
58
73
return NULL ;
59
74
}
60
- buffer -> base_ptr = tmp ;
61
75
buffer -> pack_ptr = ((char * ) buffer -> base_ptr ) + pack_offset ;
62
76
buffer -> unpack_ptr = ((char * ) buffer -> base_ptr ) + unpack_offset ;
63
77
buffer -> bytes_allocated = to_alloc ;
0 commit comments