Skip to content

Memory allocation review #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions include/skyr/v1/containers/static_vector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Copyright 2020 Glyn Matthews.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef SKYR_V1_CONTAINERS_STATIC_VECTOR_HPP
#define SKYR_V1_CONTAINERS_STATIC_VECTOR_HPP

#include <cstdlib>
#include <array>
#include <type_traits>
#include <optional>

namespace skyr {
inline namespace v1 {
///
/// \tparam T
/// \tparam Capacity
template <
class T,
std::size_t Capacity
>
class static_vector {
private:

using impl_type = std::array<T, Capacity>;

impl_type impl_;
std::size_t size_ = 0;

public:

///
using value_type = T;
///
using const_reference = const T &;
///
using reference = T &;
///
using const_pointer = const T *;
///
using pointer = T *;
///
using size_type = std::size_t;
///
using difference_type = std::ptrdiff_t;
///
using const_iterator = typename impl_type::const_iterator;
///
using iterator = typename impl_type::iterator;

/// Constructor
constexpr static_vector() = default;

/// Gets the first const element in the vector
/// \return a const T &
/// \pre `size() > 0`
constexpr auto front() const noexcept -> const_reference {
return impl_[0];
}

/// Gets the first element in the vector
/// \return a T &
/// \pre `size() > 0`
constexpr auto front() noexcept -> reference {
return impl_[0];
}

///
/// \return
/// \pre `size() > 0`
constexpr auto back() const noexcept -> const_reference {
return impl_[size_ - 1];
}

///
/// \return
/// \pre `size() > 0`
constexpr auto back() noexcept -> reference {
return impl_[size_ - 1];
}

///
/// \param value
/// \return
/// \pre `size() < `capacity()`
/// \post `size() > 0 && size() <= capacity()`
constexpr auto push_back(const_reference value) noexcept -> reference {
impl_[size_++] = value;
return impl_[size_ - 1];
}

///
/// \tparam Args
/// \param args
/// \return
/// \pre `size() < `capacity()`
/// \post `size() > 0 && size() <= capacity()`
template <class... Args>
constexpr auto emplace_back(Args &&... args)
noexcept(std::is_trivially_move_assignable_v<T>) -> reference {
impl_[size_++] = value_type{args...};
return impl_[size_ - 1];
}

///
/// \pre `size() > 0`
constexpr void pop_back() noexcept {
--size_;
}

///
/// \return
[[nodiscard]] constexpr auto data() const noexcept -> const value_type * {
return impl_.data();
}

///
/// \return
[[nodiscard]] constexpr auto size() const noexcept -> size_type {
return size_;
}

///
/// \return
[[nodiscard]] constexpr auto max_size() const noexcept -> size_type {
return Capacity;
}

///
/// \return `true` if there are elements
[[nodiscard]] constexpr auto empty() const noexcept -> bool {
return size_ == 0;
}

///
/// \return
[[nodiscard]] constexpr auto begin() noexcept -> iterator {
return impl_.begin();
}

///
/// \return
[[nodiscard]] constexpr auto end() noexcept -> iterator {
auto last = impl_.begin();
std::advance(last, size_);
return last;
}

///
/// \return
[[nodiscard]] constexpr auto begin() const noexcept -> const_iterator {
return impl_.begin();
}

///
/// \return
[[nodiscard]] constexpr auto end() const noexcept -> const_iterator {
auto last = impl_.begin();
std::advance(last, size_);
return last;
}

};
} // namespace v1
} // namespace skyr

