/*----------------------------------------------------------------------------- * Find a block via client ptr, return NULL if not found *----------------------------------------------------------------------------*/ static MemBlock *find_block_no_warn( void *memptr ) { MemBlock *block = BLOCK_PTR( memptr ); if ( block->signature == MEMBLOCK_SIGN ) return block; else return NULL; }
void print_dir_entries(const struct ext2_inode * inode) { unsigned int i, j, k; struct ext2_dir_entry_2 * dir_entry; for (i = 0; i < 12; i++) { if (inode->i_block[i]) { print_list_names(BLOCK_PTR(inode->i_block[i])); } } }
void BasicMemoryPool::linkChunksAndBlocks_(){ const unsigned int chunksNum = memSize_/CHUNK_SIZE_B; for(int i = 0; i < chunksNum; i++){ CHUNK_PTR temp = CHUNK_PTR( new CHUNK ); temp->base = mem_.get() + i * CHUNK_SIZE_B; temp->isAlloc = false; chunks_.push_back( temp ); } BLOCK_PTR initBlock = BLOCK_PTR( new BLOCK ); initBlock->baseChunkIndex = 0; initBlock->length = chunksNum; initBlock->isAlloc = false; blocks_.push_back( initBlock ); }
void *best_fit_realloc(t_ctx *ctx, void *p, size_t size) { t_mblk *block; void *np; (void)ctx; if (!p) return (ctx->fn.malloc(ctx, size)); if (!best_fit_valid_pointer(ctx, p)) return (NULL); size = ALIGN4(size); block = BLOCK_PTR(p); if (check_extend_block(ctx, block, size)) return (extend_block(ctx, block, size)); np = ctx->fn.malloc(ctx, size); memcpy(np, p, MIN(block->size, size)); ctx->fn.free(ctx, p); return (np); }
bool best_fit_valid_pointer(t_ctx *ctx, void *p) { t_mblk *block; if (!ctx->root) { errno = ENOMEM; return (false); } if (!(p > (void *)ctx->root && p < sbrk(0))) { errno = ENOMEM; return (false); } block = BLOCK_PTR(p); if (block->magic != FT_MALLOC_MAGIC) return (false); return (true); }
void* malloc(size_t nbytes) { if (nbytes == 0) return NULL; /* round number of units up to block size */ size_t nunits = BLOCK_ALIGN(nbytes); Header *prevp; if ((prevp = freep) == NULL) { /* initialize free segment */ Header *e = (Header *) MEMLOC; e->next = freep = prevp = &base; e->size = ALLOCSIZ; base.next = e; base.size = 0; } Header *p; for (p = prevp->next; ; prevp = p, p = p->next) { /* check block size */ if (p->size >= nunits) { if (p->size == nunits) { /* exact */ prevp->next = p->next; } else { /* allocate tail end */ p->size -= nunits; p += p->size; p->size = nunits; } /* move free pointer */ freep = prevp; return BLOCK_PTR(p); } if (p == freep) { /* wrapped around free list; no blocks left */ return NULL; } } }
void * BasicMemoryPool::newMemory(const unsigned int size, const UNIT u){ const unsigned int sizeb = sizeInByte_(size, u); const unsigned int chunkNum = sizeb % CHUNK_SIZE_B == 0 ? sizeb/CHUNK_SIZE_B : sizeb/CHUNK_SIZE_B + 1; std::list<BLOCK_PTR>::iterator blockIter = findMemoryBlock(sizeb); if( blockIter == blocks_.end() ){ std::cerr << "find not suitable block. " << std::endl; return NULL; } else{ BLOCK_PTR block = *blockIter; // split block into 2 blocks if necessary if( block->length * CHUNK_SIZE_B > sizeb ){ BLOCK_PTR newblock = BLOCK_PTR(new BLOCK); newblock->isAlloc = true; newblock->length = chunkNum; newblock->baseChunkIndex = block->baseChunkIndex; for(int i = 0; i < newblock->length; i++) chunks_[ newblock->baseChunkIndex + i ]->isAlloc = true; blocks_.insert(blockIter, newblock); block->isAlloc = false; block->length -= newblock->length; block->baseChunkIndex += newblock->length; return chunks_[ newblock->baseChunkIndex ]->base; }else{ block->isAlloc = true; for(int i = 0; i < block->length; i++) chunks_[ block->baseChunkIndex + i ]->isAlloc = true; return chunks_[ block->baseChunkIndex ]->base; } } }