C++简单封装共享内存

这篇博客主要介绍了如何在Linux环境下使用C++进行共享内存的简单封装,目前内容仅针对Linux,Windows平台的实现将在后续补充。
摘要由CSDN通过智能技术生成

目前代码只考虑linux环境,win32将随后补充:

Assertx.h

/* 
 * File:   Assertx.h
 * Author: Vicky.H
 * Email:  eclipser@163.com
 *
 * Created on 2014年1月16日, 下午8:33
 */

// 断言

#ifndef CN_VICKY__ASSERTX_H
#define	CN_VICKY__ASSERTX_H

#include <sys/types.h>
#include <stdio.h>

extern int g_Command_Assert; //控制参数,不提示Assert的对话框,直接忽略
extern int g_Command_IgnoreMessageBox; //控制参数,跳过MyMessageBox的中断

void __assert__(const char* file, uint line, const char* func, const char* expr);
void __assertex__(const char* file, uint line, const char* func, const char* expr, const char* msg);
void __assertspecial__(const char* file, uint line, const char* func, const char* expr, const char* msg);
void __messagebox__(const char* msg);

void __protocol_assert__(const char* file, int line, const char* func, const char* expr);

#define Assert(expr) {if(!(expr)){__assert__(__FILE__,__LINE__,__PRETTY_FUNCTION__,#expr);}}
#define ProtocolAssert(expr) ((VOID)((expr)?0:(__protocol_assert__(__FILE__,__LINE__,__PRETTY_FUNCTION__,#expr),0)))
#define AssertEx(expr,msg) {if(!(expr)){__assertex__(__FILE__,__LINE__,__PRETTY_FUNCTION__,#expr,msg);}}
#define AssertSpecial(expr,msg) {if(!(expr)){__assertspecial__(__FILE__,__LINE__,__PRETTY_FUNCTION__,#expr,msg);}}
#define AssertExPass(expr,msg) {if(!(expr)){__assertex__(__FILE__,__LINE__,__PRETTY_FUNCTION__,#expr,msg);}}
#define MyMessageBox(msg) ((VOID)0)

#endif	/* CN_VICKY__ASSERTX_H */

Assertx

#include "Assertx.h"
#include <time.h>
#include <execinfo.h>

int g_Command_Assert = 0;
//             控制参数,0:会通过弹出对话框让用户选择(缺省值)
//			1:忽略
//			2:继续抛出异常用于获取运行堆栈
int g_Command_IgnoreMessageBox = false; //控制参数,跳过MyMessageBox的中断

void __show__(const char* szTemp) {
    printf("Assert:%s", szTemp);
    throw (1);
}

void __messagebox__(const char*msg) {
    if (g_Command_IgnoreMessageBox)
        return;
}

void __assert__(const char * file, uint line, const char * func, const char * expr) {
    char szTemp[1024] = {0};

    sprintf(szTemp, "[%s][%d][%s][%s]\n", file, line, func, expr);
    __show__(szTemp);
}

void __assertex__(const char * file, uint line, const char * func, const char * expr, const char* msg) {
    char szTemp[1024] = {0};
    sprintf(szTemp, "[%s][%d][%s][%s]\n[%s]\n", file, line, func, expr, msg);
    __show__(szTemp);
}

void __assertspecial__(const char * file, uint line, const char * func, const char * expr, const char* msg) {
    char szTemp[1024] = {0};

    sprintf(szTemp, "S[%s][%d][%s][%s]\n[%s]\n", file, line, func, expr, msg);
    __show__(szTemp);
}

void __protocol_assert__(const char * file, uint line, const char * func, const char * expr) {
    printf("[%s][%d][%s][%s]", file, line, func, expr);
}

Common.h

/* 
 * File:   Common.h
 * Author: Vicky.H
 * Email:  eclipser@163.com
 *
 * Created on 2014年1月16日, 下午10:46
 */

#ifndef CN_VICKY__COMMON_H
#define	CN_VICKY__COMMON_H

