This source file includes following definitions.
- opal_dss_buffer_extend
- opal_dss_too_small
- opal_dss_store_data_type
- opal_dss_get_data_type
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include "opal_config.h"
21
22 #include <stdio.h>
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
27 #include "opal/class/opal_pointer_array.h"
28
29 #include "opal/dss/dss_internal.h"
30
31
32
33
34
35 char* opal_dss_buffer_extend(opal_buffer_t *buffer, size_t bytes_to_add)
36 {
37 size_t required, to_alloc;
38 size_t pack_offset, unpack_offset;
39
40
41
42 if ((buffer->bytes_allocated - buffer->bytes_used) >= bytes_to_add) {
43 return buffer->pack_ptr;
44 }
45
46 required = buffer->bytes_used + bytes_to_add;
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 } else {
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 }
58 }
59
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) {
73 return NULL;
74 }
75 buffer->pack_ptr = ((char*) buffer->base_ptr) + pack_offset;
76 buffer->unpack_ptr = ((char*) buffer->base_ptr) + unpack_offset;
77 buffer->bytes_allocated = to_alloc;
78
79
80
81 return buffer->pack_ptr;
82 }
83
84
85
86
87
88 bool opal_dss_too_small(opal_buffer_t *buffer, size_t bytes_reqd)
89 {
90 size_t bytes_remaining_packed;
91
92 if (buffer->pack_ptr < buffer->unpack_ptr) {
93 return true;
94 }
95
96 bytes_remaining_packed = buffer->pack_ptr - buffer->unpack_ptr;
97
98 if (bytes_remaining_packed < bytes_reqd) {
99
100
101
102 return true;
103 }
104
105 return false;
106 }
107
108 int opal_dss_store_data_type(opal_buffer_t *buffer, opal_data_type_t type)
109 {
110 opal_dss_type_info_t *info;
111
112
113
114 if (NULL == (info = (opal_dss_type_info_t*)opal_pointer_array_get_item(&opal_dss_types, OPAL_DATA_TYPE_T))) {
115 return OPAL_ERR_PACK_FAILURE;
116 }
117
118 return info->odti_pack_fn(buffer, &type, 1, OPAL_DATA_TYPE_T);
119 }
120
121 int opal_dss_get_data_type(opal_buffer_t *buffer, opal_data_type_t *type)
122 {
123 opal_dss_type_info_t *info;
124 int32_t n=1;
125
126
127
128 if (NULL == (info = (opal_dss_type_info_t*)opal_pointer_array_get_item(&opal_dss_types, OPAL_DATA_TYPE_T))) {
129 return OPAL_ERR_PACK_FAILURE;
130 }
131
132 return info->odti_unpack_fn(buffer, type, &n, OPAL_DATA_TYPE_T);
133 }