本文整理汇总了C++中Heap::GetBlock方法的典型用法代码示例。如果您正苦于以下问题:C++ Heap::GetBlock方法的具体用法?C++ Heap::GetBlock怎么用?C++ Heap::GetBlock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Heap
的用法示例。
在下文中一共展示了Heap::GetBlock方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Semaphore
static void *internal_Allocate(int size, int flags, char *file, int line){
#else
static void *internal_Allocate(int size, int flags){
#endif
// assert(size!=48);
//int log=1;
//int tmp = size-1;
//while((tmp>>=1)!=0) log++;
//printf("AllocateSize %d,%d\n",size,log);
if(!initialized){
heap_sem = new Semaphore();
heap_sem->Acquire();
#ifdef MEMORY_STATS
thread_sem = new Semaphore();
thread_sem->Acquire();
unsigned long dwThreadId;
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ticker, 0, 0, &dwThreadId);
// _beginthread(&ticker,4096,0);
thread_sem->Acquire();
// Sleep(100000);
#endif
HANDLE file = CreateFileMapping((HANDLE)-1,0,PAGE_READWRITE,0,HEAP_SIZE+sizeof(Heap),0);
printf("Total shared heap space allocated = %d bytes\n",heap_bytes+=HEAP_SIZE);
Heap *h = (Heap*)MapViewOfFile(file,FILE_MAP_WRITE,0,0,0);
assert(h);
first_heap = h;
h->Next = 0;
h->File = file;
memset(h->Mask,0,HEAP_SIZE/BLOCK_SIZE/8);
memset(h->Type,0,HEAP_SIZE/BLOCK_SIZE/8);
memset(h->Size,-1,HEAP_SIZE/BLOCK_SIZE*sizeof(int));
h->Blocks = (void*)(int(h)+sizeof(Heap));
initialized = true; // Assume we get no errors
#ifdef _DEBUG
void *block = internal_Allocate(BLOCK_SIZE-sizeof(MemHeader),flags,0,0);
#else
void *block = internal_Allocate(BLOCK_SIZE-sizeof(MemHeader),flags);
#endif
assert(block);
if(!block){
heap_sem->Release();
printf("Failed!\n");
return 0;
}
int _size = 32*sizeof(Fragment*); // fixme: not 32 bits
int log=1;
int tmp = _size-1;
while((tmp>>=1)!=0) log++;
h = GetHeap(block);
int blockn = h->GetBlock(block);
h->Size[blockn] = size;
h->Type[blockn/8]|=1<<(blockn&7);
Fragments = (Fragment**)block;
memset(Fragments,0,32*sizeof(Fragment*)); // fixme: not 32 bits
Fragments[log] = (Fragment*)(int(block)+(2<<log));
Fragments[log]->Next = 0;
Fragments[log]->Prev = 0;
for(int n=3; n<(BLOCK_SIZE-sizeof(MemHeader))>>log; n++){
Fragment *frag = (Fragment*)(int(block)+(n<<log));
Fragments[log]->Next = frag;
frag->Next = 0;
frag->Prev = Fragments[log];
Fragments[log] = frag;
}
heap_sem->Release();
#ifdef MEMORY_STATS
total_used_history = (DynamicArray*)internal_Allocate(sizeof(DynamicArray),flags,0,0);
total_size_history = (DynamicArray*)internal_Allocate(sizeof(DynamicArray),flags,0,0);
total_frag_history = (DynamicArray*)internal_Allocate(sizeof(DynamicArray),flags,0,0);
total_frags_history = (DynamicArray*)internal_Allocate(sizeof(DynamicArray),flags,0,0);
total_blocks_history = (DynamicArray*)internal_Allocate(sizeof(DynamicArray),flags,0,0);
total_used_history->DynamicArray::DynamicArray(DARRAY_SHARED_MEMORY);
total_size_history->DynamicArray::DynamicArray(DARRAY_SHARED_MEMORY);
total_frag_history->DynamicArray::DynamicArray(DARRAY_SHARED_MEMORY);
total_frags_history->DynamicArray::DynamicArray(DARRAY_SHARED_MEMORY);
total_blocks_history->DynamicArray::DynamicArray(DARRAY_SHARED_MEMORY);
#endif
}
heap_sem->Acquire();
int orgsize = size;
size+=sizeof(MemHeader);
if(size<sizeof(Fragment)) size = sizeof(Fragment);
if(size<=BLOCK_SIZE/2){
int log=1;
int tmp = size-1;
while((tmp>>=1)!=0) log++;
//printf("Frag %d!\n",log);
if(!Fragments[log]){
//.........这里部分代码省略.........