Changeset c24b0dcb in mainline for uspace/lib/c
- Timestamp:
- 2019-09-30T16:38:33Z (6 years ago)
- 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)
- Location:
- uspace/lib/c
- Files:
-
- 2 edited
- 3 moved
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/capa.c
ref0a3375 rc24b0dcb 34 34 */ 35 35 36 #include <cap .h>36 #include <capa.h> 37 37 #include <errno.h> 38 38 #include <imath.h> … … 43 43 enum { 44 44 /** Simplified capacity maximum integer digits */ 45 scap _max_idig = 3,45 scapa_max_idig = 3, 46 46 /** Simplified capacity maximum significant digits */ 47 scap _max_sdig = 447 scapa_max_sdig = 4 48 48 }; 49 49 … … 60 60 }; 61 61 62 void cap _from_blocks(uint64_t nblocks, size_t block_size, cap_spec_t *cap)62 void capa_from_blocks(uint64_t nblocks, size_t block_size, capa_spec_t *capa) 63 63 { 64 64 uint64_t tsize; 65 65 66 66 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; 70 70 } 71 71 … … 81 81 * and @c cv_max gives the maximum value. 82 82 */ 83 errno_t cap _to_blocks(cap_spec_t *cap, cap_vsel_t cvsel, size_t block_size,83 errno_t capa_to_blocks(capa_spec_t *capa, capa_vsel_t cvsel, size_t block_size, 84 84 uint64_t *rblocks) 85 85 { … … 92 92 errno_t rc; 93 93 94 exp = cap ->cunit * 3 - cap->dp;94 exp = capa->cunit * 3 - capa->dp; 95 95 if (exp < 0) { 96 96 rc = ipow10_u64(-exp, &f); 97 97 if (rc != EOK) 98 98 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) 101 101 return ERANGE; 102 102 } else { … … 118 118 } 119 119 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) 122 122 return ERANGE; 123 123 } … … 138 138 * digits and at most two fractional digits, e.g abc.xy <unit>. 139 139 */ 140 void cap _simplify(cap_spec_t *cap)140 void capa_simplify(capa_spec_t *capa) 141 141 { 142 142 uint64_t div; … … 146 146 errno_t rc; 147 147 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); 150 150 assert(rc == EOK); 151 151 152 rc = ipow10_u64(cap ->dp, &div);152 rc = ipow10_u64(capa->dp, &div); 153 153 assert(rc == EOK); 154 154 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; 158 158 div = div * 1000; 159 159 } 160 160 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) { 164 164 /* 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; 168 168 169 169 rc = ipow10_u64(rdig, &div); 170 170 assert(rc == EOK); 171 171 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 177 errno_t capa_format(capa_spec_t *capa, char **rstr) 178 178 { 179 179 errno_t rc; … … 186 186 sunit = NULL; 187 187 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); 191 191 if (rc != EOK) 192 192 return rc; 193 193 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) { 199 199 ret = asprintf(rstr, "%" PRIu64 ".%0*" PRIu64 " %s", ipart, 200 (int)cap ->dp, fpart, sunit);200 (int)capa->dp, fpart, sunit); 201 201 } else { 202 202 ret = asprintf(rstr, "%" PRIu64 " %s", ipart, sunit); … … 208 208 } 209 209 210 static errno_t cap _digit_val(char c, int *val)210 static errno_t capa_digit_val(char c, int *val) 211 211 { 212 212 switch (c) { … … 248 248 } 249 249 250 errno_t cap _parse(const char *str, cap_spec_t *cap)250 errno_t capa_parse(const char *str, capa_spec_t *capa) 251 251 { 252 252 const char *eptr; … … 260 260 261 261 eptr = str; 262 while (cap _digit_val(*eptr, &d) == EOK) {262 while (capa_digit_val(*eptr, &d) == EOK) { 263 263 m = m * 10 + d; 264 264 ++eptr; … … 268 268 ++eptr; 269 269 dp = 0; 270 while (cap _digit_val(*eptr, &d) == EOK) {270 while (capa_digit_val(*eptr, &d) == EOK) { 271 271 m = m * 10 + d; 272 272 ++dp; … … 281 281 282 282 if (*eptr == '\0') { 283 cap ->cunit = cu_byte;283 capa->cunit = cu_byte; 284 284 } else { 285 285 for (i = 0; i < CU_LIMIT; i++) { … … 296 296 return EINVAL; 297 297 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; 303 303 return EOK; 304 304 } -
uspace/lib/c/include/capa.h
ref0a3375 rc24b0dcb 55 55 cu_zbyte, 56 56 cu_ybyte 57 } cap _unit_t;57 } capa_unit_t; 58 58 59 59 /** Which of values within the precision of the capacity */ … … 65 65 /** The maximum value */ 66 66 cv_max 67 } cap _vsel_t;67 } capa_vsel_t; 68 68 69 69 #define CU_LIMIT (cu_ybyte + 1) … … 87 87 unsigned dp; 88 88 /** Capacity unit */ 89 cap _unit_t cunit;90 } cap _spec_t;89 capa_unit_t cunit; 90 } capa_spec_t; 91 91 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 *);92 extern errno_t capa_format(capa_spec_t *, char **); 93 extern errno_t capa_parse(const char *, capa_spec_t *); 94 extern void capa_simplify(capa_spec_t *); 95 extern void capa_from_blocks(uint64_t, size_t, capa_spec_t *); 96 extern errno_t capa_to_blocks(capa_spec_t *, capa_vsel_t, size_t, uint64_t *); 97 97 98 98 #endif -
uspace/lib/c/meson.build
ref0a3375 rc24b0dcb 65 65 'generic/bd_srv.c', 66 66 'generic/perm.c', 67 'generic/cap .c',67 'generic/capa.c', 68 68 'generic/clipboard.c', 69 69 'generic/config.c', … … 204 204 'test/adt/circ_buf.c', 205 205 'test/adt/odict.c', 206 'test/cap .c',206 'test/capa.c', 207 207 'test/casting.c', 208 208 'test/double_to_str.c', -
uspace/lib/c/test/capa.c
ref0a3375 rc24b0dcb 28 28 29 29 #include <pcut/pcut.h> 30 #include <cap .h>30 #include <capa.h> 31 31 32 32 PCUT_INIT; 33 33 34 PCUT_TEST_SUITE(cap );35 36 PCUT_TEST(cap _format)34 PCUT_TEST_SUITE(capa); 35 36 PCUT_TEST(capa_format) 37 37 { 38 38 int block_size = 4; … … 86 86 }; 87 87 88 cap _spec_t cap;88 capa_spec_t capa; 89 89 char *str; 90 90 errno_t rc; … … 93 93 for (i = 0; i < input_size; i++) { 94 94 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); 99 99 100 100 PCUT_ASSERT_ERRNO_VAL(EOK, rc); … … 102 102 free(str); 103 103 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); 108 108 109 109 PCUT_ASSERT_ERRNO_VAL(EOK, rc); … … 114 114 } 115 115 116 PCUT_TEST(cap _format_rounding)116 PCUT_TEST(capa_format_rounding) 117 117 { 118 118 int input_size = 8; … … 139 139 }; 140 140 141 cap _spec_t cap;141 capa_spec_t capa; 142 142 char *str; 143 143 errno_t rc; … … 145 145 int i; 146 146 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); 151 151 152 152 PCUT_ASSERT_ERRNO_VAL(EOK, rc); … … 154 154 free(str); 155 155 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); 160 160 161 161 PCUT_ASSERT_ERRNO_VAL(EOK, rc); … … 165 165 } 166 166 167 PCUT_TEST(cap _parse)167 PCUT_TEST(capa_parse) 168 168 { 169 169 int input_size = 4; … … 196 196 }; 197 197 198 cap _spec_t cap;198 capa_spec_t capa; 199 199 errno_t rc; 200 200 int i; 201 201 202 202 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 212 PCUT_TEST(capa_to_blocks) 213 213 { 214 214 int input_size = 0; … … 261 261 }; 262 262 263 cap _spec_t cap;263 capa_spec_t capa; 264 264 errno_t rc; 265 265 int i; … … 267 267 268 268 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); 274 274 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 275 275 PCUT_ASSERT_INT_EQUALS(out_nom[i], ret); 276 276 277 rc = cap _to_blocks(&cap, cv_min, block[i], &ret);277 rc = capa_to_blocks(&capa, cv_min, block[i], &ret); 278 278 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 279 279 PCUT_ASSERT_INT_EQUALS(out_min[i], ret); 280 280 281 rc = cap _to_blocks(&cap, cv_max, block[i], &ret);281 rc = capa_to_blocks(&capa, cv_max, block[i], &ret); 282 282 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 283 283 PCUT_ASSERT_INT_EQUALS(out_max[i], ret); … … 285 285 } 286 286 287 PCUT_EXPORT(cap );287 PCUT_EXPORT(capa); -
uspace/lib/c/test/main.c
ref0a3375 rc24b0dcb 32 32 PCUT_INIT; 33 33 34 PCUT_IMPORT(cap );34 PCUT_IMPORT(capa); 35 35 PCUT_IMPORT(casting); 36 36 PCUT_IMPORT(circ_buf);
Note:
See TracChangeset
for help on using the changeset viewer.