mirror of
https://github.com/openappsec/openappsec.git
synced 2025-09-29 19:24:26 +03:00
Mar 26th 2023 Dev
This commit is contained in:
46
core/shm_pkt_queue/shared_string_wrapper.h
Normal file
46
core/shm_pkt_queue/shared_string_wrapper.h
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (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.
|
||||
|
||||
#ifndef __SHARED_STRING_WRAPPER_H__
|
||||
#define __SHARED_STRING_WRAPPER_H__
|
||||
|
||||
#include <boost/lockfree/spsc_queue.hpp>
|
||||
#include <boost/interprocess/managed_shared_memory.hpp>
|
||||
#include <boost/interprocess/allocators/allocator.hpp>
|
||||
#include <boost/interprocess/containers/string.hpp>
|
||||
|
||||
using char_alloc = boost::interprocess::allocator<u_char, boost::interprocess::managed_shared_memory::segment_manager>;
|
||||
using shared_string = boost::interprocess::basic_string<u_char, std::char_traits<u_char>, char_alloc>;
|
||||
|
||||
class SharedStringWrapper
|
||||
{
|
||||
public:
|
||||
static void setAlloc(boost::interprocess::managed_shared_memory::segment_manager *_alloc) { alloc = _alloc; }
|
||||
|
||||
SharedStringWrapper() : str(alloc) {}
|
||||
|
||||
void reserve(size_t size) { str.reserve(size); }
|
||||
void append(const u_char *data, size_t len) { str.append(data, len); }
|
||||
size_t size() const { return str.size(); }
|
||||
shared_string::iterator begin() { return str.begin(); }
|
||||
shared_string::iterator end() { return str.end(); }
|
||||
u_char * data() { return str.data(); }
|
||||
|
||||
private:
|
||||
static boost::interprocess::managed_shared_memory::segment_manager *alloc;
|
||||
shared_string str;
|
||||
};
|
||||
|
||||
using ring_buffer = boost::lockfree::spsc_queue<SharedStringWrapper, boost::lockfree::capacity<200>>;
|
||||
|
||||
#endif // __SHARED_STRING_WRAPPER_H__
|
@@ -16,42 +16,16 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <boost/lockfree/spsc_queue.hpp>
|
||||
#include <boost/interprocess/managed_shared_memory.hpp>
|
||||
#include <boost/interprocess/allocators/allocator.hpp>
|
||||
#include <boost/interprocess/containers/string.hpp>
|
||||
#include <sys/time.h>
|
||||
#include "common.h"
|
||||
|
||||
static const int queue_size = 200;
|
||||
#include "shared_string_wrapper.h"
|
||||
|
||||
const int shm_pkt_queue_bad_alloc = -2;
|
||||
namespace bip = boost::interprocess;
|
||||
|
||||
using char_alloc = bip::allocator<u_char, bip::managed_shared_memory::segment_manager>;
|
||||
using shared_string = bip::basic_string<u_char, std::char_traits<u_char>, char_alloc>;
|
||||
|
||||
class SharedStringWrapper
|
||||
{
|
||||
public:
|
||||
static void setAlloc(bip::managed_shared_memory::segment_manager *_alloc) { alloc = _alloc; }
|
||||
|
||||
SharedStringWrapper() : str(alloc) {}
|
||||
|
||||
void reserve(size_t size) { str.reserve(size); }
|
||||
void append(const u_char *data, size_t len) { str.append(data, len); }
|
||||
size_t size() const { return str.size(); }
|
||||
shared_string::iterator begin() { return str.begin(); }
|
||||
shared_string::iterator end() { return str.end(); }
|
||||
|
||||
private:
|
||||
static bip::managed_shared_memory::segment_manager *alloc;
|
||||
shared_string str;
|
||||
};
|
||||
|
||||
bip::managed_shared_memory::segment_manager *SharedStringWrapper::alloc = nullptr;
|
||||
|
||||
using ring_buffer = boost::lockfree::spsc_queue<SharedStringWrapper, boost::lockfree::capacity<queue_size>>;
|
||||
|
||||
class Impl
|
||||
{
|
||||
public:
|
||||
|
@@ -1,22 +1,25 @@
|
||||
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (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 "shmpktqueue.h"
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
#include <boost/lockfree/spsc_queue.hpp>
|
||||
#include <boost/interprocess/managed_shared_memory.hpp>
|
||||
#include <boost/interprocess/allocators/allocator.hpp>
|
||||
#include <boost/interprocess/containers/string.hpp>
|
||||
|
||||
#include "cptest.h"
|
||||
#include "maybe_res.h"
|
||||
#include "../shared_string_wrapper.h"
|
||||
|
||||
namespace bip = boost::interprocess;
|
||||
|
||||
static const int queue_size = 200;
|
||||
using char_alloc = bip::allocator<u_char, bip::managed_shared_memory::segment_manager>;
|
||||
using shared_string = bip::basic_string<u_char, std::char_traits<u_char>, char_alloc>;
|
||||
using ring_buffer = boost::lockfree::spsc_queue<shared_string, boost::lockfree::capacity<queue_size>>;
|
||||
|
||||
using namespace std;
|
||||
|
||||
static const int segment_name_len = 128;
|
||||
@@ -103,8 +106,7 @@ class ShmPktQueueTest : public ::testing::Test {
|
||||
pop_packet_via_boost()
|
||||
{
|
||||
ring_buffer *queue = segment->find_or_construct<ring_buffer>(queue_name)();
|
||||
char_alloc char_alloc(segment->get_segment_manager());
|
||||
shared_string node_content(char_alloc);
|
||||
SharedStringWrapper node_content;
|
||||
PacketInfo packet_pop_by_boost;
|
||||
|
||||
if (queue->pop(node_content)) {
|
||||
|
Reference in New Issue
Block a user