Add shared data module.
This commit is contained in:
parent
8c9d0d89cb
commit
6c5379565b
|
@ -1,5 +1,6 @@
|
|||
#Compile gtest for test code.
|
||||
add_subdirectory(Config)
|
||||
add_subdirectory(Log)
|
||||
add_subdirectory(SharedData)
|
||||
|
||||
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
|
||||
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
|
||||
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
|
||||
|
||||
include_directories(
|
||||
${UTILS_SOURCE_PATH}/LogC/include
|
||||
${UTILS_SOURCE_PATH}/LogC/src
|
||||
${UTILS_SOURCE_PATH}/ReturnCode/include
|
||||
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
|
||||
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
|
||||
)
|
||||
|
||||
link_directories(
|
||||
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib
|
||||
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib
|
||||
${LIBS_OUTPUT_PATH}
|
||||
)
|
||||
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
aux_source_directory(./ SRC_FILES)
|
||||
aux_source_directory(./src SRC_FILES)
|
||||
|
||||
set(TARGET_NAME LogCTest)
|
||||
add_executable(${TARGET_NAME} ${SRC_FILES})
|
||||
target_link_libraries(${TARGET_NAME} gtest gmock pthread ReturnCode LogCAbstract)
|
||||
# target_link_libraries(${TARGET_NAME} gtest gmock pthread ReturnCode LogCAbstract LogCUb)
|
||||
if(${TEST_COVERAGE} MATCHES "true")
|
||||
target_link_libraries(${TARGET_NAME} gcov)
|
||||
endif()
|
|
@ -1,20 +0,0 @@
|
|||
#include "iLog.h"
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
namespace LogCTest
|
||||
{
|
||||
// ../output_files/test/bin/LogCTest --gtest_filter=LogCTest.Demo
|
||||
TEST(LogCTest, Demo)
|
||||
{
|
||||
create_log_module();
|
||||
i_log_init();
|
||||
|
||||
LogInfo("hello world.");
|
||||
LogError("create ... failed.");
|
||||
LogDebug("a = %d b = %s", 124, "apple");
|
||||
|
||||
i_log_unInit();
|
||||
destroy_log_module();
|
||||
}
|
||||
}
|
54
test/utils/SharedData/CMakeLists.txt
Normal file
54
test/utils/SharedData/CMakeLists.txt
Normal file
|
@ -0,0 +1,54 @@
|
|||
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
|
||||
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
|
||||
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
|
||||
|
||||
include_directories(
|
||||
${UTILS_SOURCE_PATH}/StatusCode/include
|
||||
${UTILS_SOURCE_PATH}/Log/include
|
||||
${UTILS_SOURCE_PATH}/SharedData/include
|
||||
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
|
||||
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
|
||||
)
|
||||
|
||||
link_directories(
|
||||
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib
|
||||
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib
|
||||
${LIBS_OUTPUT_PATH}
|
||||
)
|
||||
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
aux_source_directory(. SRC_FILES)
|
||||
aux_source_directory(./src SRC_FILES)
|
||||
|
||||
set(TARGET_NAME SharedDataTest)
|
||||
add_executable(${TARGET_NAME} ${SRC_FILES})
|
||||
target_link_libraries(${TARGET_NAME} SharedData gtest gmock pthread Log)
|
||||
if(${TEST_COVERAGE} MATCHES "true")
|
||||
target_link_libraries(${TARGET_NAME} gcov)
|
||||
endif()
|
||||
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
|
||||
add_custom_target(
|
||||
SharedDataTest_code_check
|
||||
COMMAND ${CLANG_TIDY_EXE}
|
||||
-checks='${CLANG_TIDY_CHECKS}'
|
||||
--header-filter=.*
|
||||
--system-headers=false
|
||||
${SRC_FILES}
|
||||
${CLANG_TIDY_CONFIG}
|
||||
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
|
||||
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
|
||||
-p ${CMAKE_SOURCE_DIR_IPCSDK}/cmake-shell
|
||||
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/SharedData
|
||||
)
|
||||
add_custom_command(
|
||||
TARGET ${TARGET_NAME}
|
||||
PRE_BUILD
|
||||
COMMAND make SharedDataTest_code_check
|
||||
WORKING_DIRECTORY ${PROJECT_ROOT_PATH}/cmake-shell/
|
||||
)
|
||||
endif()
|
34
test/utils/SharedData/src/SharedDataTest.cpp
Normal file
34
test/utils/SharedData/src/SharedDataTest.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* Licensed 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 "SharedData.h"
|
||||
#include "ILog.h"
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
namespace SharedDataTest
|
||||
{
|
||||
// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.Demo
|
||||
TEST(SharedDataTest, Demo)
|
||||
{
|
||||
CreateLogModule();
|
||||
ILogInit(LOG_INSTANCE_TYPE_END);
|
||||
SharedData *sharedData = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", 9);
|
||||
if (nullptr != sharedData)
|
||||
{
|
||||
sharedData->mMakeSharedData(sharedData, sizeof(int), sizeof(int));
|
||||
sharedData->mFree(sharedData);
|
||||
}
|
||||
ILogUnInit();
|
||||
}
|
||||
} // namespace SharedDataTest
|
4
tools/README.md
Normal file
4
tools/README.md
Normal file
|
@ -0,0 +1,4 @@
|
|||
# 1. 工具
|
||||
|
||||
## 1.1. 概述
|
||||
  该目录存放使用到的工具。
