Skip to content
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
4 changes: 4 additions & 0 deletions lib/swoc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,7 @@ set_target_properties(
install(TARGETS libswoc PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/swoc)

add_library(libswoc::libswoc ALIAS libswoc)

if(BUILD_TESTING)
add_subdirectory(unit_tests)
endif()
9 changes: 9 additions & 0 deletions lib/swoc/include/swoc/IntrusiveHashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,15 @@ IntrusiveHashMap<H>::insert(value_type *v) {
if (spot != bucket->_v) {
mixed_p = true; // found some other key, it's going to be mixed.
}
if (spot != limit) {
// If an equal key was found, walk past those to insert at the upper end of the range.
do {
spot = H::next_ptr(spot);
} while (spot != limit && H::equal(key, H::key_of(spot)));
if (spot != limit) { // something not equal past last equivalent, it's going to be mixed.
mixed_p = true;
}
}
Comment on lines +517 to +525
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This puts back what we reverted because origin side session pooling was broken by it. See #10992 where we pulled this out.

To address the functional concerns, see my patch below in HttpSessionManager.cc


_list.insert_before(spot, v);
if (spot == bucket->_v) { // added before the bucket start, update the start.
Expand Down
2 changes: 1 addition & 1 deletion lib/swoc/include/swoc/TextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,7 @@ TextView::suffix(int n) const noexcept {
inline TextView
TextView::suffix_at(char c) const {
self_type zret;
if (auto n = this->rfind(c); n != npos) {
if (auto n = this->rfind(c); n != npos && n + 1 < this->size()) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This addresses a compiler warning from Centos that zret.assign referenced the byte after data() if n pointed to the last byte in the string.

++n;
zret.assign(this->data() + n, this->size() - n);
}
Expand Down
51 changes: 51 additions & 0 deletions lib/swoc/unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
cmake_minimum_required(VERSION 3.12)
project(test_libswoc CXX)
set(CMAKE_CXX_STANDARD 17)

add_executable(
test_libswoc
unit_test_main.cc
test_BufferWriter.cc
test_bw_format.cc
test_Errata.cc
test_IntrusiveDList.cc
test_IntrusiveHashMap.cc
test_ip.cc
test_Lexicon.cc
test_MemSpan.cc
test_MemArena.cc
test_meta.cc
test_range.cc
test_TextView.cc
test_Scalar.cc
test_swoc_file.cc
test_Vectray.cc
ex_bw_format.cc
ex_IntrusiveDList.cc
ex_ipspace_properties.cc
ex_Lexicon.cc
ex_MemArena.cc
ex_TextView.cc
ex_UnitParser.cc
)

target_link_libraries(test_libswoc PUBLIC libswoc PRIVATE catch2::catch2)
set_target_properties(test_libswoc PROPERTIES CLANG_FORMAT_DIRS ${CMAKE_CURRENT_SOURCE_DIR})
if(CMAKE_COMPILER_IS_GNUCXX)
target_compile_options(
test_libswoc
PRIVATE -Wall
-Wextra
-Werror
-Wno-unused-parameter
-Wno-format-truncation
-Wno-stringop-overflow
-Wno-invalid-offsetof
)
# stop the compiler from complaining about unused variable in structured binding
if(GCC_VERSION VERSION_LESS 8.0)
target_compile_options(test_libswoc PRIVATE -Wno-unused-variable)
endif()
endif()

add_test(NAME test_libswoc COMMAND test_libswoc)
215 changes: 215 additions & 0 deletions lib/swoc/unit_tests/ex_IntrusiveDList.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/** @file

IntrusiveDList documentation examples.

This code is run during unit tests to verify that it compiles and runs correctly, but the primary
purpose of the code is for documentation, not testing per se. This means editing the file is
almost certain to require updating documentation references to code in this file.

@section license License

Licensed to the Apache Software Foundation (ASF) under one or more contributor license
agreements. See the NOTICE file distributed with this work for additional information regarding
copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with the License. You may
obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the specific language governing permissions and
limitations under the License.
*/

#include <iostream>
#include <string_view>
#include <string>
#include <algorithm>

#include "swoc/IntrusiveDList.h"
#include "swoc/bwf_base.h"

#include "catch.hpp"

using swoc::IntrusiveDList;

class Message {
using self_type = Message; ///< Self reference type.

public:
// Message severity level.
enum Severity { LVL_DEBUG, LVL_INFO, LVL_WARN, LVL_ERROR };

protected:
std::string _text; // Text of the message.
Severity _severity{LVL_DEBUG};
int _indent{0}; // indentation level for display.

// Intrusive list support.
struct Linkage {
static self_type *&next_ptr(self_type *); // Link accessor.
static self_type *&prev_ptr(self_type *); // Link accessor.

self_type *_next{nullptr}; // Forward link.
self_type *_prev{nullptr}; // Backward link.
} _link;

bool is_in_list() const;

friend class Container;
};

auto
Message::Linkage::next_ptr(self_type *that) -> self_type *& {
return that->_link._next;
}
auto
Message::Linkage::prev_ptr(self_type *that) -> self_type *& {
return that->_link._prev;
}

bool
Message::is_in_list() const {
return _link._next || _link._prev;
}

class Container {
using self_type = Container;
using MessageList = IntrusiveDList<Message::Linkage>;

public:
~Container();

template <typename... Args> self_type &debug(std::string_view fmt, Args &&...args);

size_t count() const;
self_type &clear();
Message::Severity max_severity() const;
void print() const;

protected:
MessageList _msgs;
};

Container::~Container() {
this->clear(); // clean up memory.
}

auto
Container::clear() -> self_type & {
Message *msg;
while (nullptr != (msg = _msgs.take_head())) {
delete msg;
}
_msgs.clear();
return *this;
}

size_t
Container::count() const {
return _msgs.count();
}

template <typename... Args>
auto
Container::debug(std::string_view fmt, Args &&...args) -> self_type & {
Message *msg = new Message;
swoc::bwprint_v(msg->_text, fmt, std::forward_as_tuple(args...));
msg->_severity = Message::LVL_DEBUG;
_msgs.append(msg);
return *this;
}

Message::Severity
Container::max_severity() const {
auto spot = std::max_element(_msgs.begin(), _msgs.end(),
[](Message const &lhs, Message const &rhs) { return lhs._severity < rhs._severity; });
return spot == _msgs.end() ? Message::Severity::LVL_DEBUG : spot->_severity;
}

void
Container::print() const {
for (auto &&elt : _msgs) {
std::cout << static_cast<unsigned int>(elt._severity) << ": " << elt._text << std::endl;
}
}

TEST_CASE("IntrusiveDList Example", "[libswoc][IntrusiveDList]") {
Container container;

container.debug("This is message {}", 1);
REQUIRE(container.count() == 1);
// Destructor is checked for non-crashing as container goes out of scope.
}

struct Thing {
std::string _payload;
Thing *_next{nullptr};
Thing *_prev{nullptr};
using Linkage = swoc::IntrusiveLinkage<Thing, &Thing::_next, &Thing::_prev>;

Thing(std::string_view text) : _payload(text) {}
};

// Just for you, @maskit ! Demonstrating non-public links and subclassing.
class PrivateThing : protected Thing {
using self_type = PrivateThing;
using super_type = Thing;

public:
PrivateThing(std::string_view text) : super_type(text) {}

struct Linkage {
static self_type *&
next_ptr(self_type *t) {
return swoc::ptr_ref_cast<self_type>(t->_next);
}
static self_type *&
prev_ptr(self_type *t) {
return swoc::ptr_ref_cast<self_type>(t->_prev);
}
};

std::string const &
payload() const {
return _payload;
}
};

class PrivateThing2 : protected Thing {
using self_type = PrivateThing2;
using super_type = Thing;

public:
PrivateThing2(std::string_view text) : super_type(text) {}
using Linkage = swoc::IntrusiveLinkageRebind<self_type, super_type::Linkage>;
friend Linkage;

std::string const &
payload() const {
return _payload;
}
};

TEST_CASE("IntrusiveDList Inheritance", "[libswoc][IntrusiveDList][example]") {
IntrusiveDList<PrivateThing::Linkage> priv_list;
for (size_t i = 1; i <= 23; ++i) {
swoc::LocalBufferWriter<16> w;
w.print("Item {}", i);
priv_list.append(new PrivateThing(w.view()));
REQUIRE(priv_list.count() == i);
}
REQUIRE(priv_list.head()->payload() == "Item 1");
REQUIRE(priv_list.tail()->payload() == "Item 23");

IntrusiveDList<PrivateThing2::Linkage> priv2_list;
for (size_t i = 1; i <= 23; ++i) {
swoc::LocalBufferWriter<16> w;
w.print("Item {}", i);
priv2_list.append(new PrivateThing2(w.view()));
REQUIRE(priv2_list.count() == i);
}
REQUIRE(priv2_list.head()->payload() == "Item 1");
REQUIRE(priv2_list.tail()->payload() == "Item 23");
}
Loading