#include "Assertx.h"

//无效的ID值
#define INVALID_ID -1

#define __ENTER_FUNCTION {try{
#define __LEAVE_FUNCTION }catch(...){AssertSpecial(false,__PRETTY_FUNCTION__);}}

//根据指针值删除内存
#ifndef SAFE_DELETE
#define SAFE_DELETE(x)	if( (x)!=NULL ) { delete (x); (x)=NULL; }
#endif
//根据指针值删除数组类型内存
#ifndef SAFE_DELETE_ARRAY
#define SAFE_DELETE_ARRAY(x)	if( (x)!=NULL ) { delete[] (x); (x)=NULL; }
#endif
//根据指针调用free接口
#ifndef SAFE_FREE
#define SAFE_FREE(x)	if( (x)!=NULL ) { free(x); (x)=NULL; }
#endif
//根据指针调用Release接口
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(x)	if( (x)!=NULL ) { (x)->Release(); (x)=NULL; }
#endif

#endif	/* CN_VICKY__COMMON_H */

log.h

/* 
 * File:   Log.h
 * Author: Vicky.H
 * Email:  eclipser@163.com
 *
 * Created on 2014年1月16日, 下午10:45
 */

#ifndef CN_VICKY__LOG_H
#define	CN_VICKY__LOG_H
#include <pthread.h>

#define SHMEM_LOG_PATH "./Log/ShareMemory.log"

extern pthread_mutex_t log_mutex;

class Log {
public:
    Log();
    ~Log();
    //支持异步写入操作的日志写入
    static void SaveLog(const char* filename, const char* msg, ...);
};

#endif	/* CN_VICKY__LOG_H */

Log.cpp

#include "Log.h"
#include "Common.h"
#include <stdarg.h>
#include <string.h>
#include <stdio.h>

// 保证日志记录线程安全
pthread_mutex_t log_mutex;

Log::Log() {
}

Log::~Log() {
}

void Log::SaveLog(const char* filename, const char* msg, ...) {
    __ENTER_FUNCTION

    pthread_mutex_lock(&log_mutex);

    char buffer[2048];
    memset(buffer, 0, 2048);

    va_list argptr;

    try {
        va_start(argptr, msg);
        vsprintf(buffer, msg, argptr);
        va_end(argptr);

        FILE* f = fopen(filename, "ab");
        fwrite(buffer, 1, strlen(buffer), f);
        fclose(f);

        printf(buffer);
    } catch (...) {
        printf("a big error here");
    }

    pthread_mutex_unlock(&log_mutex);
    return;

    __LEAVE_FUNCTION

    pthread_mutex_unlock(&log_mutex);
}

ShareMemApi.h

共享内存封装指的是将共享内存的相关操作封装成一个独立的函数或者类,以提供更方便的使用方式和更高的代码复用性。 在实现共享内存封装的过程中,一般会包含以下几个步骤: 1. 创建共享内存封装函数或类中应该提供创建共享内存的接口,通过系统调用或第三方库来创建一个共享内存区域。 2. 连接到共享内存封装函数或类中应该提供连接到已经存在的共享内存的接口,以便于进行数据的读写操作。 3. 读写共享内存封装函数或类中应该提供读写共享内存的方法,可以通过指针操作或者其他方式来实现对共享内存数据的读写。 4. 销毁共享内存封装函数或类中应该提供销毁共享内存的接口,以确保在不再需要共享内存时能够正确释放相关资源。 通过进行共享内存封装,可以实现对共享内存的抽象和封装,提供更易用的接口和更清晰的设计,减少对外暴露细节,在一定程度上提高代码的可维护性和可读性。 同时,在封装的过程中,还可以考虑引入其他特性,比如读写锁、信号量等,以提供更高级别的功能和更好的线程安全性,确保多个进程之间对共享内存的访问能够有序进行。 总结来说,共享内存封装是为了简化共享内存的使用和提高代码复用性,通过提供一组接口和抽象,使得对共享内存的操作更加方便和可控。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值