Changeset c24b0dcb in mainline for uspace/lib/c


Ignore:
Timestamp:
2019-09-30T16:38:33Z (6 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8f57fb2
Parents:
ef0a3375
git-author:
Jakub Jermar <jakub@…> (2019-09-30 16:34:10)
git-committer:
Jakub Jermar <jakub@…> (2019-09-30 16:38:33)
Message:

Rename capacity-related 'cap' to 'capa'

This allows to use 'cap' for capabilities.

Location:
uspace/lib/c
Files:
2 edited
3 moved

Legend:

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

    ref0a3375 rc24b0dcb  
    3434 */
    3535
    36 #include <cap.h>
     36#include <capa.h>
    3737#include <errno.h>
    3838#include <imath.h>
     
    4343enum {
    4444        /** Simplified capacity maximum integer digits */
    45         scap_max_idig = 3,
     45        scapa_max_idig = 3,
    4646        /** Simplified capacity maximum significant digits */
    47         scap_max_sdig = 4
     47        scapa_max_sdig = 4
    4848};
    4949
     
    6060};
    6161
    62 void cap_from_blocks(uint64_t nblocks, size_t block_size, cap_spec_t *cap)
     62void capa_from_blocks(uint64_t nblocks, size_t block_size, capa_spec_t *capa)
    6363{
    6464        uint64_t tsize;
    6565
    6666        tsize = nblocks * block_size;
    67         cap->m = tsize;
    68         cap->dp = 0;
    69         cap->cunit = cu_byte;
     67        capa->m = tsize;
     68        capa->dp = 0;
     69        capa->cunit = cu_byte;
    7070}
    7171
     
    8181 * and @c cv_max gives the maximum value.
    8282 */
    83 errno_t cap_to_blocks(cap_spec_t *cap, cap_vsel_t cvsel, size_t block_size,
     83errno_t capa_to_blocks(capa_spec_t *capa, capa_vsel_t cvsel, size_t block_size,
    8484    uint64_t *rblocks)
    8585{
     
    9292        errno_t rc;
    9393
    94         exp = cap->cunit * 3 - cap->dp;
     94        exp = capa->cunit * 3 - capa->dp;
    9595        if (exp < 0) {
    9696                rc = ipow10_u64(-exp, &f);
    9797                if (rc != EOK)
    9898                        return ERANGE;
    99                 bytes = (cap->m + (f / 2)) / f;
    100                 if (bytes * f - (f / 2) != cap->m)
     99                bytes = (capa->m + (f / 2)) / f;
     100                if (bytes * f - (f / 2) != capa->m)
    101101                        return ERANGE;
    102102        } else {
     
    118118                }
    119119
    120                 bytes = cap->m * f + adj;
    121                 if ((bytes - adj) / f != cap->m)
     120                bytes = capa->m * f + adj;
     121                if ((bytes - adj) / f != capa->m)
    122122                        return ERANGE;
    123123        }
     
    138138 * digits and at most two fractional digits, e.g abc.xy <unit>.
    139139 */
    140 void cap_simplify(cap_spec_t *cap)
     140void capa_simplify(capa_spec_t *capa)
    141141{
    142142        uint64_t div;
     
    146146        errno_t rc;
    147147
    148         /* Change units so that we have at most @c scap_max_idig integer digits */
    149         rc = ipow10_u64(scap_max_idig, &maxv);
     148        /* Change units so that we have at most @c scapa_max_idig integer digits */
     149        rc = ipow10_u64(scapa_max_idig, &maxv);
    150150        assert(rc == EOK);
    151151
    152         rc = ipow10_u64(cap->dp, &div);
     152        rc = ipow10_u64(capa->dp, &div);
    153153        assert(rc == EOK);
    154154
    155         while (cap->m / div >= maxv) {
    156                 ++cap->cunit;
    157                 cap->dp += 3;
     155        while (capa->m / div >= maxv) {
     156                ++capa->cunit;
     157                capa->dp += 3;
    158158                div = div * 1000;
    159159        }
    160160
    161         /* Round the number so that we have at most @c scap_max_sdig significant digits */
    162         sdig = 1 + ilog10_u64(cap->m); /* number of significant digits */
    163         if (sdig > scap_max_sdig) {
     161        /* Round the number so that we have at most @c scapa_max_sdig significant digits */
     162        sdig = 1 + ilog10_u64(capa->m); /* number of significant digits */
     163        if (sdig > scapa_max_sdig) {
    164164                /* Number of digits to remove */
    165                 rdig = sdig - scap_max_sdig;
    166                 if (rdig > cap->dp)
    167                         rdig = cap->dp;
     165                rdig = sdig - scapa_max_sdig;
     166                if (rdig > capa->dp)
     167                        rdig = capa->dp;
    168168
    169169                rc = ipow10_u64(rdig, &div);
    170170                assert(rc == EOK);
    171171
    172                 cap->m = (cap->m + (div / 2)) / div;
    173                 cap->dp -= rdig;
    174         }
    175 }
    176 
    177 errno_t cap_format(cap_spec_t *cap, char **rstr)
     172                capa->m = (capa->m + (div / 2)) / div;
     173                capa->dp -= rdig;
     174        }
     175}
     176
     177errno_t capa_format(capa_spec_t *capa, char **rstr)
    178178{
    179179        errno_t rc;
     
    186186        sunit = NULL;
    187187
    188         assert(cap->cunit < CU_LIMIT);
    189 
    190         rc = ipow10_u64(cap->dp, &div);
     188        assert(capa->cunit < CU_LIMIT);
     189
     190        rc = ipow10_u64(capa->dp, &div);
    191191        if (rc != EOK)
    192192                return rc;
    193193
    194         ipart = cap->m / div;
    195         fpart = cap->m % div;
    196 
    197         sunit = cu_str[cap->cunit];
    198         if (cap->dp > 0) {
     194        ipart = capa->m / div;
     195        fpart = capa->m % div;
     196
     197        sunit = cu_str[capa->cunit];
     198        if (capa->dp > 0) {
    199199                ret = asprintf(rstr, "%" PRIu64 ".%0*" PRIu64 " %s", ipart,
    200                     (int)cap->dp, fpart, sunit);
     200                    (int)capa->dp, fpart, sunit);
    201201        } else {
    202202                ret = asprintf(rstr, "%" PRIu64 " %s", ipart, sunit);
     
    208208}
    209209
    210 static errno_t cap_digit_val(char c, int *val)
     210static errno_t capa_digit_val(char c, int *val)
    211211{
    212212        switch (c) {
     
    248248}
    249249
    250 errno_t cap_parse(const char *str, cap_spec_t *cap)
     250errno_t capa_parse(const char *str, capa_spec_t *capa)
    251251{
    252252        const char *eptr;
     
    260260
    261261        eptr = str;
    262         while (cap_digit_val(*eptr, &d) == EOK) {
     262        while (capa_digit_val(*eptr, &d) == EOK) {
    263263                m = m * 10 + d;
    264264                ++eptr;
     
    268268                ++eptr;
    269269                dp = 0;
    270                 while (cap_digit_val(*eptr, &d) == EOK) {
     270                while (capa_digit_val(*eptr, &d) == EOK) {
    271271                        m = m * 10 + d;
    272272                        ++dp;
     
    281281
    282282        if (*eptr == '\0') {
    283                 cap->cunit = cu_byte;
     283                capa->cunit = cu_byte;
    284284        } else {
    285285                for (i = 0; i < CU_LIMIT; i++) {
     
    296296                return EINVAL;
    297297        found:
    298                 cap->cunit = i;
    299         }
    300 
    301         cap->m = m;
    302         cap->dp = dp;
     298                capa->cunit = i;
     299        }
     300
     301        capa->m = m;
     302        capa->dp = dp;
    303303        return EOK;
    304304}
  • uspace/lib/c/include/capa.h

    ref0a3375 rc24b0dcb  
    5555        cu_zbyte,
    5656        cu_ybyte
    57 } cap_unit_t;
     57} capa_unit_t;
    5858
    5959/** Which of values within the precision of the capacity */
     
    6565        /** The maximum value */
    6666        cv_max
    67 } cap_vsel_t;
     67} capa_vsel_t;
    6868
    6969#define CU_LIMIT (cu_ybyte + 1)
     
    8787        unsigned dp;
    8888        /** Capacity unit */
    89         cap_unit_t cunit;
    90 } cap_spec_t;
     89        capa_unit_t cunit;
     90} capa_spec_t;
    9191
    92 extern errno_t cap_format(cap_spec_t *, char **);
    93 extern errno_t cap_parse(const char *, cap_spec_t *);
    94 extern void cap_simplify(cap_spec_t *);
    95 extern void cap_from_blocks(uint64_t, size_t, cap_spec_t *);
    96 extern errno_t cap_to_blocks(cap_spec_t *, cap_vsel_t, size_t, uint64_t *);
     92extern errno_t capa_format(capa_spec_t *, char **);
     93extern errno_t capa_parse(const char *, capa_spec_t *);
     94extern void capa_simplify(capa_spec_t *);
     95extern void capa_from_blocks(uint64_t, size_t, capa_spec_t *);
     96extern errno_t capa_to_blocks(capa_spec_t *, capa_vsel_t, size_t, uint64_t *);
    9797
    9898#endif
  • uspace/lib/c/meson.build

    ref0a3375 rc24b0dcb  
    6565        'generic/bd_srv.c',
    6666        'generic/perm.c',
    67         'generic/cap.c',
     67        'generic/capa.c',
    6868        'generic/clipboard.c',
    6969        'generic/config.c',
     
    204204        'test/adt/circ_buf.c',
    205205        'test/adt/odict.c',
    206         'test/cap.c',
     206        'test/capa.c',
    207207        'test/casting.c',
    208208        'test/double_to_str.c',
  • uspace/lib/c/test/capa.c

    ref0a3375 rc24b0dcb  
    2828
    2929#include <pcut/pcut.h>
    30 #include <cap.h>
     30#include <capa.h>
    3131
    3232PCUT_INIT;
    3333
    34 PCUT_TEST_SUITE(cap);
    35 
    36 PCUT_TEST(cap_format)
     34PCUT_TEST_SUITE(capa);
     35
     36PCUT_TEST(capa_format)
    3737{
    3838        int block_size = 4;
     
    8686        };
    8787
    88         cap_spec_t cap;
     88        capa_spec_t capa;
    8989        char *str;
    9090        errno_t rc;
     
    9393        for (i = 0; i < input_size; i++) {
    9494                for (x = 0; x < block_size; x++) {
    95                         cap_from_blocks(input[i], block[x], &cap);
    96                         cap_simplify(&cap);
    97 
    98                         rc = cap_format(&cap, &str);
     95                        capa_from_blocks(input[i], block[x], &capa);
     96                        capa_simplify(&capa);
     97
     98                        rc = capa_format(&capa, &str);
    9999
    100100                        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     
    102102                        free(str);
    103103
    104                         cap_from_blocks(block[x], input[i], &cap);
    105                         cap_simplify(&cap);
    106 
    107                         rc = cap_format(&cap, &str);
     104                        capa_from_blocks(block[x], input[i], &capa);
     105                        capa_simplify(&capa);
     106
     107                        rc = capa_format(&capa, &str);
    108108
    109109                        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     
    114114}
    115115
    116 PCUT_TEST(cap_format_rounding)
     116PCUT_TEST(capa_format_rounding)
    117117{
    118118        int input_size = 8;
     
    139139        };
    140140
    141         cap_spec_t cap;
     141        capa_spec_t capa;
    142142        char *str;
    143143        errno_t rc;
     
    145145        int i;
    146146        for (i = 0; i < input_size; i++) {
    147                 cap_from_blocks(input[i], 1, &cap);
    148                 cap_simplify(&cap);
    149 
    150                 rc = cap_format(&cap, &str);
     147                capa_from_blocks(input[i], 1, &capa);
     148                capa_simplify(&capa);
     149
     150                rc = capa_format(&capa, &str);
    151151
    152152                PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     
    154154                free(str);
    155155
    156                 cap_from_blocks(1, input[i], &cap);
    157                 cap_simplify(&cap);
    158 
    159                 rc = cap_format(&cap, &str);
     156                capa_from_blocks(1, input[i], &capa);
     157                capa_simplify(&capa);
     158
     159                rc = capa_format(&capa, &str);
    160160
    161161                PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     
    165165}
    166166
    167 PCUT_TEST(cap_parse)
     167PCUT_TEST(capa_parse)
    168168{
    169169        int input_size = 4;
     
    196196        };
    197197
    198         cap_spec_t cap;
     198        capa_spec_t capa;
    199199        errno_t rc;
    200200        int i;
    201201
    202202        for (i = 0; i < input_size; i++) {
    203                 rc = cap_parse(input[i], &cap);
    204 
    205                 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    206                 PCUT_ASSERT_INT_EQUALS(out_cunit[i], cap.cunit);
    207                 PCUT_ASSERT_INT_EQUALS(out_dp[i], cap.dp);
    208                 PCUT_ASSERT_INT_EQUALS(out_m[i], cap.m);
    209         }
    210 }
    211 
    212 PCUT_TEST(cap_to_blocks)
     203                rc = capa_parse(input[i], &capa);
     204
     205                PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     206                PCUT_ASSERT_INT_EQUALS(out_cunit[i], capa.cunit);
     207                PCUT_ASSERT_INT_EQUALS(out_dp[i], capa.dp);
     208                PCUT_ASSERT_INT_EQUALS(out_m[i], capa.m);
     209        }
     210}
     211
     212PCUT_TEST(capa_to_blocks)
    213213{
    214214        int input_size = 0;
     
    261261        };
    262262
    263         cap_spec_t cap;
     263        capa_spec_t capa;
    264264        errno_t rc;
    265265        int i;
     
    267267
    268268        for (i = 0; i < input_size; i++) {
    269                 cap.m = input_m[i];
    270                 cap.dp = input_dp[i];
    271                 cap.cunit = cu_kbyte;
    272 
    273                 rc = cap_to_blocks(&cap, cv_nom, block[i], &ret);
     269                capa.m = input_m[i];
     270                capa.dp = input_dp[i];
     271                capa.cunit = cu_kbyte;
     272
     273                rc = capa_to_blocks(&capa, cv_nom, block[i], &ret);
    274274                PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    275275                PCUT_ASSERT_INT_EQUALS(out_nom[i], ret);
    276276
    277                 rc = cap_to_blocks(&cap, cv_min, block[i], &ret);
     277                rc = capa_to_blocks(&capa, cv_min, block[i], &ret);
    278278                PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    279279                PCUT_ASSERT_INT_EQUALS(out_min[i], ret);
    280280
    281                 rc = cap_to_blocks(&cap, cv_max, block[i], &ret);
     281                rc = capa_to_blocks(&capa, cv_max, block[i], &ret);
    282282                PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    283283                PCUT_ASSERT_INT_EQUALS(out_max[i], ret);
     
    285285}
    286286
    287 PCUT_EXPORT(cap);
     287PCUT_EXPORT(capa);
  • uspace/lib/c/test/main.c

    ref0a3375 rc24b0dcb  
    3232PCUT_INIT;
    3333
    34 PCUT_IMPORT(cap);
     34PCUT_IMPORT(capa);
    3535PCUT_IMPORT(casting);
    3636PCUT_IMPORT(circ_buf);
Note: See TracChangeset for help on using the changeset viewer.