Changeset c24b0dcb in mainline for uspace/lib/c/generic/capa.c
- Timestamp:
- 2019-09-30T16:38:33Z (5 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)
- File:
-
- 1 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 }
Note:
See TracChangeset
for help on using the changeset viewer.