Changeset 8fb1bf82 in mainline for uspace/lib/c/generic/adt/measured_strings.c
- Timestamp:
- 2010-11-25T13:42:50Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 8df8415
- Parents:
- a93d79a (diff), eb667613 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/adt/measured_strings.c
ra93d79a r8fb1bf82 41 41 #include <unistd.h> 42 42 #include <errno.h> 43 #include <err.h>44 43 #include <async.h> 45 44 … … 52 51 * @param[in] string The initial character string to be stored. 53 52 * @param[in] length The length of the given string without the terminating 54 * zero (' /0') character. If the length is zero (0), the55 * actuallength is computed. The given length is used and56 * appended with the terminating zero ('\ \0') character53 * zero ('\0') character. If the length is zero, the actual 54 * length is computed. The given length is used and 55 * appended with the terminating zero ('\0') character 57 56 * otherwise. 58 * @return sThe new bundled character string with measured length.59 * @return sNULL if there is not enough memory left.60 */ 61 measured_string_ ref62 measured_string_create_bulk(const char * 63 { 64 measured_string_ refnew;57 * @return The new bundled character string with measured length. 58 * @return NULL if there is not enough memory left. 59 */ 60 measured_string_t * 61 measured_string_create_bulk(const char *string, size_t length) 62 { 63 measured_string_t *new; 65 64 66 65 if (length == 0) { 67 66 while (string[length]) 68 ++length;69 } 70 new = (measured_string_ ref) malloc(sizeof(measured_string_t) +67 length++; 68 } 69 new = (measured_string_t *) malloc(sizeof(measured_string_t) + 71 70 (sizeof(char) * (length + 1))); 72 71 if (!new) … … 85 84 * 86 85 * @param[in] source The source measured string to be copied. 87 * @return sThe copy of the given measured string.88 * @return sNULL if the source parameter is NULL.89 * @return sNULL if there is not enough memory left.90 */ 91 measured_string_ ref measured_string_copy(measured_string_refsource)92 { 93 measured_string_ refnew;86 * @return The copy of the given measured string. 87 * @return NULL if the source parameter is NULL. 88 * @return NULL if there is not enough memory left. 89 */ 90 measured_string_t *measured_string_copy(measured_string_t *source) 91 { 92 measured_string_t *new; 94 93 95 94 if (!source) 96 95 return NULL; 97 96 98 new = (measured_string_ ref) malloc(sizeof(measured_string_t));97 new = (measured_string_t *) malloc(sizeof(measured_string_t)); 99 98 if (new) { 100 99 new->value = (char *) malloc(source->length + 1); … … 104 103 new->value[new->length] = '\0'; 105 104 return new; 106 } else {107 free(new);108 105 } 106 free(new); 109 107 } 110 108 … … 122 120 * actual character strings. 123 121 * @param[in] count The size of the measured strings array. 124 * @return sEOK on success.125 * @return sEINVAL if the strings or data parameter is NULL.126 * @return sEINVAL if the count parameter is zero (0).127 * @return sEINVAL if the sent array differs in size.128 * @return sEINVAL if there is inconsistency in sent measured122 * @return EOK on success. 123 * @return EINVAL if the strings or data parameter is NULL. 124 * @return EINVAL if the count parameter is zero (0). 125 * @return EINVAL if the sent array differs in size. 126 * @return EINVAL if there is inconsistency in sent measured 129 127 * strings' lengths (should not occur). 130 * @return sENOMEM if there is not enough memory left.131 * @return sOther error codes as defined for the128 * @return ENOMEM if there is not enough memory left. 129 * @return Other error codes as defined for the 132 130 * async_data_write_finalize() function. 133 131 */ 134 132 int 135 measured_strings_receive(measured_string_ ref*strings, char **data,133 measured_strings_receive(measured_string_t **strings, char **data, 136 134 size_t count) 137 135 { 138 ERROR_DECLARE;139 140 136 size_t *lengths; 141 137 size_t index; … … 143 139 char *next; 144 140 ipc_callid_t callid; 141 int rc; 145 142 146 143 if ((!strings) || (!data) || (count <= 0)) … … 156 153 return EINVAL; 157 154 } 158 if(ERROR_OCCURRED(async_data_write_finalize(callid, lengths,159 sizeof(size_t) * (count + 1)))) {160 free(lengths); 161 return ERROR_CODE;155 rc = async_data_write_finalize(callid, lengths, length); 156 if (rc != EOK) { 157 free(lengths); 158 return rc; 162 159 } 163 160 164 161 *data = malloc(lengths[count]); 165 if (! (*data)) {162 if (!*data) { 166 163 free(lengths); 167 164 return ENOMEM; … … 169 166 (*data)[lengths[count] - 1] = '\0'; 170 167 171 *strings = (measured_string_ ref) malloc(sizeof(measured_string_t) *168 *strings = (measured_string_t *) malloc(sizeof(measured_string_t) * 172 169 count); 173 if (! (*strings)) {170 if (!*strings) { 174 171 free(lengths); 175 172 free(*data); … … 178 175 179 176 next = *data; 180 for (index = 0; index < count; ++index) {177 for (index = 0; index < count; index++) { 181 178 (*strings)[index].length = lengths[index]; 182 179 if (lengths[index] > 0) { 183 if ( (!async_data_write_receive(&callid, &length)) ||180 if (!async_data_write_receive(&callid, &length) || 184 181 (length != lengths[index])) { 185 182 free(*data); … … 188 185 return EINVAL; 189 186 } 190 ERROR_PROPAGATE(async_data_write_finalize(callid, next, 191 lengths[index])); 187 rc = async_data_write_finalize(callid, next, 188 lengths[index]); 189 if (rc != EOK) { 190 free(*data); 191 free(*strings); 192 free(lengths); 193 return rc; 194 } 192 195 (*strings)[index].value = next; 193 196 next += lengths[index]; 194 *next = '\0'; 195 ++next; 197 *next++ = '\0'; 196 198 } else { 197 199 (*strings)[index].value = NULL; … … 207 209 * @param[in] strings The measured strings array to be processed. 208 210 * @param[in] count The measured strings array size. 209 * @return sThe computed sizes array.210 * @return sNULL if there is not enough memory left.211 */ 212 static size_t *prepare_lengths(const measured_string_ refstrings, size_t count)211 * @return The computed sizes array. 212 * @return NULL if there is not enough memory left. 213 */ 214 static size_t *prepare_lengths(const measured_string_t *strings, size_t count) 213 215 { 214 216 size_t *lengths; … … 221 223 222 224 length = 0; 223 for (index = 0; index < count; ++ index) {225 for (index = 0; index < count; index++) { 224 226 lengths[index] = strings[index].length; 225 227 length += lengths[index] + 1; … … 236 238 * @param[in] strings The measured strings array to be transferred. 237 239 * @param[in] count The measured strings array size. 238 * @return sEOK on success.239 * @return sEINVAL if the strings parameter is NULL.240 * @return sEINVAL if the count parameter is zero (0).241 * @return sEINVAL if the calling module does not accept the given240 * @return EOK on success. 241 * @return EINVAL if the strings parameter is NULL. 242 * @return EINVAL if the count parameter is zero (0). 243 * @return EINVAL if the calling module does not accept the given 242 244 * array size. 243 * @return sEINVAL if there is inconsistency in sent measured245 * @return EINVAL if there is inconsistency in sent measured 244 246 * strings' lengths (should not occur). 245 * @return sOther error codes as defined for the247 * @return Other error codes as defined for the 246 248 * async_data_read_finalize() function. 247 249 */ 248 int measured_strings_reply(const measured_string_ref strings, size_t count) 249 { 250 ERROR_DECLARE; 251 250 int measured_strings_reply(const measured_string_t *strings, size_t count) 251 { 252 252 size_t *lengths; 253 253 size_t index; 254 254 size_t length; 255 255 ipc_callid_t callid; 256 int rc; 256 257 257 258 if ((!strings) || (count <= 0)) … … 262 263 return ENOMEM; 263 264 264 if ( (!async_data_read_receive(&callid, &length)) ||265 if (!async_data_read_receive(&callid, &length) || 265 266 (length != sizeof(size_t) * (count + 1))) { 266 267 free(lengths); 267 268 return EINVAL; 268 269 } 269 if (ERROR_OCCURRED(async_data_read_finalize(callid, lengths,270 sizeof(size_t) * (count + 1)))) {271 free(lengths); 272 return ERROR_CODE;270 rc = async_data_read_finalize(callid, lengths, length); 271 if (rc != EOK) { 272 free(lengths); 273 return rc; 273 274 } 274 275 free(lengths); 275 276 276 for (index = 0; index < count; ++ index) {277 for (index = 0; index < count; index++) { 277 278 if (strings[index].length > 0) { 278 if ((!async_data_read_receive(&callid, &length))||279 if (!async_data_read_receive(&callid, &length) || 279 280 (length != strings[index].length)) { 280 281 return EINVAL; 281 282 } 282 ERROR_PROPAGATE(async_data_read_finalize(callid, 283 strings[index].value, strings[index].length)); 283 rc = async_data_read_finalize(callid, 284 strings[index].value, strings[index].length); 285 if (rc != EOK) 286 return rc; 284 287 } 285 288 } … … 299 302 * actual character strings. 300 303 * @param[in] count The size of the measured strings array. 301 * @return sEOK on success.302 * @return sEINVAL if the strings or data parameter is NULL.303 * @return sEINVAL if the phone or count parameter is not positive.304 * @return sEINVAL if the sent array differs in size.305 * @return sENOMEM if there is not enough memory left.306 * @return sOther error codes as defined for the304 * @return EOK on success. 305 * @return EINVAL if the strings or data parameter is NULL. 306 * @return EINVAL if the phone or count parameter is not positive. 307 * @return EINVAL if the sent array differs in size. 308 * @return ENOMEM if there is not enough memory left. 309 * @return Other error codes as defined for the 307 310 * async_data_read_start() function. 308 311 */ 309 312 int 310 measured_strings_return(int phone, measured_string_ ref*strings, char **data,313 measured_strings_return(int phone, measured_string_t **strings, char **data, 311 314 size_t count) 312 315 { 313 ERROR_DECLARE;314 315 316 size_t *lengths; 316 317 size_t index; 317 318 char *next; 318 319 if ((phone <= 0) || (!strings) || (!data) || (count <= 0)) 319 int rc; 320 321 if ((phone < 0) || (!strings) || (!data) || (count <= 0)) 320 322 return EINVAL; 321 323 … … 324 326 return ENOMEM; 325 327 326 if (ERROR_OCCURRED(async_data_read_start(phone, lengths, 327 sizeof(size_t) * (count + 1)))) { 328 free(lengths); 329 return ERROR_CODE; 328 rc = async_data_read_start(phone, lengths, 329 sizeof(size_t) * (count + 1)); 330 if (rc != EOK) { 331 free(lengths); 332 return rc; 330 333 } 331 334 332 335 *data = malloc(lengths[count]); 333 if (! (*data)) {334 free(lengths); 335 return ENOMEM; 336 } 337 338 *strings = (measured_string_ ref) malloc(sizeof(measured_string_t) *336 if (!*data) { 337 free(lengths); 338 return ENOMEM; 339 } 340 341 *strings = (measured_string_t *) malloc(sizeof(measured_string_t) * 339 342 count); 340 if (! (*strings)) {343 if (!*strings) { 341 344 free(lengths); 342 345 free(*data); … … 345 348 346 349 next = *data; 347 for (index = 0; index < count; ++ index) {350 for (index = 0; index < count; index++) { 348 351 (*strings)[index].length = lengths[index]; 349 352 if (lengths[index] > 0) { 350 ERROR_PROPAGATE(async_data_read_start(phone, next, 351 lengths[index])); 353 rc = async_data_read_start(phone, next, lengths[index]); 354 if (rc != EOK) { 355 free(lengths); 356 free(data); 357 free(strings); 358 return rc; 359 } 352 360 (*strings)[index].value = next; 353 361 next += lengths[index]; 354 *next = '\0'; 355 ++ next; 362 *next++ = '\0'; 356 363 } else { 357 364 (*strings)[index].value = NULL; … … 371 378 * @param[in] strings The measured strings array to be transferred. 372 379 * @param[in] count The measured strings array size. 373 * @return sEOK on success.374 * @return sEINVAL if the strings parameter is NULL.375 * @return sEINVAL if the phone or count parameter is not positive.376 * @return sOther error codes as defined for the380 * @return EOK on success. 381 * @return EINVAL if the strings parameter is NULL. 382 * @return EINVAL if the phone or count parameter is not positive. 383 * @return Other error codes as defined for the 377 384 * async_data_write_start() function. 378 385 */ 379 386 int 380 measured_strings_send(int phone, const measured_string_ refstrings,387 measured_strings_send(int phone, const measured_string_t *strings, 381 388 size_t count) 382 389 { 383 ERROR_DECLARE;384 385 390 size_t *lengths; 386 391 size_t index; 387 388 if ((phone <= 0) || (!strings) || (count <= 0)) 392 int rc; 393 394 if ((phone < 0) || (!strings) || (count <= 0)) 389 395 return EINVAL; 390 396 … … 393 399 return ENOMEM; 394 400 395 if (ERROR_OCCURRED(async_data_write_start(phone, lengths, 396 sizeof(size_t) * (count + 1)))) { 397 free(lengths); 398 return ERROR_CODE; 401 rc = async_data_write_start(phone, lengths, 402 sizeof(size_t) * (count + 1)); 403 if (rc != EOK) { 404 free(lengths); 405 return rc; 399 406 } 400 407 401 408 free(lengths); 402 409 403 for (index = 0; index < count; ++index) {410 for (index = 0; index < count; index++) { 404 411 if (strings[index].length > 0) { 405 ERROR_PROPAGATE(async_data_write_start(phone, 406 strings[index].value, strings[index].length)); 412 rc = async_data_write_start(phone, strings[index].value, 413 strings[index].length); 414 if (rc != EOK) 415 return rc; 407 416 } 408 417 }
Note:
See TracChangeset
for help on using the changeset viewer.