Skip to content

Commit bef8ad4

Browse files
Address clang-tidy feedback on readability and brace use
1 parent 8d6776e commit bef8ad4

7 files changed

+49
-45
lines changed

Diff for: src/aligned_malloc.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ void* aligned_malloc(size_t align, size_t size)
5252
* We also allocate extra bytes to ensure we can meet the alignment
5353
*/
5454
size_t hdr_size = PTR_OFFSET_SZ + (align - 1);
55-
void* p = malloc(size + hdr_size);
55+
void* base_ptr = malloc(size + hdr_size);
5656

57-
if(p)
57+
if(base_ptr)
5858
{
5959
/*
6060
* Add the offset size to malloc's pointer (we will always store that)
6161
* Then align the resulting value to the target alignment
6262
*/
63-
ptr = (void*)align_up(((uintptr_t)p + PTR_OFFSET_SZ), align);
63+
ptr = (void*)align_up(((uintptr_t)base_ptr + PTR_OFFSET_SZ), align);
6464

6565
// Calculate the offset and store it behind our aligned pointer
66-
*((offset_t*)ptr - 1) = (offset_t)((uintptr_t)ptr - (uintptr_t)p);
66+
*((offset_t*)ptr - 1) = (offset_t)((uintptr_t)ptr - (uintptr_t)base_ptr);
6767

6868
} // else NULL, could not malloc
6969
} // else NULL, invalid arguments
@@ -97,6 +97,6 @@ void aligned_free(void* ptr)
9797
/*
9898
* Once we have the offset, we can get our original pointer and call free
9999
*/
100-
void* p = (void*)((uint8_t*)ptr - offset);
101-
free(p);
100+
void* base_ptr = (void*)((uint8_t*)ptr - offset);
101+
free(base_ptr);
102102
}

Diff for: src/malloc_freertos.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ static volatile bool initialized_ = false;
5151

5252
#pragma mark - Private Functions -
5353

54-
static int cmp_heap(const void* a, const void* b)
54+
static int cmp_heap(const void* region_a, const void* region_b)
5555
{
56-
const HeapRegion_t* ua = a;
57-
const HeapRegion_t* ub = b;
56+
const HeapRegion_t* heapregion_a = region_a;
57+
const HeapRegion_t* heapregion_b = region_b;
5858

59-
return ((ua->pucStartAddress < ub->pucStartAddress)
59+
return ((heapregion_a->pucStartAddress < heapregion_b->pucStartAddress)
6060
? -1
61-
: ((ua->pucStartAddress != ub->pucStartAddress)));
61+
: ((heapregion_a->pucStartAddress != heapregion_b->pucStartAddress)));
6262
}
6363

6464
/**

Diff for: src/malloc_threadx.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ void malloc_addblock(void* addr, size_t size)
4141
* tx_byte_pool_create is ThreadX's API to create a byte pool using a memory block.
4242
* We are essentially just wrapping ThreadX APIs into a simpler form
4343
*/
44-
unsigned r = tx_byte_pool_create(&malloc_pool_, "Heap Memory Pool", addr, size);
45-
assert(r == TX_SUCCESS);
44+
unsigned return_code = tx_byte_pool_create(&malloc_pool_, "Heap Memory Pool", addr, size);
45+
assert(return_code == TX_SUCCESS);
4646

4747
// Signal to any threads waiting on do_malloc that we are done
4848
initialized_ = true;
@@ -65,10 +65,10 @@ void* malloc(size_t size)
6565
if(size > 0)
6666
{
6767
// We simply wrap the threadX call into a standard form
68-
unsigned r = tx_byte_allocate(&malloc_pool_, &ptr, size, TX_WAIT_FOREVER);
68+
unsigned return_code = tx_byte_allocate(&malloc_pool_, &ptr, size, TX_WAIT_FOREVER);
6969

7070
// I add the string to provide a more helpful error output. It's value is always true.
71-
assert(r == TX_SUCCESS && "malloc failed");
71+
assert(return_code == TX_SUCCESS && "malloc failed");
7272
} // else NULL if there was an error
7373

7474
return ptr;
@@ -82,7 +82,7 @@ void free(void* ptr)
8282
if(ptr)
8383
{
8484
// We simply wrap the threadX call into a standard form
85-
unsigned r = tx_byte_release(ptr);
86-
assert(r == TX_SUCCESS);
85+
unsigned return_code = tx_byte_release(ptr);
86+
assert(return_code == TX_SUCCESS);
8787
}
8888
}

Diff for: src/posix_memalign.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@
77

88
int posix_memalign(void** __memptr, size_t __alignment, size_t __size)
99
{
10-
int r = ENOMEM;
10+
int return_code = ENOMEM;
1111

1212
assert(__memptr);
1313
assert(__size > 0);
1414

1515
// TODO: Do we need to check if __alignment is a multiple of sizeof(void *)?
1616
if(!IS_POWER_2(__alignment))
1717
{
18-
r = EINVAL;
18+
return_code = EINVAL;
1919
}
2020
else
2121
{
2222
*__memptr = aligned_malloc(__alignment, __size);
2323

2424
if(*__memptr != NULL)
2525
{
26-
r = 0;
26+
return_code = 0;
2727
}
2828
}
2929

30-
return r;
30+
return return_code;
3131
}

