Memory management in C + + is the most troublesome thing, memory request release, memory leak, memory out of bounds, or even memory fragmentation, will cause the program out of core or slow. How to manage memory effectively, there are many ways, I think the easiest way is to use a memory pool to manage memory.
When it comes to the memory pool, it is necessary to say the life cycle and scope of the program, the data is divided into three categories: 1 classes are process data (global data). 2, thread data, each one of the threads. 3, request the data, each time calls one copy.
It may not be appropriate for a memory pool to have any lifecycle and scope data in it. So my advice is to use a process of one memory pool, one memory pool per thread, and one memory pool per request.
The initial memory pool at the start of the process/thread/request, destroying the memory pool at the end of the process/thread/request.
So, there's this very simple memory pool, the code is a little bit around, but it's understandable to read it carefully.
Header file AutoMemory.h
#ifndef _auto_memory_h__
#define _AUTO_MEMORY_H__
#include <stddef.h>/
/#define _MEM_TEST_
/ /#define _MEM_DEBUG_
#define _MEM_USE_
//default 1M
#define MEMORY_BLOCK_SIZE 1000000
*
* thread compatible, Automatic release
*
/class automemory
{public
:
automemory ();
automemory (int block_size);
~automemory ();
void* Alloc (size_t size);
void free (void* ptr);
void Freeall ();
void Debug ();
int Blockcount ();
size_t leftsize ();
size_t memsize ();
Private:
struct memblock{
memblock* prev;
Private:
int headersize;
int blockSize;
size_t memsize;
int blockcount;
Char* begin;
Char* end;
memblock* currblock;
};
#endif
CPP file AutoMemory.cpp
#include <stdlib.h> #include <stdio.h> #include <string.h> #include "AutoMemory.h" Automemory::
Automemory () {blockSize = memory_block_size;
Blockcount = 0;
memsize = 0;
headersize = sizeof (memblock);
Begin = end = (char*) headersize;
} automemory::automemory (int block_size) {blockSize = block_size;
Blockcount = 0;
memsize = 0;
headersize = sizeof (memblock);
Begin = end = (char*) headersize;
} automemory::~automemory () {Freeall ();}
size_t automemory::memsize () {return memsize;}
int Automemory::blockcount () {return blockcount;}
size_t automemory::leftsize () {return (size_t) (End-begin);} void Automemory::D ebug () {printf ("block count%d, left size%d, total size%d\n", This->blockcount (), this->l
Eftsize (), this->memsize ()); void* Automemory::alloc (size_t size) {#ifndef _mem_use_ return malloc (size); #endif if ((size_t) (End-begin) ; Size) {if (size >= blockSize) {char* ptr = (char*) malloc (headersize + size);
memblock* pnew = (memblock*) ptr;
memblock* Pheader = (memblock*) (begin-headersize);
if (pheader) {Pnew->prev = pheader->prev;
Pheader->prev = pnew;
}else{end = begin = (char*) (ptr + headersize);
Pnew->prev = NULL;
} blockcount = 1;
Memsize + = size;
return end;
else{char* ptr = (char*) malloc (headersize + blockSize);
memblock* pnew = (memblock*) ptr;
Pnew->prev = (memblock*) (begin-headersize);
Begin = (char*) (ptr + headersize);
End = begin + BlockSize;
Blockcount + 1;
Memsize + = BlockSize;
} return end-= size; } void Automemory::free (void* ptr) {#ifndef _mem_use_ free (PTR); #endif} void Automemory::freeall () {memblock* Phea
Der = (memblock*) (begin-headersize);
while (Pheader) {memblock* ptemp = pheader->prev;
Free (pheader);
Pheader = ptemp;
Begin = end = (char*) headersize;
memsize = 0;
Blockcount = 0; #ifdef _mem_test_ int Main () {automemory am(100);
printf ("IDX 1, Alloc 10\n"); char* buf = (char*) am.
Alloc (10); Am.
Debug ();
printf ("IDX 2, Alloc 90\n"); Am.
Alloc (90); Am.
Debug ();
printf ("IDX 3, Alloc 10\n"); Am.
Alloc (10); Am.
Debug ();
printf ("IDX 4, Alloc 100\n"); Am.
Alloc (100); Am.
Debug ();
printf ("IDX 5, Alloc 10\n"); Am.
Alloc (10); Am.
Debug ();
} #endif