Skip to content

Commit

Permalink
Clean up redundant declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-kirienko committed Oct 3, 2021
1 parent 85a31fb commit 5677168
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
15 changes: 4 additions & 11 deletions o1heap/o1heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,12 @@ static_assert(INSTANCE_SIZE_PADDED >= sizeof(O1HeapInstance), "Invalid instance
static_assert((INSTANCE_SIZE_PADDED % O1HEAP_ALIGNMENT) == 0U, "Invalid instance footprint computation");

/// True if the argument is an integer power of two or zero.
O1HEAP_PRIVATE bool isPowerOf2(const size_t x);
O1HEAP_PRIVATE bool isPowerOf2(const size_t x)
{
return (x & (x - 1U)) == 0U;
}

/// Special case: if the argument is zero, returns zero.
O1HEAP_PRIVATE uint8_t log2Floor(const size_t x);
O1HEAP_PRIVATE uint8_t log2Floor(const size_t x)
{
size_t tmp = x;
Expand All @@ -135,7 +133,6 @@ O1HEAP_PRIVATE uint8_t log2Floor(const size_t x)
}

/// Special case: if the argument is zero, returns zero.
O1HEAP_PRIVATE uint8_t log2Ceil(const size_t x);
O1HEAP_PRIVATE uint8_t log2Ceil(const size_t x)
{
return (uint8_t) (log2Floor(x) + (isPowerOf2(x) ? 0U : 1U));
Expand All @@ -144,14 +141,12 @@ O1HEAP_PRIVATE uint8_t log2Ceil(const size_t x)
/// Raise 2 into the specified power.
/// You might be tempted to do something like (1U << power). WRONG! We humans are prone to forgetting things.
/// If you forget to cast your 1U to size_t or ULL, you may end up with undefined behavior.
O1HEAP_PRIVATE size_t pow2(const uint8_t power);
O1HEAP_PRIVATE size_t pow2(const uint8_t power)
{
return ((size_t) 1U) << power;
}

/// Links two fragments so that their next/prev pointers point to each other; left goes before right.
O1HEAP_PRIVATE void interlink(Fragment* const left, Fragment* const right);
O1HEAP_PRIVATE void interlink(Fragment* const left, Fragment* const right)
{
if (O1HEAP_LIKELY(left != NULL))
Expand All @@ -164,8 +159,7 @@ O1HEAP_PRIVATE void interlink(Fragment* const left, Fragment* const right)
}
}

/// Adds a new block into the appropriate bin and updates the lookup mask.
O1HEAP_PRIVATE void rebin(O1HeapInstance* const handle, Fragment* const fragment);
/// Adds a new fragment into the appropriate bin and updates the lookup mask.
O1HEAP_PRIVATE void rebin(O1HeapInstance* const handle, Fragment* const fragment)
{
O1HEAP_ASSERT(handle != NULL);
Expand All @@ -175,7 +169,7 @@ O1HEAP_PRIVATE void rebin(O1HeapInstance* const handle, Fragment* const fragment
const uint8_t idx = log2Floor(fragment->header.size / FRAGMENT_SIZE_MIN); // Round DOWN when inserting.
O1HEAP_ASSERT(idx < NUM_BINS_MAX);
// Add the new fragment to the beginning of the bin list.
// I.e., each allocation will be returning the least-recently-used fragment -- good for caching.
// I.e., each allocation will be returning the most-recently-used fragment -- good for caching.
fragment->next_free = handle->bins[idx];
fragment->prev_free = NULL;
if (O1HEAP_LIKELY(handle->bins[idx] != NULL))
Expand All @@ -186,8 +180,7 @@ O1HEAP_PRIVATE void rebin(O1HeapInstance* const handle, Fragment* const fragment
handle->nonempty_bin_mask |= pow2(idx);
}

/// Removes the specified block from its bin.
O1HEAP_PRIVATE void unbin(O1HeapInstance* const handle, const Fragment* const fragment);
/// Removes the specified fragment from its bin.
O1HEAP_PRIVATE void unbin(O1HeapInstance* const handle, const Fragment* const fragment)
{
O1HEAP_ASSERT(handle != NULL);
Expand Down Expand Up @@ -439,7 +432,7 @@ bool o1heapDoInvariantsHold(const O1HeapInstance* const handle)
valid = valid && (mask_bit_set == bin_nonempty);
}

// Create a local copy of the diagnostics struct to check later and release the critical section early.
// Create a local copy of the diagnostics struct.
const O1HeapDiagnostics diag = handle->diagnostics;

// Capacity check.
Expand Down
12 changes: 6 additions & 6 deletions o1heap/o1heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extern "C" {
#endif

/// The semantic version number of this distribution.
#define O1HEAP_VERSION_MAJOR 1
#define O1HEAP_VERSION_MAJOR 2

/// The guaranteed alignment depends on the platform pointer width.
#define O1HEAP_ALIGNMENT (sizeof(void*) * 4U)
Expand All @@ -40,12 +40,12 @@ typedef struct O1HeapInstance O1HeapInstance;
/// as required by certain safety-critical development guidelines.
/// If assertion checks are not disabled, the library will perform automatic runtime self-diagnostics that trigger
/// an assertion failure if a heap corruption is detected.
/// Health checks and validation can be done with @ref o1heapDoInvariantsHold().
/// Health checks and validation can be done with o1heapDoInvariantsHold().
typedef struct
{
/// The total amount of memory available for serving allocation requests (heap size).
/// The maximum allocation size is (capacity - O1HEAP_ALIGNMENT).
/// This parameter does not include the overhead used up by @ref O1HeapInstance and arena alignment.
/// This parameter does not include the overhead used up by O1HeapInstance and arena alignment.
/// This parameter is constant.
size_t capacity;

Expand All @@ -66,7 +66,7 @@ typedef struct
uint64_t oom_count;
} O1HeapDiagnostics;

/// The arena base pointer shall be aligned at @ref O1HEAP_ALIGNMENT, otherwise NULL is returned.
/// The arena base pointer shall be aligned at O1HEAP_ALIGNMENT, otherwise NULL is returned.
///
/// The total heap capacity cannot exceed approx. (SIZE_MAX/2). If the arena size allows for a larger heap,
/// the excess will be silently truncated away (no error). This is not a realistic use case because a typical
Expand All @@ -87,7 +87,7 @@ O1HeapInstance* o1heapInit(void* const base, const size_t size);
/// The semantics follows malloc() with additional guarantees the full list of which is provided below.
///
/// If the allocation request is served successfully, a pointer to the newly allocated memory fragment is returned.
/// The returned pointer is guaranteed to be aligned at @ref O1HEAP_ALIGNMENT.
/// The returned pointer is guaranteed to be aligned at O1HEAP_ALIGNMENT.
///
/// If the allocation request cannot be served due to the lack of memory or its excessive fragmentation,
/// a NULL pointer is returned.
Expand All @@ -111,7 +111,7 @@ void o1heapFree(O1HeapInstance* const handle, void* const pointer);
/// The return value is truth if the heap looks valid, falsity otherwise.
bool o1heapDoInvariantsHold(const O1HeapInstance* const handle);

/// Samples and returns a copy of the diagnostic information, see @ref O1HeapDiagnostics.
/// Samples and returns a copy of the diagnostic information, see O1HeapDiagnostics.
/// This function merely copies the structure from an internal storage, so it is fast to return.
/// If the handle pointer is NULL, the behavior is undefined.
O1HeapDiagnostics o1heapGetDiagnostics(const O1HeapInstance* const handle);
Expand Down
29 changes: 17 additions & 12 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if (NOT NO_STATIC_ANALYSIS)
message(FATAL_ERROR "Could not locate clang-tidy")
endif ()
message(STATUS "Using clang-tidy: ${clang_tidy}")
set(CMAKE_C_CLANG_TIDY ${clang_tidy})
set(CMAKE_C_CLANG_TIDY ${clang_tidy})
set(CMAKE_CXX_CLANG_TIDY ${clang_tidy})

# clang-format
Expand All @@ -43,9 +43,9 @@ if (NOT NO_STATIC_ANALYSIS)
message(FATAL_ERROR "Could not locate clang-format")
endif ()
file(
GLOB format_files
${library_dir}/*.[ch]
${CMAKE_SOURCE_DIR}/*.[ch]pp
GLOB format_files
${library_dir}/*.[ch]
${CMAKE_SOURCE_DIR}/*.[ch]pp
)
message(STATUS "Using clang-format: ${clang_format}; files: ${format_files}")
add_custom_target(format COMMAND ${clang_format} -i -fallback-style=none -style=file --verbose ${format_files})
Expand Down Expand Up @@ -78,16 +78,21 @@ function(gen_test name files compile_definitions compile_features compile_flags
add_test("run_${name}" "${name}" --rng-seed time)
endfunction()

function(gen_test_matrix name files compile_definitions)
gen_test("${name}_c99_x64" "${files}" "${compile_definitions}" c_std_99 "-m64" "-m64")
gen_test("${name}_c99_x32" "${files}" "${compile_definitions}" c_std_99 "-m32" "-m32")
gen_test("${name}_c11_x64" "${files}" "${compile_definitions}" c_std_11 "-m64" "-m64")
gen_test("${name}_c11_x32" "${files}" "${compile_definitions}" c_std_11 "-m32" "-m32")
function(gen_test_matrix name files compile_definitions compile_flags)
gen_test("${name}_c99_x64" "${files}" "${compile_definitions}" c_std_99 "${compile_flags} -m64" "-m64")
gen_test("${name}_c99_x32" "${files}" "${compile_definitions}" c_std_99 "${compile_flags} -m32" "-m32")
gen_test("${name}_c11_x64" "${files}" "${compile_definitions}" c_std_11 "${compile_flags} -m64" "-m64")
gen_test("${name}_c11_x32" "${files}" "${compile_definitions}" c_std_11 "${compile_flags} -m32" "-m32")
# Coverage is only available for GCC builds.
if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") AND (CMAKE_BUILD_TYPE STREQUAL "Debug"))
gen_test("${name}_cov" "${files}" "${compile_definitions}" c_std_11 "-g -O0 --coverage" "--coverage")
gen_test("${name}_cov"
"${files}"
"${compile_definitions}"
c_std_11
"${compile_flags} -g -O0 --coverage"
"--coverage")
endif ()
endfunction()

gen_test_matrix(test_private test_private.cpp O1HEAP_EXPOSE_INTERNALS=1)
gen_test_matrix(test_general test_general.cpp "")
gen_test_matrix(test_private test_private.cpp O1HEAP_EXPOSE_INTERNALS=1 "-Wno-missing-declarations")
gen_test_matrix(test_general test_general.cpp "" "")
2 changes: 1 addition & 1 deletion tests/internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ struct O1HeapInstance final
}
};

static_assert(O1HEAP_VERSION_MAJOR == 1);
static_assert(O1HEAP_VERSION_MAJOR == 2);

} // namespace internal

Expand Down

0 comments on commit 5677168

Please sign in to comment.