Changeset 278ac72 in mainline for uspace/lib/c/generic/loc.c


Ignore:
Timestamp:
2011-08-16T15:35:56Z (13 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
30c78c0
Parents:
16dc887
Message:

Add API for getting list of categories.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/loc.c

    r16dc887 r278ac72  
    589589}
    590590
    591 static int loc_category_get_svcs_internal(category_id_t cat_id,
    592     service_id_t *id_buf, size_t buf_size, size_t *act_size)
     591static int loc_category_get_ids_once(sysarg_t method, sysarg_t arg1,
     592    sysarg_t *id_buf, size_t buf_size, size_t *act_size)
    593593{
    594594        async_exch_t *exch = loc_exchange_begin_blocking(LOC_PORT_CONSUMER);
    595595
    596596        ipc_call_t answer;
    597         aid_t req = async_send_1(exch, LOC_CATEGORY_GET_SVCS, cat_id,
    598             &answer);
     597        aid_t req = async_send_1(exch, method, arg1, &answer);
    599598        int rc = async_data_read_start(exch, id_buf, buf_size);
    600599       
     
    614613       
    615614        *act_size = IPC_GET_ARG1(answer);
     615        return EOK;
     616}
     617
     618/** Get list of IDs.
     619 *
     620 * Returns an allocated array of service IDs.
     621 *
     622 * @param method        IPC method
     623 * @param arg1          IPC argument 1
     624 * @param data          Place to store pointer to array of IDs
     625 * @param count         Place to store number of IDs
     626 * @return              EOK on success or negative error code
     627 */
     628static int loc_get_ids_internal(sysarg_t method, sysarg_t arg1,
     629    sysarg_t **data, size_t *count)
     630{
     631        service_id_t *ids;
     632        size_t act_size;
     633        size_t alloc_size;
     634        int rc;
     635
     636        *data = NULL;
     637        act_size = 0;   /* silence warning */
     638
     639        rc = loc_category_get_ids_once(method, arg1, NULL, 0,
     640            &act_size);
     641        if (rc != EOK)
     642                return rc;
     643
     644        alloc_size = act_size;
     645        ids = malloc(alloc_size);
     646        if (ids == NULL)
     647                return ENOMEM;
     648
     649        while (true) {
     650                rc = loc_category_get_ids_once(method, arg1, ids, alloc_size,
     651                    &act_size);
     652                if (rc != EOK)
     653                        return rc;
     654
     655                if (act_size <= alloc_size)
     656                        break;
     657
     658                alloc_size *= 2;
     659                free(ids);
     660
     661                ids = malloc(alloc_size);
     662                if (ids == NULL)
     663                        return ENOMEM;
     664        }
     665
     666        *count = act_size / sizeof(category_id_t);
     667        *data = ids;
    616668        return EOK;
    617669}
     
    626678 * @return              EOK on success or negative error code
    627679 */
    628 int loc_category_get_svcs(category_id_t cat_id, category_id_t **data,
     680int loc_category_get_svcs(category_id_t cat_id, service_id_t **data,
    629681    size_t *count)
    630682{
    631         service_id_t *ids;
    632         size_t act_size;
    633         size_t alloc_size;
    634         int rc;
    635 
    636         *data = NULL;
    637         act_size = 0;   /* silence warning */
    638 
    639         rc = loc_category_get_svcs_internal(cat_id, NULL, 0, &act_size);
    640         if (rc != EOK)
    641                 return rc;
    642 
    643         alloc_size = act_size;
    644         ids = malloc(alloc_size);
    645         if (ids == NULL)
    646                 return ENOMEM;
    647 
    648         while (true) {
    649                 rc = loc_category_get_svcs_internal(cat_id, ids, alloc_size,
    650                     &act_size);
    651                 if (rc != EOK)
    652                         return rc;
    653 
    654                 if (act_size <= alloc_size)
    655                         break;
    656 
    657                 alloc_size *= 2;
    658                 free(ids);
    659 
    660                 ids = malloc(alloc_size);
    661                 if (ids == NULL)
    662                         return ENOMEM;
    663         }
    664 
    665         *count = act_size / sizeof(category_id_t);
    666         *data = ids;
    667         return EOK;
    668 }
     683        return loc_get_ids_internal(LOC_CATEGORY_GET_SVCS, cat_id,
     684            data, count);
     685}
     686
     687/** Get list of categories.
     688 *
     689 * Returns an allocated array of category IDs.
     690 *
     691 * @param data          Place to store pointer to array of IDs
     692 * @param count         Place to store number of IDs
     693 * @return              EOK on success or negative error code
     694 */
     695int loc_get_categories(category_id_t **data, size_t *count)
     696{
     697        return loc_get_ids_internal(LOC_GET_CATEGORIES, 0,
     698            data, count);
     699}
Note: See TracChangeset for help on using the changeset viewer.