src/page_hlp.c
// TODO: add block after each block creation to optimize space
// maybe use page_mem_left_total
add block after each block creation to optimize space
maybe use page_mem_left_total
add block after each block creation to optimize space maybe use page_mem_left_total
t_block			*page_request_space(t_page *ref, size_t len)
{
	size_t		total;
	size_t		left;
	t_block		*start;
	t_block		*current;
	t_block		*last;
	if (ref == 0)
		return (0);
	total = ref->len - sizeof(t_page);
	left = 0;
	start = 0;
	last = 0;
	current = ref->start;
	while (current)
	{
		if (current->free)
		{
			if (start == 0)
				start = current;
			else
				left += sizeof(t_block);
			left += current->len;
		}
		else if (left >= len && start)
		{
			start->len = len;
			if (current != start)
				start->next = current;
			else
				start->next = 0;
			start->free = 0;
			return (start);
		}
		else
		{
			start = 0;
			left = 0;
		}
		if ((current->len + sizeof(t_block)) >= total)
			total = 0;
		else
			total -= current->len + sizeof(t_block);
		last = current;
		current = current->next;
	}
	if ((left + total) >= len && last)
	{
		start = (t_block *)((char *)last + sizeof(t_block) + last->len);
		start->len = len;
		start->next = 0;
		start->free = 0;
		last->next = start;
		return (start);
	}
	return (0);
}
t_block * page_request_space ( t_page * ref , size_t len ) { size_t total ; size_t left ; t_block * start ; t_block * current ; t_block * last ; if ( ref == 0 ) return ( 0 ) ; total = ref -> len - sizeof ( t_page ) ; left = 0 ; start = 0 ; last = 0 ; current = ref -> start ; while ( current ) { if ( current -> free ) { if ( start == 0 ) start = current ; else left += sizeof ( t_block ) ; left += current -> len ; } else if ( left >= len && start ) { start -> len = len ; if ( current != start ) start -> next = current ; else start -> next = 0 ; start -> free = 0 ; return ( start ) ; } else { start = 0 ; left = 0 ; } if ( ( current -> len + sizeof ( t_block ) ) >= total ) total = 0 ; else total -= current -> len + sizeof ( t_block ) ; last = current ; current = current -> next ; } if ( ( left + total ) >= len && last ) { start = ( t_block * ) ( ( char * ) last + sizeof ( t_block ) + last -> len ) ; start -> len = len ; start -> next = 0 ; start -> free = 0 ; last -> next = start ; return ( start ) ; } return ( 0 ) ; }
TODO: add block after each block creation to optimize space
maybe use page_mem_left_total
TODO : add block after each block creation to optimize space maybe use page_mem_left_total
// TODO: add size of block // TODO: should be called more often // TODO: if left != 0 take block start