Diff for: test/src/aligned_malloc.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ static void aligned_malloc_test(void** __attribute__((unused)) state)
7171
__attribute__((unused)) static void posix_memalign_test(void** __attribute__((unused)) state)
7272
{
7373
void* ptr = NULL;
74-
int r = posix_memalign(&ptr, 8, 8);
74+
int return_code = posix_memalign(&ptr, 8, 8);
7575
assert_non_null(ptr);
76-
assert_int_equal(r, 0);
76+
assert_int_equal(return_code, 0);
7777

7878
aligned_free(ptr);
7979
ptr = NULL;
8080

81-
r = posix_memalign(&ptr, 3, 8);
81+
return_code = posix_memalign(&ptr, 3, 8);
8282
assert_null(ptr);
83-
assert_int_equal(r, EINVAL);
83+
assert_int_equal(return_code, EINVAL);
8484
}
8585

8686
int aligned_malloc_tests(void)

Diff for: test/src/malloc_freelist.c

+15-11
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#define ALLOCATION_TEST_COUNT 768
2020

21-
static void* p[ALLOCATION_TEST_COUNT];
21+
static void* pointer_array[ALLOCATION_TEST_COUNT];
2222

2323
static void malloc_test(void** __attribute__((unused)) state)
2424
{
@@ -47,34 +47,38 @@ static void malloc_test(void** __attribute__((unused)) state)
4747

4848
for(size_t i = 0; i < ALLOCATION_TEST_COUNT; i++)
4949
{
50-
p[i] = malloc(1024);
51-
assert_non_null(p[i]);
52-
assert_in_range((uintptr_t)p[i], mem_block_addr, mem_block_end_addr);
50+
pointer_array[i] = malloc(1024);
51+
assert_non_null(pointer_array[i]);
52+
assert_in_range((uintptr_t)pointer_array[i], mem_block_addr, mem_block_end_addr);
5353
// Test for unique addresses
5454
if(i > 0)
55-
assert_not_in_set((uintptr_t)p[i], (uintptr_t*)p, i - 1);
55+
{
56+
assert_not_in_set((uintptr_t)pointer_array[i], (uintptr_t*)pointer_array, i - 1);
57+
}
5658
}
5759

5860
// Cleanup
5961
for(size_t i = 0; i < ALLOCATION_TEST_COUNT; i++)
6062
{
61-
free(p[i]);
63+
free(pointer_array[i]);
6264
}
6365

6466
// Run test again, will not fail if our memory has been returned!
6567
for(size_t i = 0; i < ALLOCATION_TEST_COUNT; i++)
6668
{
67-
p[i] = malloc(1024);
68-
assert_non_null(p[i]);
69-
assert_in_range((uintptr_t)p[i], mem_block_addr, mem_block_end_addr);
69+
pointer_array[i] = malloc(1024);
70+
assert_non_null(pointer_array[i]);
71+
assert_in_range((uintptr_t)pointer_array[i], mem_block_addr, mem_block_end_addr);
7072
if(i > 0)
71-
assert_not_in_set((uintptr_t)p[i], (uintptr_t*)p, i - 1);
73+
{
74+
assert_not_in_set((uintptr_t)pointer_array[i], (uintptr_t*)pointer_array, i - 1);
75+
}
7276
}
7377

7478
// Cleanup
7579
for(size_t i = 0; i < ALLOCATION_TEST_COUNT; i++)
7680
{
77-
free(p[i]);
81+
free(pointer_array[i]);
7882
}
7983
}
8084

Diff for: test/src/malloc_freelist_locking.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#define ALLOCATION_TEST_COUNT 768
2020

21-
static void* p[ALLOCATION_TEST_COUNT];
21+
static void* ptr_array[ALLOCATION_TEST_COUNT];
2222

2323
bool malloc_lock_called = false;
2424
bool malloc_unlock_called = false;
@@ -67,29 +67,29 @@ static void malloc_locking_test(void** __attribute__((unused)) state)
6767

6868
for(int i = 0; i < ALLOCATION_TEST_COUNT; i++)
6969
{
70-
p[i] = malloc(1024);
71-
assert_non_null(p[i]);
72-
assert_in_range((uintptr_t)p[i], mem_block_addr, mem_block_end_addr);
70+
ptr_array[i] = malloc(1024);
71+
assert_non_null(ptr_array[i]);
72+
assert_in_range((uintptr_t)ptr_array[i], mem_block_addr, mem_block_end_addr);
7373
}
7474

7575
// Cleanup
7676
for(int i = 0; i < ALLOCATION_TEST_COUNT; i++)
7777
{
78-
free(p[i]);
78+
free(ptr_array[i]);
7979
}
8080

8181
// Run test again, will not fail if our memory has been returned!
8282
for(int i = 0; i < ALLOCATION_TEST_COUNT; i++)
8383
{
84-
p[i] = malloc(1024);
85-
assert_non_null(p[i]);
86-
assert_in_range((uintptr_t)p[i], mem_block_addr, mem_block_end_addr);
84+
ptr_array[i] = malloc(1024);
85+
assert_non_null(ptr_array[i]);
86+
assert_in_range((uintptr_t)ptr_array[i], mem_block_addr, mem_block_end_addr);
8787
}
8888

8989
// Cleanup
9090
for(int i = 0; i < ALLOCATION_TEST_COUNT; i++)
9191
{
92-
free(p[i]);
92+
free(ptr_array[i]);
9393
}
9494

9595
assert_true(malloc_lock_called);

0 commit comments

Comments
 (0)