diff --git a/hypervisor/lib/memory.c b/hypervisor/lib/memory.c index 7d73df9f2..a81d0c50e 100644 --- a/hypervisor/lib/memory.c +++ b/hypervisor/lib/memory.c @@ -80,8 +80,9 @@ static void *allocate_mem(struct mem_pool *pool, unsigned int num_bytes) for (bit_idx = ffz64(pool->bitmap[idx]); bit_idx < BITMAP_WORD_SIZE; bit_idx++) { /* Check if selected buffer is free */ - if ((pool->bitmap[idx] & (1U << bit_idx)) != 0U) + if ((pool->bitmap[idx] & (1U << bit_idx)) != 0U) { continue; + } /* Declare temporary variables to be used locally in * this block @@ -109,8 +110,9 @@ static void *allocate_mem(struct mem_pool *pool, unsigned int num_bytes) /* Break if selected buffer is not free */ if ((pool->bitmap[tmp_idx] - & (1U << tmp_bit_idx)) != 0U) + & (1U << tmp_bit_idx)) != 0U) { break; + } } /* Check if requested_buffs number of free contiguous @@ -207,16 +209,18 @@ static void deallocate_mem(struct mem_pool *pool, void *ptr) contiguity_bitmask = &pool->contiguity_bitmap[bmp_idx]; /* Mark the buffer as free */ - if ((*bitmask & (1U << bit_idx)) != 0U) + if ((*bitmask & (1U << bit_idx)) != 0U) { *bitmask ^= (1U << bit_idx); - else + } else { break; + } /* Reset the Contiguity bit of buffer */ - if ((*contiguity_bitmask & (1U << bit_idx)) != 0U) + if ((*contiguity_bitmask & (1U << bit_idx)) != 0U) { *contiguity_bitmask ^= (1U << bit_idx); - else + } else { break; + } /* Increment buff_idx */ buff_idx++; @@ -245,8 +249,9 @@ void *malloc(unsigned int num_bytes) } /* Check if memory allocation is successful */ - if (memory == NULL) + if (memory == NULL) { pr_err("%s: failed to alloc 0x%x Bytes", __func__, num_bytes); + } /* Return memory pointer to caller */ return memory; @@ -260,8 +265,9 @@ void *alloc_pages(unsigned int page_num) memory = allocate_mem(&Paging_Memory_Pool, page_num * CPU_PAGE_SIZE); /* Check if memory allocation is successful */ - if (memory == NULL) + if (memory == NULL) { pr_err("%s: failed to alloc %d pages", __func__, page_num); + } return memory; } @@ -311,8 +317,9 @@ void *memchr(const void *void_s, int c, size_t n) unsigned char *end = ptr + n; while (ptr < end) { - if (*ptr == val) + if (*ptr == val) { return ((void *)ptr); + } ptr++; } return NULL; @@ -348,16 +355,19 @@ void *memcpy_s(void *d, size_t dmax, const void *s, size_t slen) uint8_t *dest8; uint8_t *src8; - if (slen == 0U || dmax == 0U || dmax < slen) + if (slen == 0U || dmax == 0U || dmax < slen) { ASSERT(false); + } if ((d > s && d <= s + slen - 1) - || (d < s && s <= d + dmax - 1)) + || (d < s && s <= d + dmax - 1)) { ASSERT(false); + } /*same memory block, no need to copy*/ - if (d == s) + if (d == s) { return d; + } dest8 = (uint8_t *)d; src8 = (uint8_t *)s; diff --git a/hypervisor/lib/sprintf.c b/hypervisor/lib/sprintf.c index 6aca43a34..08d3f51fe 100644 --- a/hypervisor/lib/sprintf.c +++ b/hypervisor/lib/sprintf.c @@ -99,8 +99,9 @@ static const char *get_int(const char *s, int *x) } /* apply sign to result */ - if (negative != 0) + if (negative != 0) { *x = -*x; + } return s; } @@ -143,12 +144,14 @@ static const char *get_flags(const char *s, int *flags) } /* Spec says that '-' has a higher priority than '0' */ - if ((*flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) + if ((*flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) { *flags &= ~PRINT_FLAG_PAD_ZERO; + } /* Spec says that '+' has a higher priority than ' ' */ - if ((*flags & PRINT_FLAG_SIGN) != 0) + if ((*flags & PRINT_FLAG_SIGN) != 0) { *flags &= ~PRINT_FLAG_SPACE; + } return s; } @@ -167,15 +170,15 @@ static const char *get_length_modifier(const char *s, *flags |= PRINT_FLAG_SHORT; *mask = 0x0000FFFF; } - } + } else if (*s == 'l') { /* check for l[l] (long/long long) */ - else if (*s == 'l') { s++; if (*s == 'l') { *flags |= PRINT_FLAG_LONG_LONG; ++s; - } else + } else { *flags |= PRINT_FLAG_LONG; + } } return s; @@ -199,12 +202,14 @@ static int format_number(struct print_param *param) width = param->vars.valuelen + param->vars.prefixlen; /* calculate additional characters for precision */ - if ((uint32_t)(param->vars.precision) > width) + if ((uint32_t)(param->vars.precision) > width) { p = param->vars.precision - width; + } /* calculate additional characters for width */ - if ((uint32_t)(param->vars.width) > (width + p)) + if ((uint32_t)(param->vars.width) > (width + p)) { w = param->vars.width - (width + p); + } /* handle case of right justification */ if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0) { @@ -243,21 +248,24 @@ static int format_number(struct print_param *param) /* emit prefix (if any), return early in case of an error */ res = param->emit(PRINT_CMD_COPY, param->vars.prefix, param->vars.prefixlen, param->data); - if ((param->vars.prefix != NULL) && (res < 0)) + if ((param->vars.prefix != NULL) && (res < 0)) { return res; + } /* insert additional 0's for precision, return early if an error * occurred */ res = param->emit(PRINT_CMD_FILL, "0", p, param->data); - if (res < 0) + if (res < 0) { return res; + } /* emit the pre-calculated result, return early in case of an error */ res = param->emit(PRINT_CMD_COPY, param->vars.value, param->vars.valuelen, param->data); - if (res < 0) + if (res < 0) { return res; + } /* handle left justification */ if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) { @@ -415,40 +423,46 @@ static int print_string(struct print_param *param, const char *s) /* we need the length of the string if either width or precision is * given */ - if ((param->vars.precision != 0)|| (param->vars.width != 0)) + if ((param->vars.precision != 0)|| (param->vars.width != 0)) { len = strnlen_s(s, PRINT_STRING_MAX_LEN); + } /* precision gives the max. number of characters to emit. */ - if ((param->vars.precision != 0) && (len > param->vars.precision)) + if ((param->vars.precision != 0) && (len > param->vars.precision)) { len = param->vars.precision; + } /* calculate the number of additional characters to get the required * width */ - if (param->vars.width > 0 && param->vars.width > len) + if (param->vars.width > 0 && param->vars.width > len) { w = param->vars.width - len; + } /* emit additional characters for width, return early if an error * occurred */ if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0) { res = param->emit(PRINT_CMD_FILL, " ", w, param->data); - if (res < 0) + if (res < 0) { return res; + } } /* emit the string, return early if an error occurred */ res = param->emit(PRINT_CMD_COPY, s, len, param->data); - if (res < 0) + if (res < 0) { return res; + } /* emit additional characters on the right, return early if an error * occurred */ if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) { res = param->emit(PRINT_CMD_FILL, " ", w, param->data); - if (res < 0) + if (res < 0) { return res; + } } return res; @@ -479,8 +493,9 @@ int do_print(const char *fmt, struct print_param *param, */ res = param->emit(PRINT_CMD_COPY, start, fmt - start, param->data); - if (res < 0) + if (res < 0) { return res; + } /* continue only if the '%' character was found */ if (*fmt == '%') { @@ -518,9 +533,8 @@ int do_print(const char *fmt, struct print_param *param, if (ch == '%') { res = param->emit(PRINT_CMD_COPY, &ch, 1, param->data); - } + } else if ((ch == 'd') || (ch == 'i')) { /* decimal number */ - else if ((ch == 'd') || (ch == 'i')) { res = print_decimal(param, ((param->vars.flags & PRINT_FLAG_LONG_LONG) != 0) ? @@ -556,8 +570,9 @@ int do_print(const char *fmt, struct print_param *param, } /* hexadecimal number */ else if ((ch == 'X') || (ch == 'x')) { - if (ch == 'X') + if (ch == 'X') { param->vars.flags |= PRINT_FLAG_UPPER; + } res = print_pow2(param, ((param->vars.flags & PRINT_FLAG_LONG_LONG) != 0) ? @@ -572,8 +587,9 @@ int do_print(const char *fmt, struct print_param *param, else if (ch == 's') { const char *s = __builtin_va_arg(args, char *); - if (s == NULL) + if (s == NULL) { s = "(null)"; + } res = print_string(param, s); } /* pointer argument */ @@ -601,8 +617,9 @@ int do_print(const char *fmt, struct print_param *param, } } /* return if an error occurred */ - if (res < 0) + if (res < 0) { return res; + } } /* done. Return the result of the last emit function call */ @@ -622,8 +639,9 @@ static int charmem(int cmd, const char *s, int sz, void *hnd) if (cmd == PRINT_CMD_COPY) { if (sz < 0) { while ((*s) != 0) { - if (n < param->sz - param->wrtn) + if (n < param->sz - param->wrtn) { *p = *s; + } p++; s++; n++; @@ -631,8 +649,9 @@ static int charmem(int cmd, const char *s, int sz, void *hnd) } else if (sz > 0) { while (((*s) != 0) && n < sz) { - if (n < param->sz - param->wrtn) + if (n < param->sz - param->wrtn) { *p = *s; + } p++; s++; n++; @@ -678,14 +697,17 @@ int vsnprintf(char *dst, int sz, const char *fmt, va_list args) param.data = &snparam; /* execute the printf() */ - if (do_print(fmt, ¶m, args) < 0) + if (do_print(fmt, ¶m, args) < 0) { return -1; + } /* ensure the written string is NULL terminated */ - if (snparam.wrtn < sz) + if (snparam.wrtn < sz) { snparam.dst[snparam.wrtn] = '\0'; - else + } + else { snparam.dst[sz - 1] = '\0'; + } /* return the number of chars which would be written */ res = snparam.wrtn; diff --git a/hypervisor/lib/string.c b/hypervisor/lib/string.c index 93fa4adc3..af81af037 100644 --- a/hypervisor/lib/string.c +++ b/hypervisor/lib/string.c @@ -61,14 +61,18 @@ long strtol_deci(const char *nptr) acc = 0; any = 0; do { - if (c >= '0' && c <= '9') + if (c >= '0' && c <= '9') { c -= '0'; - else + } + else { break; - if (c >= base) + } + if (c >= base) { break; - if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) + } + if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) { any = -1; + } else { any = 1; acc *= base; @@ -79,10 +83,12 @@ long strtol_deci(const char *nptr) s++; } while (true); - if (any < 0) + if (any < 0) { acc = (neg != 0) ? LONG_MIN : LONG_MAX; - else if (neg != 0) + } + else if (neg != 0) { acc = -acc; + } return acc; } @@ -115,18 +121,24 @@ uint64_t strtoul_hex(const char *nptr) acc = 0; any = 0; do { - if (c >= '0' && c <= '9') + if (c >= '0' && c <= '9') { c -= '0'; - else if (c >= 'A' && c <= 'F') + } + else if (c >= 'A' && c <= 'F') { c -= 'A' - 10; - else if (c >= 'a' && c <= 'f') + } + else if (c >= 'a' && c <= 'f') { c -= 'a' - 10; - else + } + else { break; - if (c >= base) + } + if (c >= base) { break; - if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) + } + if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) { any = -1; + } else { any = 1; acc *= base; @@ -137,9 +149,9 @@ uint64_t strtoul_hex(const char *nptr) s++; } while (true); - if (any <= 0) + if (any <= 0) { acc = ULONG_MAX; - + } return acc; } @@ -191,8 +203,9 @@ char *strcpy_s(char *d, size_t dmax, const char *s) return NULL; } - if (s == d) + if (s == d) { return d; + } overlap_guard = (uint64_t)((d > s) ? (d - s - 1) : (s - d - 1)); @@ -269,8 +282,9 @@ char *strncpy_s(char *d, size_t dmax, const char *s, size_t slen) return NULL; } - if (d == s) + if (d == s) { return d; + } overlap_guard = (uint64_t)((d > s) ? (d - s - 1) : (s - d - 1)); @@ -290,8 +304,9 @@ char *strncpy_s(char *d, size_t dmax, const char *s, size_t slen) } *d = *s; - if (*d == '\0') + if (*d == '\0') { return dest_base; + } d++; s++; @@ -337,8 +352,9 @@ size_t strnlen_s(const char *str, size_t maxlen) count = 0U; while ((*str) != 0) { - if (maxlen == 0U) + if (maxlen == 0U) { break; + } count++; maxlen--;