一个用于内存管理分块的代码
/* mIndexes 位置说明: 3是取结果的位置,2写的结果的位置,1是取图片的位置,0是放图片位置*/ /* mIndexes 其他位置*/ #define WIDTH_INDEX 10 // 图片宽度 // #define HEIGHT_INDEX (WIDTH_INDEX + 1) // 图片高度 // #define IMAGE_FORMAT_INDEX (WIDTH_INDEX + 2) // 图片格式 // #define IMAGE_SNAP_WRITE_INDEX (WIDTH_INDEX + 3) // 获取单张图片设置标准位 // #define IMAGE_SNAP_READ_INDEX (WIDTH_INDEX + 4) // 获取单张图片读取标准位 // #define IMAGE_SERIES_WRITE_RESULT (WIDTH_INDEX + 5) /* 正在连续写图片结果 */ #define IMAGE_SERIES_READ_RESULT (WIDTH_INDEX + 6) /* 正在连续读图片结果 */ #define HEADER_SIZE_UNIT (1024 * 64) /*最大的size*/ #define HEADER_RATE (4) /**/ #define DATA_START_UNIT (1024) class ImageShareBuffer { public: struct ShareBuffe { string memfile; int dataStart; int blockcount; int headersize; int width; int height; int format; }; class DataBlock { public: void* dataptr; void* infoptr; int blocksize; int position; }; static ImageShareBuffer* create(const char* configfile) { int error = 0; ImageShareBuffer* is = new ImageShareBuffer(); if (is->load(configfile) == 0) { if (is->createHost() == 0) { is->init(1); } else error = -2; } else error = -1; if (!error) return is; else { delete is; return nullptr; } } static ImageShareBuffer* create(const ShareBufferInfo info) { int error = 0; ImageShareBuffer* is = new ImageShareBuffer(); is->datastart = info.dataStart; is->blockcount = info.blockcount; is->headersize = info.headersize; is->width = info.width; is->height = info.height; is->format = info.format; is->memfile = info.memfile; is->dPix = 1; if (!error) return is; else { delete is; return nullptr; } } static ImageShareBuffer* open(const char* configfile) { int error = 0; ImageShareBuffer* is = new ImageShareBuffer(); if (is->load(configfile) == 0) { if (is->openClient() == 0) { is->init(); } else error = -2; } else error = -1; if (!error) return is; else { delete is; return nullptr; } } static ImageShareBuffer* open(const ShareBufferInfo info) { int error = 0; ImageShareBuffer* is = new ImageShareBuffer(); is->datastart = info.dataStart; is->blockcount = info.blockcount; is->headersize = info.headersize; is->width = info.width; is->height = info.height; is->format = info.format; is->memfile = info.memfile; is->dPix = 1; if (is->format == IMAGE_FORMAT_RGB888) // 1彩色;0,2,3灰色 { is->dPix = 3; // RGB } if (is->format == 4) { is->dPix = 8; } if (is->openClient() == 0) { is->init(); } else error = -2; if (!error) return is; else { delete is; return nullptr; } } static ImageShareBuffer* createMap2(const char* configfile) { int error = 0; ImageShareBuffer* is = new ImageShareBuffer(); if (is->load(configfile) == 0) { if (is->createMap() == 0) { is->init(); } else error = -2; } else error = -1; if (!error) return is; else { delete is; return nullptr; } } int position(int sectionid = 0) { return mIndexes[sectionid]; } void setdata(int data, int index = 1) { if (index > 1024 || index < 0) { cout << "setdata out of 0~1024 rect: " << index << endl; return; } mIndexes[index] = data; } int size(int sectionid = 0) { int max = mDataBlocks.size(); if (mIndexes[sectionid] < max) return mIndexes[sectionid]; else return max - 1; } DataBlock* get(int pos) { if (pos >= 0) { int max = mDataBlocks.size(); int ind = pos % max; mDataBlocks[ind].position = pos; return &mDataBlocks[ind]; } return 0; } int commit(DataBlock* db, int sectionid = 0) { int position = db->position; mIndexes[sectionid] = position + 1; return 0; } int maxBufferCnt() { return mDataBlocks.size(); } ~ImageShareBuffer() { releaseMap(); } inline void activate() { *mActiveTime = GetTickCount(); } int getSnapWriteFlag() { return mIndexes[IMAGE_SNAP_WRITE_INDEX]; } void setSnapWriteFlag(int flag) { mIndexes[IMAGE_SNAP_WRITE_INDEX] = flag; } int getSnapReadFlag() { return mIndexes[IMAGE_SNAP_READ_INDEX]; } void setSnapReadFlag(int flag) { mIndexes[IMAGE_SNAP_READ_INDEX] = flag; } int getSeriesWriteFlag() { return mIndexes[IMAGE_SERIES_WRITE_RESULT]; } void setSeriesWriteFlag(int flag) { mIndexes[IMAGE_SERIES_WRITE_RESULT] = flag; } int getSeriesReadFlag() { return mIndexes[IMAGE_SERIES_READ_RESULT]; } void setSeriesReadFlag(int flag) { mIndexes[IMAGE_SERIES_READ_RESULT] = flag; } public: int width; int height; int dPix; int blockcount; int headersize; int datastart; int format; string memfile; private: ImageShareBuffer() {} int init(int host = 0) { mActiveTime = ( long long* )mShareMemoryPointer; mIndexes = ( int* )mShareMemoryPointer + 2; if (host == 1) { memset(mIndexes, 0, datastart * DATA_START_UNIT - 2 * sizeof(int)); mIndexes[WIDTH_INDEX] = width; mIndexes[HEIGHT_INDEX] = height; mIndexes[IMAGE_FORMAT_INDEX] = dPix; mIndexes[IMAGE_SNAP_WRITE_INDEX] = 0; mIndexes[IMAGE_SNAP_READ_INDEX] = 7; } char* base = ( char* )mShareMemoryPointer + datastart * DATA_START_UNIT; for (int i = 0; i < blockcount; i++) { DataBlock db; db.infoptr = base; db.dataptr = ( void* )(base + headersize * HEADER_SIZE_UNIT); // db.blocksize = headersize * HEADER_SIZE_UNIT + width * height * dPix; db.blocksize = headersize * HEADER_SIZE_UNIT + getImageBufLen(); db.position = -1; mDataBlocks.push_back(db); base += db.blocksize; } return 0; } HANDLE mHandle; void* mShareMemoryPointer; // 首地址 // nlohmann::json mConfig; vector<DataBlock> mDataBlocks; long long* mActiveTime; int* mIndexes; int openClient() { int error = 0; mHandle = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, memfile.c_str()); if (mHandle != NULL) { mShareMemoryPointer = MapViewOfFile(mHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0); if (mShareMemoryPointer == NULL) error = -2; } else error = -1; if (error) cout << "open client fail: " << error << endl; return error; } int loadParam() { try { datastart = mConfig["datastart"]; blockcount = mConfig["blockcount"]; headersize = mConfig["headersize"]; width = mConfig["width"]; height = mConfig["height"]; format = mConfig["format"]; memfile = mConfig["memfile"]; dPix = 1; if (format == IMAGE_FORMAT_RGB888) // 1彩色;0,2,3灰色 { dPix = 3; // RGB } return 0; } catch (...) { return -1; } } int createHost() { int error = 0; // DWORD size = datastart * DATA_START_UNIT + blockcount * (headersize * HEADER_SIZE_UNIT + width * height * dPix); // int64_t big = int64_t(datastart * DATA_START_UNIT) + blockcount * int64_t(headersize * HEADER_SIZE_UNIT + width * height * dPix); int64_t big = int64_t(datastart * DATA_START_UNIT) + blockcount * int64_t(headersize * HEADER_SIZE_UNIT + getImageBufLen()); DWORD high = (big & 0xffffffff00000000) >> 32; DWORD low = big & 0x00000000ffffffff; mHandle = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, high, low, memfile.c_str()); if (mHandle != NULL) { mShareMemoryPointer = MapViewOfFile(mHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0); if (mShareMemoryPointer == NULL) error = -2; else { memset(mShareMemoryPointer, 0, datastart); } } else { error = -1; } if (error) { cout << "create host fail: " << error << endl; int last = GetLastError(); cout << "GetLastError: " << last << endl; // ERROR_INVALID_HANDLE //6,如果发现你的命名内存空间和现有的内存映射,互斥量,信号量,临界区同名就麻烦了 // ERROR_NOT_ENOUGH_MEMORY //8,内存不足 // ERROR_ALREADY_EXISTS //183,表示内存空间命名已经存在 // ERROR_FILE_INVALID //1006,企图创建一个零长度的文件映射 // ERROR_NO_SYSTEM_RESOURCES //1450,资源不足 // ERROR_COMMITMENT_LIMIT //1455,页文件太小 } return error; } int createMap() { int error = 0; // DWORD size = datastart * DATA_START_UNIT + blockcount * (headersize * HEADER_SIZE_UNIT + width * height * dPix); // int64_t big = int64_t(datastart * DATA_START_UNIT) + blockcount * int64_t(headersize * HEADER_SIZE_UNIT + width * height * dPix); int64_t big = int64_t(datastart * DATA_START_UNIT) + blockcount * int64_t(headersize * HEADER_SIZE_UNIT + getImageBufLen()); DWORD high = (big & 0xffffffff00000000) >> 32; DWORD low = big & 0x00000000ffffffff; // LARGE_INTEGER *n = (LARGE_INTEGER *)&big; // DWORD high1 = n->HighPart; // DWORD low1 = n->LowPart; // 创建共享内存// mHandle = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, high, low, memfile.c_str()); if (mHandle != NULL) { mShareMemoryPointer = MapViewOfFile(mHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0); if (mShareMemoryPointer == NULL) error = -2; else { memset(mShareMemoryPointer, 0, datastart); } } else { // 创建失败打开共享内存// mHandle = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, memfile.c_str()); if (mHandle != NULL) { mShareMemoryPointer = MapViewOfFile(mHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0); if (mShareMemoryPointer == NULL) error = -2; } else { error = -1; } } if (error) { cout << "create host fail: " << error << endl; int last = GetLastError(); cout << "GetLastError: " << last << endl; // ERROR_INVALID_HANDLE //6,如果发现你的命名内存空间和现有的内存映射,互斥量,信号量,临界区同名就麻烦了 // ERROR_NOT_ENOUGH_MEMORY //8,内存不足 // ERROR_ALREADY_EXISTS //183,表示内存空间命名已经存在 // ERROR_FILE_INVALID //1006,企图创建一个零长度的文件映射 // ERROR_NO_SYSTEM_RESOURCES //1450,资源不足 // ERROR_COMMITMENT_LIMIT //1455,页文件太小 } return error; } void releaseMap() { if (mShareMemoryPointer != NULL) UnmapViewOfFile(mShareMemoryPointer); mShareMemoryPointer = NULL; if (mHandle) CloseHandle(mHandle); mHandle = NULL; } int load(const char* jsonfile) { try { ifstream(jsonfile) >> mConfig; int res = loadParam(); return res; } catch (...) { return -1; } } public: int getImageBufLen(void) { int step = 0; if (dPix == 1) { step = calcBytesPerLine(width, 8); } else if (dPix == 3) { step = calcBytesPerLine(width, 24); } else if (dPix == 8) { step = calcBytesPerLine(width, 64); } int len = step * height; return len; } private: int calcBytesPerLine(int width, int bitCounts) { int step = 0; if (bitCounts >= 8) { step = (width * bitCounts / 8 + 3) / 4 * 4; } else { step = (width * bitCounts + 31) / 32 * 4; } return step; } };
标签:INDEX,mIndexes,return,管理,一个,else,int,error,共享内存 From: https://www.cnblogs.com/dwx-bzdcxy/p/18663577