|
|
@ -3,3 +3,5 @@
|
|||
add_subdirectory(Config)
|
||||
add_subdirectory(StatusCode)
|
||||
add_subdirectory(Log)
|
||||
add_subdirectory(SharedData)
|
||||
add_subdirectory(SharedMemory)
|
||||
|
|
44
utils/SharedData/CMakeLists.txt
Normal file
44
utils/SharedData/CMakeLists.txt
Normal file
|
@ -0,0 +1,44 @@
|
|||
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
|
||||
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
|
||||
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
|
||||
|
||||
include_directories(
|
||||
./src
|
||||
./include
|
||||
${UTILS_SOURCE_PATH}/StatusCode/include
|
||||
${UTILS_SOURCE_PATH}/Log/include
|
||||
)
|
||||
# link_directories(
|
||||
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
|
||||
# )
|
||||
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
aux_source_directory(./src SRC_FILES)
|
||||
|
||||
set(TARGET_NAME SharedData)
|
||||
add_library(${TARGET_NAME} STATIC ${SRC_FILES})
|
||||
target_link_libraries(${TARGET_NAME} StatusCode Log)
|
||||
|
||||
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
|
||||
add_custom_target(
|
||||
SharedData_code_check
|
||||
COMMAND ${CLANG_TIDY_EXE}
|
||||
-checks='${CLANG_TIDY_CHECKS}'
|
||||
--header-filter=.*
|
||||
--system-headers=false
|
||||
${SRC_FILES}
|
||||
${CLANG_TIDY_CONFIG}
|
||||
-p ${CMAKE_SOURCE_DIR_IPCSDK}/cmake-shell
|
||||
WORKING_DIRECTORY ${UTILS_SOURCE_PATH}/SharedData
|
||||
)
|
||||
add_custom_command(
|
||||
TARGET ${TARGET_NAME}
|
||||
PRE_BUILD
|
||||
COMMAND make SharedData_code_check
|
||||
WORKING_DIRECTORY ${PROJECT_ROOT_PATH}/cmake-shell/
|
||||
)
|
||||
endif()
|
||||
|
||||
define_file_name(${TARGET_NAME})
|
16
utils/SharedData/README.md
Normal file
16
utils/SharedData/README.md
Normal file
|
@ -0,0 +1,16 @@
|
|||
# 1. Linux跨进程共享数据
|
||||
|
||||
## 1.1. 概述
|
||||
|
||||
  用于跨进程进行数据共享的中间件。
