本文整理汇总了C++中split_block函数的典型用法代码示例。如果您正苦于以下问题:C++ split_block函数的具体用法?C++ split_block怎么用?C++ split_block使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了split_block函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: malloc
void *realloc(void *ptr, size_t size){
if (!ptr) {
return malloc(size);
}
if (valid_address(ptr)) {
size = align_pointer(size);
block_t block = get_block(ptr);
if (size <= block->size && BLOCK_SIZE_MIN <= block->size - size) {
split_block(block, size);
} else {
if (block->next && block->next->free && size <= BLOCK_SIZE + block->size + block->next->size) {
//merge möglich
merge_block(block);
if (BLOCK_SIZE_MIN <= block->size - size) {
split_block(block, size);
}
} else {
//real malloc
void *newptr = malloc(size);
if (!newptr) {
perror("error at realloc");
return NULL;
}
block_t newblock = get_block(newptr);
copy_block(block, newblock);
free(block);
return newptr;
}
}
return ptr;
}
return 0;
}
示例2: page_type
t_block *search_freed_block(size_t size)
{
t_page *page;
t_block *b;
size_t mem_width;
t_page_type mal_type;
mal_type = page_type(size);
page = first_page();
b = NULL;
while (page != NULL)
{
if (page->type == mal_type)
b = search_freed_block_in_page(page, size);
if (b != NULL)
break ;
page = page->next;
}
if (b == NULL)
return (NULL);
mem_width = b->size + BLOCK_SIZE;
b->size = size;
split_block(b, mem_width);
b->is_free = 0;
return (b);
}
示例3: mm_realloc
void* mm_realloc(void* ptr, size_t size)
{
#ifdef MM_USE_STUBS
return realloc(ptr, size);
#else
if(size==0){
free(ptr);
return ptr;
}
if(!ptr)
return malloc(size);
t_block last;
t_block b = valid_addr(ptr,&last);
if(!b)
return ptr;
size_t s = align4(size);
if(b->size >= s){
if(b->size > s+BLOCK_SIZE+4)
split_block(b,s);
return ptr;
}
void *new = malloc(s);
if(!new)
return NULL;
size_t i,s4,*sp,*newp;
s4=s/(sizeof(size_t));
sp=(size_t*)(p);
newp=(size_t*)(new);
for(i=0;i<s4;i++)
newp[i]=sp[i];
free(ptr);
return new;
//#error Not implemented.
#endif
}
示例4: mm_malloc
void* mm_malloc(size_t size)
{
#ifdef MM_USE_STUBS
return calloc(1, size);
#else
s_block_ptr block, last;
size_t s;
s = align4(size);
if(foot){
last = foot;
block = find_block(last, s);
if(block){
if((block->size - s) >= (BLOCK_SIZE + 4)) split_block(block, s);
block->free = 0;
}
else{
block = extend_heap(last, s);
if(!block) return NULL;
}
}
else{
block = extend_heap(NULL, s);
if(!block) return NULL;
foot = block;
}
return block->data;
//#error Not implemented.
#endif
}
示例5: allocate_new_space
void* allocate_new_space(size_t size)
{
void* extend_heap_ptr = NULL;
size_t one_time_alloc = find_one_time_sbrk_size(size);
extend_heap_ptr = sbrk(one_time_alloc);
MDIC(extend_heap_ptr) -> size = one_time_alloc;
MDIC(extend_heap_ptr) -> occupy = false;
free_list_add(extend_heap_ptr,size2order(MDIC(extend_heap_ptr)->size));
MDIC(extend_heap_ptr) -> area_head = extend_heap_ptr;
MDIC(extend_heap_ptr) -> area_end = extend_heap_ptr + one_time_alloc;
total_size += one_time_alloc;
if(heap_ptr == NULL) heap_ptr = extend_heap_ptr;
L(printf("allocate_new_space(): sbrked %zu bytes at loc %zu, total_size: %zu\n",
MDIC(extend_heap_ptr) -> size, (size_t)(extend_heap_ptr - heap_ptr) ,total_size));
if (MDIC(extend_heap_ptr) -> size >= size)
{
/* Split if necessary, then return */
if(size > MDIC(extend_heap_ptr)->size / 2) return extend_heap_ptr;
return split_block(extend_heap_ptr,size);
}
else
{
D( printf("new area %zu is smaller than the size %zu needed\n",one_time_alloc,size ) );
exit(0);
}
}
示例6: mem_get
long mem_get(long request){
long l = request;
long lidx = 0;
long idx = 2;
while(idx != 0){
if(get_length(idx) >= l && get_free(idx)){
/* Gat gevonden waar de aanvraag in past. */
lidx = idx;
l = get_length(idx);
break;
}
idx = get_next(idx);
}
if(lidx == 0){
/* Geen gat groot genoeg. */
return -1;
}else if(l == request){
/* Gat precies groot genoeg. */
mem[0] += request;
mem[1] += ADMIN_SIZE;
set_free(lidx, 0);
return lidx + ADMIN_SIZE;
}else{
/* Alleen een gat > request. */
return split_block(lidx, request);
}
}
示例7: find_fit
/**********************************************************
* find_fit
* Traverse the heap searching for a block to fit asize
* Return NULL if no free blocks can handle that size
* Assumed that asize is aligned
* Uses fit bit option
* If we find a block that is close to the request size (4*sizethreshold)
* we will use that block
* If we can't find a block close to request size then we return the closest
* size and split the block before using it
**********************************************************/
void * find_fit(size_t asize, int *list_broken)
{
if(asize>maxBlockSize1)
return NULL;
size_t bestfit = mem_heapsize();
size_t curfit;
void *bestfitptr = NULL;
void *free_bp;
int sizethreshold = 2*DSIZE;
for (free_bp = free_listp; free_bp != NULL; free_bp = *((void **)free_bp))
{
if (asize <= GET_SIZE(HDRP(free_bp)))
{
curfit = GET_SIZE(HDRP(free_bp)) - asize;
if (curfit < 4*sizethreshold) {
bestfitptr = free_bp;
break;
}
if (curfit < bestfit) {
bestfit = curfit;
bestfitptr = free_bp;
}
}
}
if ((bestfitptr != NULL) && (GET_SIZE(HDRP(bestfitptr)) - asize >= sizethreshold)) {
bestfitptr = split_block (bestfitptr, asize);
*list_broken = 1;
}
return bestfitptr;
}
示例8: list_for_each
list_for_each(p, heap) {
uint64_t start = (p->start + mask) & ~mask;
if (p->file_priv == NULL &&
start + size <= p->start + p->size)
return split_block(p, start, size, file_priv);
}
示例9: wf_malloc
void * wf_malloc (size_t size){
t_block b, last;//, size_of_whole_block;
size_t s;
//aligning the requested size using the macro defined in my_malloc.h
s = ALIGN_SIZE(size, sizeof(char*));
//if base is initialized, we can proceed to implement malloc
//size_of_used_block += size;
if(base){
//first find a block
last = base;
b = find_worst_block (&last,s);
//size_of_whole_block=last;
if (b){
// size_of_whole_block->size += b->size;
//is it big enough to be split
if((b->size - s)>= (BLOCK_SIZE + 4 ))
split_block (b,s);
b->free = 0;//mark the chunk as used
} else {//otherwise we extend the heap(NO FITTING BLOCK )
b = extend_heap(last,s);
if (!b)
return NULL;
}
}
else {//Heap is empty (first time allocation)
b = extend_heap(NULL,s);
if(!b)
return(NULL);
base = b;
}
//size_of_whole_block->size += b->size;
head = b;
return (b->data);
}
示例10: ShmemDynAlloc
/*
* ShmemDynAlloc
*/
void *
ShmemDynAlloc(Size size)
{
void *block = NULL;
Size padded_size;
size = Max(ALIGN(size), MIN_ALLOC_SIZE);
for (padded_size = 1; padded_size < size && padded_size <= 1024; padded_size *= 2);
size = Max(size, padded_size);
block = get_block(size);
if (block == NULL)
{
/*
* Don't request fewer than 1k from ShmemAlloc.
* The more contiguous memory we have, the better we
* can combat fragmentation.
*/
Size alloc_size = Max(size, MIN_SHMEM_ALLOC_SIZE);
block = ShmemAlloc(BLOCK_SIZE(alloc_size));
memset(block, 0, BLOCK_SIZE(alloc_size));
block = (void *) ((intptr_t) block + sizeof(Header));
init_block(block, alloc_size, true);
mark_allocated(block);
}
if (get_size(block) - size >= MIN_BLOCK_SIZE)
split_block(block, size);
Assert(is_allocated(block));
return block;
}
示例11: align8
//allocate memory
void *malloc(size_t size){
t_block newBlock, last;
size_t newSize = align8(size);
if(base){
last = (t_block) base;
newBlock = find_block(&last, newSize);
if(newBlock){
//can we split
if((newBlock->size - newSize) >= (OFFSET + PTR_SIZE)){
split_block(newBlock, newSize);
}
newBlock->block_free = false;
} else {
//Can't find block, try extending the heap
newBlock = extend_heap(last, newSize);
if(!newBlock){
//cannot extend heap
return 0x0;
}
}
} else {
//first call
newBlock = extend_heap(0x0, newSize);
if(!newBlock){
//failed to extend
return 0x0;
}
base = newBlock;
}
return newBlock->data;
}
示例12: bf_malloc
//No. 2 Best fit algorithm
void * bf_malloc(size_t size){
t_block b,last;//,size_of_whole_block;
size_t s;
s = ALIGN_SIZE(size, sizeof(char*));
//size_of_used_block += size;
if (base){
//find the best block
last = base;
b = find_best_block (&last, s);
//size_of_whole_block=last;
if (b){
// size_of_whole_block->size += b->size;
//is it big enough to be split
if ((b->size - s)>= (BLOCK_SIZE + 4 ))
split_block (b,s);
b->free = 0;//mark the chunk as used
} else {//otherwise we extend the heap(NO FITTING BLOCK )
b = extend_heap(last,s);
if (!b)
return NULL;
}
}
else {//Heap is empty (first time allocation)
b = extend_heap(NULL,s);
if(!b)
return(NULL);
base = b;
}
//size_of_whole_block->size += b->size;
head = b;
return (b-> data);
}
示例13: malloc
void* malloc(size_t size)
{
block_t* block;
if(size <= 0)
return NULL;
unsigned char index;
size = adjust_size(size, &index);
if(index > K_VALUE)
return NULL;
if(!global_memory){ /*first time*/
global_memory = sbrk(1<<K_VALUE);
if(global_memory == (void*)-1)
return NULL;
block = global_memory;
block->reserved = 0;
block->kval = K_VALUE;
block->succ = NULL;
block->pred = NULL;
free_list[K_VALUE] = block;
}
block = reserve_first(index);
if(block)
return (block + 1);
unsigned char new_index = index;
while(!free_list[new_index] ){
++new_index;
if(new_index > K_VALUE)
return NULL;
}
while(new_index > index){
block = remove_first(new_index);
block->reserved = 0;
block->succ = NULL;
block->pred = NULL;
block_t* new_block = split_block(block);
--new_index;
block->succ = new_block;
new_block->pred = block;
free_list[new_index] = block;
}
block = reserve_first(index);
return (block+1);
}
示例14: ALIGN
void *mm_malloc(size_t size) {
//The malloc routine which allocates free blocks
//if we are given zero size, then there's nothing to malloc!
if (size == 0) return NULL ;
int new_size = ALIGN(size + BLK_HDR_FTR_SIZE);
//check if there's a free block that will contain that size
blockHdr *free_block = find_fit(new_size);
if (free_block == NULL ) {
//if there's no free block, then create one!
//But keep track of epilogue before calling sbrk()!
blockHdr *epilogue = GET_EPILOGUE;
epilogue->size = BLK_HDR_SIZE;
//call sbrk to get more space
free_block = mem_sbrk(new_size + BLK_HDR_SIZE);
//if there's an error return NULL
if (free_block <= 0)
return NULL ;
//get the start of the freeblock
free_block = (blockHdr *) ((char *) free_block - epilogue->size);
//free_block size taking epilogue into account
free_block->size = ((epilogue->size) + new_size) | 1;
//now set the footer size of the newly created block
blockFtr *ftr = (blockFtr *) ((char *) free_block - BLK_FTR_SIZE + ((free_block->size) & ~1));
ftr->size = free_block->size;
//adjust the epilogue
epilogue = GET_EPILOGUE;
epilogue->next_p = epilogue;
epilogue->prior_p = epilogue;
epilogue->size = BLK_HDR_SIZE | 1;
} else {
//otherwise, use the free block!
//if there's too much space, split the space and put remainder in appropriate free list
free_block = split_block(free_block->size, new_size, free_block);
//use the space you now have
free_block->size |= 1;
blockFtr *ftr = (blockFtr *) ((char *) free_block - BLK_FTR_SIZE + ((free_block->size) & ~1));
ftr->size |= 1; //set footer to allocated
//and remove the free block from the doubly linked list
remove_from_free_lists(free_block);
}
return (void *) ((char *) free_block + BLK_HDR_SIZE);
}
示例15: split_block_and_df_analyze
static basic_block
split_block_and_df_analyze (basic_block bb, rtx insn)
{
basic_block next;
next = split_block (bb, insn)->dest;
df_analyze ();
return next;
}