hv: coding style: refine memory.c

1) Variable names shall start with a lower-case letter.
2) Multiplication and division come before addition and subtraction.
Everything else should be in parentheses.

Tracked-On: #861
Signed-off-by: Li, Fei1 <fei1.li@intel.com>
Acked-by: Anthony Xu <anthony.xu@intel.com>
This commit is contained in:
Li, Fei1 2018-12-18 00:15:10 +08:00 committed by wenlingz
parent 5b467269be
commit b5e0efca82

View File

@ -5,35 +5,32 @@
#include <hypervisor.h> #include <hypervisor.h>
/************************************************************************/ /*
/* Memory pool declaration (block size = CONFIG_MALLOC_ALIGN) */ * Memory pool declaration (block size = CONFIG_MALLOC_ALIGN)
/************************************************************************/ */
#define __bss_noinit __attribute__((__section__(".bss_noinit"))) #define __bss_noinit __attribute__((__section__(".bss_noinit")))
static uint8_t __bss_noinit static uint8_t __bss_noinit malloc_heap[CONFIG_HEAP_SIZE] __aligned(CONFIG_MALLOC_ALIGN);
Malloc_Heap[CONFIG_HEAP_SIZE] __aligned(CONFIG_MALLOC_ALIGN);
#define MALLOC_HEAP_BUFF_SIZE CONFIG_MALLOC_ALIGN #define MALLOC_HEAP_BUFF_SIZE CONFIG_MALLOC_ALIGN
#define MALLOC_HEAP_TOTAL_BUFF (CONFIG_HEAP_SIZE/MALLOC_HEAP_BUFF_SIZE) #define MALLOC_HEAP_TOTAL_BUFF (CONFIG_HEAP_SIZE/MALLOC_HEAP_BUFF_SIZE)
#define MALLOC_HEAP_BITMAP_SIZE \ #define MALLOC_HEAP_BITMAP_SIZE INT_DIV_ROUNDUP(MALLOC_HEAP_TOTAL_BUFF, BITMAP_WORD_SIZE)
INT_DIV_ROUNDUP(MALLOC_HEAP_TOTAL_BUFF, BITMAP_WORD_SIZE) static uint32_t malloc_heap_bitmap[MALLOC_HEAP_BITMAP_SIZE];
static uint32_t Malloc_Heap_Bitmap[MALLOC_HEAP_BITMAP_SIZE]; static uint32_t malloc_heap_contiguity_bitmap[MALLOC_HEAP_BITMAP_SIZE];
static uint32_t Malloc_Heap_Contiguity_Bitmap[MALLOC_HEAP_BITMAP_SIZE];
static struct mem_pool Memory_Pool = { static struct mem_pool memory_pool = {
.start_addr = Malloc_Heap, .start_addr = malloc_heap,
.spinlock = {.head = 0U, .tail = 0U}, .spinlock = {.head = 0U, .tail = 0U},
.size = CONFIG_HEAP_SIZE, .size = CONFIG_HEAP_SIZE,
.buff_size = MALLOC_HEAP_BUFF_SIZE, .buff_size = MALLOC_HEAP_BUFF_SIZE,
.total_buffs = MALLOC_HEAP_TOTAL_BUFF, .total_buffs = MALLOC_HEAP_TOTAL_BUFF,
.bmp_size = MALLOC_HEAP_BITMAP_SIZE, .bmp_size = MALLOC_HEAP_BITMAP_SIZE,
.bitmap = Malloc_Heap_Bitmap, .bitmap = malloc_heap_bitmap,
.contiguity_bitmap = Malloc_Heap_Contiguity_Bitmap .contiguity_bitmap = malloc_heap_contiguity_bitmap
}; };
static void *allocate_mem(struct mem_pool *pool, uint32_t num_bytes) static void *allocate_mem(struct mem_pool *pool, uint32_t num_bytes)
{ {
void *memory = NULL; void *memory = NULL;
uint32_t idx; uint32_t idx;
uint16_t bit_idx; uint16_t bit_idx;
@ -54,16 +51,13 @@ static void *allocate_mem(struct mem_pool *pool, uint32_t num_bytes)
/* Find the first occurrence of requested_buffs number of free /* Find the first occurrence of requested_buffs number of free
* buffers. The 0th bit in bitmap represents a free buffer. * buffers. The 0th bit in bitmap represents a free buffer.
*/ */
for (bit_idx = ffz64(pool->bitmap[idx]); for (bit_idx = ffz64(pool->bitmap[idx]); bit_idx < BITMAP_WORD_SIZE; bit_idx++) {
bit_idx < BITMAP_WORD_SIZE; bit_idx++) {
/* Check if selected buffer is free */ /* Check if selected buffer is free */
if ((pool->bitmap[idx] & (1U << bit_idx)) != 0U) { if ((pool->bitmap[idx] & (1U << bit_idx)) != 0U) {
continue; continue;
} }
/* Declare temporary variables to be used locally in /* Declare temporary variables to be used locally in this block */
* this block
*/
uint32_t i; uint32_t i;
uint16_t tmp_bit_idx = bit_idx; uint16_t tmp_bit_idx = bit_idx;
uint32_t tmp_idx = idx; uint32_t tmp_idx = idx;
@ -87,8 +81,7 @@ static void *allocate_mem(struct mem_pool *pool, uint32_t num_bytes)
} }
/* Break if selected buffer is not free */ /* Break if selected buffer is not free */
if ((pool->bitmap[tmp_idx] if ((pool->bitmap[tmp_idx] & (1U << tmp_bit_idx)) != 0U) {
& (1U << tmp_bit_idx)) != 0U) {
break; break;
} }
} }
@ -101,10 +94,7 @@ static void *allocate_mem(struct mem_pool *pool, uint32_t num_bytes)
* selected free contiguous buffer in the * selected free contiguous buffer in the
* memory pool * memory pool
*/ */
memory = (char *)pool->start_addr + memory = pool->start_addr + pool->buff_size * (idx * BITMAP_WORD_SIZE + bit_idx);
(pool->buff_size *
((idx * BITMAP_WORD_SIZE) +
bit_idx));
/* Update allocation bitmaps information for /* Update allocation bitmaps information for
* selected buffers * selected buffers
@ -118,22 +108,20 @@ static void *allocate_mem(struct mem_pool *pool, uint32_t num_bytes)
/* Set contiguity information for this /* Set contiguity information for this
* buffer in contiguity-bitmap * buffer in contiguity-bitmap
*/ */
if (i < (requested_buffs - 1)) { if (i < (requested_buffs - 1U)) {
/* Set contiguity bit to 1 if /* Set contiguity bit to 1 if
* this buffer is not the last * this buffer is not the last
* of selected contiguous * of selected contiguous
* buffers array * buffers array
*/ */
pool->contiguity_bitmap[idx] |= pool->contiguity_bitmap[idx] |= (1U << bit_idx);
(1U << bit_idx);
} else { } else {
/* Set contiguity bit to 0 if /* Set contiguity bit to 0 if
* this buffer is not the last * this buffer is not the last
* of selected contiguous * of selected contiguous
* buffers array * buffers array
*/ */
pool->contiguity_bitmap[idx] &= pool->contiguity_bitmap[idx] &= ~(1U << bit_idx);
~(1U << bit_idx);
} }
/* Check if bit_idx is out-of-range */ /* Check if bit_idx is out-of-range */
@ -173,8 +161,7 @@ static void deallocate_mem(struct mem_pool *pool, const void *ptr)
spinlock_obtain(&pool->spinlock); spinlock_obtain(&pool->spinlock);
/* Map the buffer address to its index. */ /* Map the buffer address to its index. */
buff_idx = ((char *)ptr - (char *)pool->start_addr) / buff_idx = (ptr - pool->start_addr) / pool->buff_size;
pool->buff_size;
/* De-allocate all allocated contiguous memory buffers */ /* De-allocate all allocated contiguous memory buffers */
while (buff_idx < pool->total_buffs) { while (buff_idx < pool->total_buffs) {
@ -222,7 +209,7 @@ void *malloc(uint32_t num_bytes)
/* /*
* Request memory allocation from smaller segmented memory pool * Request memory allocation from smaller segmented memory pool
*/ */
memory = allocate_mem(&Memory_Pool, num_bytes); memory = allocate_mem(&memory_pool, num_bytes);
} }
/* Check if memory allocation is successful */ /* Check if memory allocation is successful */
@ -251,11 +238,10 @@ void *calloc(uint32_t num_elements, uint32_t element_size)
void free(const void *ptr) void free(const void *ptr)
{ {
/* Check if ptr belongs to 16-Bytes aligned Memory Pool */ /* Check if ptr belongs to 16-Bytes aligned Memory Pool */
if ((Memory_Pool.start_addr < ptr) && if ((memory_pool.start_addr < ptr) &&
(ptr < (Memory_Pool.start_addr + (ptr < (memory_pool.start_addr + memory_pool.total_buffs * memory_pool.buff_size))) {
(Memory_Pool.total_buffs * Memory_Pool.buff_size)))) {
/* Free buffer in 16-Bytes aligned Memory Pool */ /* Free buffer in 16-Bytes aligned Memory Pool */
deallocate_mem(&Memory_Pool, ptr); deallocate_mem(&memory_pool, ptr);
} }
} }