void * malloc(size_t size) { if (size == 0) return NULL; // align the size requirement to an 8 bytes boundary size = (size + 7) & ~7; restart: if (size > sAvailable) { // try to enlarge heap if (grow_heap(size) < B_OK) return NULL; } free_chunk *chunk = sFreeAnchor.next, *last = &sFreeAnchor; while (chunk && chunk->Size() < size) { last = chunk; chunk = chunk->next; } if (chunk == NULL) { // could not find a free chunk as large as needed if (grow_heap(size) < B_OK) return NULL; goto restart; } if (chunk->Size() > size + sizeof(free_chunk) + 4) { // if this chunk is bigger than the requested size, // we split it to form two chunks (with a minimal // size of 4 allocatable bytes). free_chunk *freeChunk = chunk->Split(size); last->next = freeChunk; // re-enqueue the free chunk at the correct position freeChunk->Remove(last); freeChunk->Enqueue(); } else { // remove the chunk from the free list last->next = chunk->next; } sAvailable -= size + sizeof(uint32); return chunk->AllocatedAddress(); }
void* __malloc_internal(size_t size) { size_t rounded_size; size_t shifted_size; void *address; /* Round size to a power of two */ rounded_size = MINIMUM_BLOCK_SIZE; for (shifted_size = (size + MALLOCED_HEADER_SIZE) / MINIMUM_BLOCK_SIZE; shifted_size; shifted_size >>= 1) rounded_size <<= 1; assert(rounded_size >= size + MALLOCED_HEADER_SIZE); address = find_free_block(rounded_size); if (address == 0) { grow_heap(rounded_size); address = find_free_block(rounded_size); if (address == 0) return 0; } memset(address, ALLOC_FILL, rounded_size - MALLOCED_HEADER_SIZE); allocated_blocks++; total_allocations++; total_allocated += rounded_size; return address; }
static void insert_node(pj_timer_heap_t *ht, pj_timer_entry *new_node) { if (ht->cur_size + 2 >= ht->max_size) grow_heap(ht); reheap_up( ht, new_node, ht->cur_size, HEAP_PARENT(ht->cur_size)); ht->cur_size++; }
/* Return 0 if success, otherwise -1 is returned */ int heap_push(heap_t heap, void *elem) { if (heap == NULL || elem == NULL) return -1; if (heap->curr == heap->avail && grow_heap(heap)) return -1; heap_set(heap, ++heap->curr, elem); bubble_up(heap, heap->curr); return 0; }
/* simple allocator to carve allocations out of an up-front allocated heap, * so that we can free everything easily in one shot. */ void * ir3_alloc(struct ir3 *shader, int sz) { void *ptr; sz = align(sz, 4) / 4; if ((shader->heap_idx + sz) > CHUNK_SZ) grow_heap(shader); ptr = &shader->chunk->heap[shader->heap_idx]; shader->heap_idx += sz; return ptr; }
implicit_node* pq_insert( implicit_heap *queue, item_type item, key_type key ) { implicit_node *node = pq_alloc_node( queue->map, 0 ); ITEM_ASSIGN( node->item, item ); node->key = key; node->index = queue->size++; #ifndef USE_EAGER if( queue->size == queue->capacity ) grow_heap( queue ); #endif queue->nodes[node->index] = node; heapify_up( queue, node ); return node; }
int ast_heap_push(struct ast_heap *h, void *elm) #endif { if (h->cur_len == h->avail_len && grow_heap(h #ifdef MALLOC_DEBUG , file, lineno, func #endif )) { return -1; } heap_set(h, ++(h->cur_len), elm); bubble_up(h, h->cur_len); return 0; }
/// <summary> /// Does what you'd expect the malloc C standard library to do, check /// the requirements document for more details. /// <summary> /// <param name='size'> How many bytes the user needs to allocate </param> /// <return> /// A properely aligned pointer to a block of memory whose size at /// is at least equal to the size requested by the caller. /// </return> void *my_malloc(size_t size) { uint8_t *user_data; /* The pointer we will return to the user */ uint8_t *new_block; /* Used to point to the block added by grow_heap */ uint8_t *sliced_block; /* Used to point to the left-over of a block */ struct block_header *header, *footer, *slice_header; int slice_list;/* Which list the slice belongs to */ if(size <= 0) return NULL; size += sizeof(struct block_header) * 2; /* The header+footer */ size = (size+7) & ~7;/* Align the size to 8-byte boundary */ /* Identify which list to pick from */ int list_num = pick_list(size); DEBUG_PRINT("size requested: %zd\n", size); DEBUG_PRINT("List picked: %d\n", list_num); user_data = extract_free_block(list_num, size); /* If no list had enough space */ if(user_data == NULL) { if((new_block = grow_heap(size)) == NULL) { DEBUG_PRINT("%s\n", "-------------------------------------"); errno = ENOMEM; return NULL; } add_free_block_to_list(list_num, new_block); user_data = extract_free_block(list_num, size); } assert(user_data != NULL); DEBUG_PRINT("%s\n", "Found a block!"); /* If we can slice, add the left-over back to our lists */ sliced_block = slice_block(user_data, size); if(sliced_block != NULL) { slice_header = (struct block_header *) sliced_block; slice_list = pick_list(slice_header->block_size); add_free_block_to_list(slice_list, sliced_block); } /* Mark the block as allocated */ header = (struct block_header *) user_data; SET_ALLOC(header); footer = get_footer(user_data); SET_ALLOC(footer); user_data = user_data + 8;/* Now points past the header */ DEBUG_PRINT("%s\n", "-------------------------------------"); return user_data; }
struct ir3 * ir3_create(struct ir3_compiler *compiler, unsigned nin, unsigned nout) { struct ir3 *shader = calloc(1, sizeof(struct ir3)); grow_heap(shader); shader->compiler = compiler; shader->ninputs = nin; shader->inputs = ir3_alloc(shader, sizeof(shader->inputs[0]) * nin); shader->noutputs = nout; shader->outputs = ir3_alloc(shader, sizeof(shader->outputs[0]) * nout); list_inithead(&shader->block_list); list_inithead(&shader->array_list); return shader; }
int ast_heap_push(struct ast_heap *h, void *elm) #endif { int i; if (h->cur_len == h->avail_len && grow_heap(h #ifdef MALLOC_DEBUG , file, lineno, func #endif )) { return -1; } heap_set(h, ++(h->cur_len), elm); for (i = h->cur_len; i > 1 && h->cmp_fn(heap_get(h, parent_node(i)), heap_get(h, i)) < 0; i = parent_node(i)) { heap_swap(h, i, parent_node(i)); } return 0; }