1 /*
2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
3  *                         University Research and Technology
4  *                         Corporation.  All rights reserved.
5  * Copyright (c) 2004-2006 The University of Tennessee and The University
6  *                         of Tennessee Research Foundation.  All rights
7  *                         reserved.
8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
9  *                         University of Stuttgart.  All rights reserved.
10  * Copyright (c) 2004-2005 The Regents of the University of California.
11  *                         All rights reserved.
12  * Copyright (c) 2017      Intel, Inc.  All rights reserved.
13  * $COPYRIGHT$
14  *
15  * Additional copyrights may follow
16  *
17  * $HEADER$
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  * Internal function that resizes (expands) an inuse buffer if
33  * necessary.
34  */
opal_dss_buffer_extend(opal_buffer_t * buffer,size_t bytes_to_add)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     /* Check to see if we have enough space already */
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     /* All done */
80 
81     return buffer->pack_ptr;
82 }
83 
84 /*
85  * Internal function that checks to see if the specified number of bytes
86  * remain in the buffer for unpacking
87  */
opal_dss_too_small(opal_buffer_t * buffer,size_t bytes_reqd)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         /* don't error log this - it could be that someone is trying to
100          * simply read until the buffer is empty
101          */
102         return true;
103     }
104 
105     return false;
106 }
107 
opal_dss_store_data_type(opal_buffer_t * buffer,opal_data_type_t type)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     /* Lookup the pack function for the actual opal_data_type type and call it */
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 
opal_dss_get_data_type(opal_buffer_t * buffer,opal_data_type_t * type)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     /* Lookup the unpack function for the actual opal_data_type type and call it */
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 }
134