#endif // SKYR_V1_CONTAINERS_STATIC_VECTOR_HPP
2 changes: 2 additions & 0 deletions include/skyr/v1/domain/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ enum class domain_errc {
invalid_length,
/// Empty domain
empty_string,
/// The number of labels in the domain is too large
too_many_labels,
};
} // namespace v1
} // namespace skyr
Expand Down
2 changes: 1 addition & 1 deletion include/skyr/v1/unicode/traits/range_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace unicode::traits {
template <class Range>
class range_iterator {
public:
using type = typename Range::const_iterator;
using type = typename std::decay_t<Range>::const_iterator;
};

///
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ target_sources(skyr-url
v1/domain/domain.cpp
v1/domain/idna.hpp
v1/domain/idna.cpp
v1/domain/idna_code_point_map_iterator.hpp
v1/domain/punycode.hpp
v1/domain/punycode.cpp
v1/url/url.cpp
Expand All @@ -50,6 +51,7 @@ target_sources(skyr-url
${PROJECT_SOURCE_DIR}/include/skyr/v1/unicode/ranges/sentinel.hpp
${PROJECT_SOURCE_DIR}/include/skyr/v1/unicode/details/to_u8.hpp
${PROJECT_SOURCE_DIR}/include/skyr/v1/string/starts_with.hpp
${PROJECT_SOURCE_DIR}/include/skyr/v1/containers/static_vector.hpp
${PROJECT_SOURCE_DIR}/include/skyr/v1/domain/errors.hpp
${PROJECT_SOURCE_DIR}/include/skyr/v1/domain/domain.hpp
${PROJECT_SOURCE_DIR}/include/skyr/v1/platform/endianness.hpp
Expand Down
17 changes: 13 additions & 4 deletions src/v1/core/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
#include <skyr/v1/core/errors.hpp>
#include <skyr/v1/percent_encoding/percent_encoded_char.hpp>
#include <skyr/v1/percent_encoding/percent_decode.hpp>
#include <skyr/v1/containers/static_vector.hpp>
#include <skyr/v1/domain/domain.hpp>

#if !defined(SKYR_DOMAIN_MAX_DOMAIN_LENGTH)
#define SKYR_DOMAIN_MAX_DOMAIN_LENGTH 253
#endif // !defined(SKYR_DOMAIN_MAX_DOMAIN_LENGTH)

namespace skyr {
inline namespace v1 {
namespace {
Expand Down Expand Up @@ -73,12 +78,16 @@ auto parse_host(
[] (auto &&h) -> tl::expected<host, url_parse_errc> { return host{h}; });
}

auto domain = percent_decode(input);
if (!domain) {
return tl::make_unexpected(url_parse_errc::cannot_decode_host_point);
auto domain = static_vector<char, SKYR_DOMAIN_MAX_DOMAIN_LENGTH>{};
auto range = percent_encoding::percent_decode_range{input};
for (auto it = std::cbegin(range); it != std::cend(range); ++it) {
if ((domain.size() == domain.max_size()) || !*it) {
return tl::make_unexpected(url_parse_errc::cannot_decode_host_point);
}
domain.push_back((*it).value());
}

auto ascii_domain = domain_to_ascii(domain.value());
auto ascii_domain = domain_to_ascii(std::string_view(domain.data(), domain.size()));
if (!ascii_domain) {
return tl::make_unexpected(url_parse_errc::domain_error);
}
Expand Down
17 changes: 2 additions & 15 deletions src/v1/core/url_parser_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,11 @@ inline auto is_windows_drive_letter(std::string_view segment) noexcept {
}

inline auto is_single_dot_path_segment(std::string_view segment) noexcept {
return
(segment == ".") ||
(segment == "%2e") ||
(segment == "%2E");
return (segment == ".") || (segment == "%2E");
}

auto is_double_dot_path_segment(std::string_view segment) noexcept {
constexpr static auto to_lower = [] (auto byte) -> decltype(byte) {
return std::tolower(byte, std::locale::classic());
};

auto lower = std::string(segment);
std::transform(begin(lower), end(lower), begin(lower), to_lower);
return (
(lower == "..") ||
(lower == ".%2e") ||
(lower == "%2e.") ||
(lower == "%2e%2e"));
return (segment == "..") || (segment == "%2E.") || (segment == ".%2E") || (segment == "%2E%2E");
}

void shorten_path(std::string_view scheme, std::vector<std::string> &path) {
Expand Down
Loading