Changeset 8aa42e3 in mainline for uspace/lib/libc/generic/async.c


Ignore:
Timestamp:
2009-12-13T15:07:21Z (14 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
531695f
Parents:
1e4cada
Message:

add two convenient functions for transfering data

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/libc/generic/async.c

    r1e4cada r8aa42e3  
    13451345}
    13461346
     1347/** Wrapper for receiving blobs via the async_data_write_*
     1348 *
     1349 * This wrapper only makes it more comfortable to use async_data_write_*
     1350 * functions to receive blobs.
     1351 *
     1352 * @param blob     Pointer to data pointer (which should be later disposed
     1353 *                 by free()). If the operation fails, the pointer is not
     1354 *                 touched.
     1355 * @param max_size Maximum size (in bytes) of the blob to receive. 0 means
     1356 *                 no limit.
     1357 * @param received If not NULL, the size of the received data is stored here.
     1358 *
     1359 * @return Zero on success or a value from @ref errno.h on failure.
     1360 *
     1361 */
     1362int async_data_blob_receive(char **blob, const size_t max_size, size_t *received)
     1363{
     1364        ipc_callid_t callid;
     1365        size_t size;
     1366        if (!async_data_write_receive(&callid, &size)) {
     1367                ipc_answer_0(callid, EINVAL);
     1368                return EINVAL;
     1369        }
     1370       
     1371        if ((max_size > 0) && (size > max_size)) {
     1372                ipc_answer_0(callid, EINVAL);
     1373                return EINVAL;
     1374        }
     1375       
     1376        char *data = (char *) malloc(size);
     1377        if (data == NULL) {
     1378                ipc_answer_0(callid, ENOMEM);
     1379                return ENOMEM;
     1380        }
     1381       
     1382        int rc = async_data_write_finalize(callid, data, size);
     1383        if (rc != EOK) {
     1384                free(data);
     1385                return rc;
     1386        }
     1387       
     1388        *blob = data;
     1389        if (received != NULL)
     1390                *received = size;
     1391       
     1392        return EOK;
     1393}
     1394
     1395/** Wrapper for receiving strings via the async_data_write_*
     1396 *
     1397 * This wrapper only makes it more comfortable to use async_data_write_*
     1398 * functions to receive strings.
     1399 *
     1400 * @param str      Pointer to string pointer (which should be later disposed
     1401 *                 by free()). If the operation fails, the pointer is not
     1402 *                 touched.
     1403 * @param max_size Maximum size (in bytes) of the string to receive. 0 means
     1404 *                 no limit.
     1405 *
     1406 * @return Zero on success or a value from @ref errno.h on failure.
     1407 *
     1408 */
     1409int async_data_string_receive(char **str, const size_t max_size)
     1410{
     1411        ipc_callid_t callid;
     1412        size_t size;
     1413        if (!async_data_write_receive(&callid, &size)) {
     1414                ipc_answer_0(callid, EINVAL);
     1415                return EINVAL;
     1416        }
     1417       
     1418        if ((max_size > 0) && (size > max_size)) {
     1419                ipc_answer_0(callid, EINVAL);
     1420                return EINVAL;
     1421        }
     1422       
     1423        char *data = (char *) malloc(size + 1);
     1424        if (data == NULL) {
     1425                ipc_answer_0(callid, ENOMEM);
     1426                return ENOMEM;
     1427        }
     1428       
     1429        int rc = async_data_write_finalize(callid, data, size);
     1430        if (rc != EOK) {
     1431                free(data);
     1432                return rc;
     1433        }
     1434       
     1435        data[size] = 0;
     1436        *str = data;
     1437        return EOK;
     1438}
     1439
    13471440/** @}
    13481441 */
Note: See TracChangeset for help on using the changeset viewer.