1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 13:37:44 +00:00

AK+Kernel+LibELF: Remove the need for IteratorDecision::Continue

By constraining two implementations, the compiler will select the best
fitting one. All this will require is duplicating the implementation and
simplifying for the `void` case.

This constraining also informs both the caller and compiler by passing
the callback parameter types as part of the constraint
(e.g.: `IterationFunction<int>`).

Some `for_each` functions in LibELF only take functions which return
`void`. This is a minimal correctness check, as it removes one way for a
function to incompletely do something.

There seems to be a possible idiom where inside a lambda, a `return;` is
the same as `continue;` in a for-loop.
This commit is contained in:
Nicholas Baron 2021-05-16 02:36:52 -07:00 committed by GitHub
parent bbaa463032
commit aa4d41fe2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 311 additions and 127 deletions

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/Concepts.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <Kernel/VirtualAddress.h>
@ -134,7 +135,8 @@ public:
}
unsigned relocation_count() const { return entry_count(); }
Relocation relocation(unsigned index) const;
template<typename F>
template<VoidFunction<Image::Relocation&> F>
void for_each_relocation(F) const;
};
@ -167,13 +169,24 @@ public:
ProgramHeader program_header(unsigned) const;
FlatPtr program_header_table_offset() const;
template<typename F>
template<IteratorFunction<Image::Section> F>
void for_each_section(F) const;
template<typename F>
template<VoidFunction<Section> F>
void for_each_section(F) const;
template<IteratorFunction<Section&> F>
void for_each_section_of_type(unsigned, F) const;
template<typename F>
template<VoidFunction<Section&> F>
void for_each_section_of_type(unsigned, F) const;
template<IteratorFunction<Symbol> F>
void for_each_symbol(F) const;
template<typename F>
template<VoidFunction<Symbol> F>
void for_each_symbol(F) const;
template<IteratorFunction<ProgramHeader> F>
void for_each_program_header(F func) const;
template<VoidFunction<ProgramHeader> F>
void for_each_program_header(F) const;
Optional<Section> lookup_section(String const& name) const;
@ -222,15 +235,26 @@ private:
mutable Vector<SortedSymbol> m_sorted_symbols;
};
template<typename F>
template<IteratorFunction<Image::Section> F>
inline void Image::for_each_section(F func) const
{
auto section_count = this->section_count();
for (unsigned i = 0; i < section_count; ++i)
func(section(i));
for (unsigned i = 0; i < section_count; ++i) {
if (func(section(i)) == IterationDecision::Break)
break;
}
}
template<typename F>
template<VoidFunction<Image::Section> F>
inline void Image::for_each_section(F func) const
{
for_each_section([&](auto section) {
func(move(section));
return IterationDecision::Continue;
});
}
template<IteratorFunction<Image::Section&> F>
inline void Image::for_each_section_of_type(unsigned type, F func) const
{
auto section_count = this->section_count();
@ -243,17 +267,25 @@ inline void Image::for_each_section_of_type(unsigned type, F func) const
}
}
template<typename F>
template<VoidFunction<Image::Section&> F>
inline void Image::for_each_section_of_type(unsigned type, F func) const
{
for_each_section_of_type(type, [&](auto& section) {
func(section);
return IterationDecision::Continue;
});
}
template<VoidFunction<Image::Relocation&> F>
inline void Image::RelocationSection::for_each_relocation(F func) const
{
auto relocation_count = this->relocation_count();
for (unsigned i = 0; i < relocation_count; ++i) {
if (func(relocation(i)) == IterationDecision::Break)
break;
func(relocation(i));
}
}
template<typename F>
template<IteratorFunction<Image::Symbol> F>
inline void Image::for_each_symbol(F func) const
{
auto symbol_count = this->symbol_count();
@ -263,14 +295,32 @@ inline void Image::for_each_symbol(F func) const
}
}
template<typename F>
template<VoidFunction<Image::Symbol> F>
inline void Image::for_each_symbol(F func) const
{
for_each_symbol([&](auto symbol) {
func(move(symbol));
return IterationDecision::Continue;
});
}
template<IteratorFunction<Image::ProgramHeader> F>
inline void Image::for_each_program_header(F func) const
{
auto program_header_count = this->program_header_count();
for (unsigned i = 0; i < program_header_count; ++i) {
if (func(program_header(i)) == IterationDecision::Break)
return;
break;
}
}
template<VoidFunction<Image::ProgramHeader> F>
inline void Image::for_each_program_header(F func) const
{
for_each_program_header([&](auto header) {
func(move(header));
return IterationDecision::Continue;
});
}
} // end namespace ELF