|
||||
|
||||
## 1.2. UML类图
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
i_ipc_config <.. ipc_config:实现
|
||||
ipc_config --> i_config_manager:依赖
|
||||
ipc_config --> IHal:依赖
|
||||
i_config_manager <.. config_manager:实现
|
||||
config_manager --> libconfig开源库:依赖
|
||||
```
|
41
utils/SharedData/include/SharedData.h
Normal file
41
utils/SharedData/include/SharedData.h
Normal file
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* Licensed 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.
|
||||
*/
|
||||
#ifndef SHARED_DATA_H
|
||||
#define SHARED_DATA_H
|
||||
#include "StatusCode.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
enum SHARER_NAME
|
||||
{
|
||||
SHARER_NAME_PRIMARY = 0,
|
||||
SHARER_NAME_MINOR,
|
||||
SHARER_NAME_END
|
||||
};
|
||||
typedef struct shared_data SharedData;
|
||||
typedef struct shared_data
|
||||
{
|
||||
const StatusCode (*mMakeSharedData)(SharedData *, const int, const int);
|
||||
const StatusCode (*mCleanSharedData)(SharedData *);
|
||||
void *(*mGetReadableDataPointer)(SharedData *);
|
||||
void *(*mGetWritableDataPointer)(SharedData *);
|
||||
void (*mFree)(void *);
|
||||
} SharedData;
|
||||
SharedData *CreateSharedData(const SHARER_NAME name, const char *path, const int projectId);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
20
utils/SharedData/src/SharedData.cpp
Normal file
20
utils/SharedData/src/SharedData.cpp
Normal file
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* Licensed 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 "SharedData.h"
|
||||
#include "SharedDataImpl.h"
|
||||
SharedData *CreateSharedData(const SHARER_NAME name, const char *path, const int projectId)
|
||||
{
|
||||
return (SharedData *)NewSharedDataImpl(name, path, projectId);
|
||||
}
|
82
utils/SharedData/src/SharedDataImpl.cpp
Normal file
82
utils/SharedData/src/SharedDataImpl.cpp
Normal file
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* Licensed 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 "SharedDataImpl.h"
|
||||
#include "ILog.h"
|
||||
#include <cstring>
|
||||
static const char *SHARED_DATA_NAME = "shared_data";
|
||||
constexpr short THERE_TWO_USER_DATA_HEADER = 2;
|
||||
SharedDataCpp::SharedDataCpp(const SHARER_NAME &sharerName, const char *path, const int &projectId)
|
||||
: SharedMemory(path, projectId), mSharerName(sharerName)
|
||||
{
|
||||
mReadableSize = 0;
|
||||
mWritableSize = 0;
|
||||
mSharedMemeory = nullptr;
|
||||
}
|
||||
void SharedDataCpp::MakeSharedMemory(const int readableSize, const int writableSize)
|
||||
{
|
||||
mReadableSize = readableSize;
|
||||
mWritableSize = writableSize;
|
||||
const int SHARED_MEMORY_SIZE = readableSize + writableSize + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER;
|
||||
SharedMemory::MakeSharedMemory(SHARED_MEMORY_SIZE);
|
||||
mSharedMemeory = SharedMemory::GetMemory();
|
||||
}
|
||||
void *SharedDataCpp::GetReadableMemory(void)
|
||||
{
|
||||
if (SHARER_NAME_PRIMARY == mSharerName)
|
||||
{
|
||||
return (char *)mSharedMemeory + sizeof(UserDataHeader);
|
||||
}
|
||||
if (SHARER_NAME_MINOR == mSharerName)
|
||||
{
|
||||
return (char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mReadableSize;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
static const StatusCode MakeSharedData(SharedData *object, const int readableSize, const int writableSize)
|
||||
{
|
||||
SharedDataImpl *impl = ((SharedDataImpl *)(((char *)object) - sizeof(SharedDataHeader)));
|
||||
impl->mSharedData->MakeSharedMemory(readableSize, writableSize);
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
static void *GetSharedReadableMemory(SharedData *object)
|
||||
{
|
||||
SharedDataImpl *impl = ((SharedDataImpl *)(((char *)object) - sizeof(SharedDataHeader)));
|
||||
return impl->mSharedData->GetReadableMemory();
|
||||
}
|
||||
static void SharedDataImplFree(void *ptr)
|
||||
{
|
||||
SharedDataImpl *object = ((SharedDataImpl *)(((char *)ptr) - sizeof(SharedDataHeader)));
|
||||
if (SHARED_DATA_NAME == object->mHeader.mCheckName)
|
||||
{
|
||||
object->mSharedData = nullptr;
|
||||
free(((char *)ptr) - sizeof(SharedDataHeader));
|
||||
}
|
||||
else
|
||||
{
|
||||
LogError("Unknow ptr.\n");
|
||||
}
|
||||
}
|
||||
SharedData *NewSharedDataImpl(const SHARER_NAME &name, const char *path, const int &projectId)
|
||||
{
|
||||
SharedDataImpl *impl = (SharedDataImpl *)malloc(sizeof(SharedDataImpl));
|
||||
SharedDataImpl tmp;
|
||||
memcpy((void *)impl, (void *)&tmp, sizeof(SharedDataImpl));
|
||||
impl->mHeader.mCheckName = SHARED_DATA_NAME;
|
||||
impl->mBase.mMakeSharedData = MakeSharedData;
|
||||
impl->mBase.mGetReadableDataPointer = GetSharedReadableMemory;
|
||||
impl->mBase.mFree = SharedDataImplFree;
|
||||
impl->mSharedData = std::make_shared<SharedDataCpp>(name, path, projectId);
|
||||
return (SharedData *)(((char *)impl) + sizeof(SharedDataHeader));
|
||||
}
|
52
utils/SharedData/src/SharedDataImpl.h
Normal file
52
utils/SharedData/src/SharedDataImpl.h
Normal file
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* Licensed 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.
|
||||
*/
|
||||
#ifndef SHARED_DATA_IMPL_H
|
||||
#define SHARED_DATA_IMPL_H
|
||||
#include "SharedData.h"
|
||||
#include "SharedMemory.h"
|
||||
#include <memory>
|
||||
typedef struct shared_data_header
|
||||
{
|
||||
const char *mCheckName;
|
||||
} SharedDataHeader;
|
||||
typedef struct user_data_header
|
||||
{
|
||||
int mIsInit;
|
||||
} UserDataHeader;
|
||||
class SharedDataCpp : public SharedMemory
|
||||
{
|
||||
public:
|
||||
SharedDataCpp(const SHARER_NAME &sharerName, const char *path, const int &projectId);
|
||||
virtual ~SharedDataCpp() = default;
|
||||
void MakeSharedMemory(const int readableSize, const int writableSize);
|
||||
void *GetReadableMemory(void);
|
||||
|
||||
private:
|
||||
const SHARER_NAME mSharerName;
|
||||
// std::shared_ptr<SharedMemory> mReadableMemory;
|
||||
// std::shared_ptr<SharedMemory> mWritableMemory;
|
||||
unsigned int mReadableSize;
|
||||
unsigned int mWritableSize;
|
||||
void *mSharedMemeory;
|
||||
};
|
||||
typedef struct shared_data_impl SharedDataImpl;
|
||||
typedef struct shared_data_impl
|
||||
{
|
||||
SharedDataHeader mHeader;
|
||||
SharedData mBase;
|
||||
std::shared_ptr<SharedDataCpp> mSharedData;
|
||||
} SharedDataImpl;
|
||||
SharedData *NewSharedDataImpl(const SHARER_NAME &name, const char *path, const int &projectId);
|
||||
#endif
|
65
utils/SharedData/src/SharedMemory.cpp
Normal file
65
utils/SharedData/src/SharedMemory.cpp
Normal file
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* Licensed 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 "SharedMemory.h"
|
||||
#include "ILog.h"
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
constexpr int SHMGET_FAILED = -1;
|
||||
SharedMemory::SharedMemory(const char *path, const int &projectId)
|
||||
: mPath(path), mProjectId(projectId)
|
||||
{
|
||||
mId = SHMGET_FAILED;
|
||||
}
|
||||
StatusCode SharedMemory::MakeSharedMemory(const int &size)
|
||||
{
|
||||
char touchPath[128] = {0};
|
||||
if (access(mPath, F_OK) != 0)
|
||||
{
|
||||
sprintf(touchPath, "%s %s", "touch", mPath);
|
||||
system(touchPath);
|
||||
}
|
||||
key_t key = ftok(mPath, mProjectId);
|
||||
if (key < 0)
|
||||
{
|
||||
LogError("ftok failed.\n");
|
||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||
}
|
||||
mId = shmget(key, size, IPC_CREAT | 0666);
|
||||
if (mId == SHMGET_FAILED)
|
||||
{
|
||||
LogError("shmget failed.\n");
|
||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||
}
|
||||
LogInfo("Make shared memory succeed.\n");
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
StatusCode SharedMemory::CleanSharedMemory(void)
|
||||
{
|
||||
if (shmctl(mId, IPC_RMID, NULL) < 0)
|
||||
{
|
||||
LogError("shmctl failed.\n");
|
||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||
}
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
void *SharedMemory::GetMemory(void)
|
||||
{
|
||||
return shmat(mId, NULL, 0);
|
||||
}
|
32
utils/SharedData/src/SharedMemory.h
Normal file
32
utils/SharedData/src/SharedMemory.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Fancy Code.
|
||||
* Licensed 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.
|
||||
*/
|
||||
#ifndef SHARED_MEMORY_H
|
||||
#define SHARED_MEMORY_H
|
||||
#include "StatusCode.h"
|
||||
class SharedMemory
|
||||
{
|
||||
public:
|
||||
SharedMemory(const char *path, const int &projectId);
|
||||
virtual ~SharedMemory() = default;
|
||||
StatusCode MakeSharedMemory(const int &size);
|
||||
StatusCode CleanSharedMemory(void);
|
||||
void *GetMemory(void);
|
||||
|
||||
private:
|
||||
const char *mPath;
|
||||
const int mProjectId;
|
||||
int mId;
|
||||
};
|
||||
#endif // !SHARED_MEMORY_H
|
Loading…
Reference in New Issue
Block a user