void *free_allocation(info_p handler, long n_bytes){
	int full_mem=0;
	
	if(n_bytes < 0){
		printf("Free Allocation Error: requested negative space\n");
		return NULL;
	}
	
	if(handler->n_bytes < n_bytes){
		printf("Free Allocation Error: requested too much space\n");
		return NULL; //User requested more space than permitted
	}
	
	if(n_bytes == handler->n_bytes){
		//printf("request:%lu; bytes: %lu; declaring full memory\n", n_bytes, handler->n_bytes);
		full_mem=1;
	}
	
	if(handler->free_list == NULL){
		return NULL;
	}
	
	void *mem = handler->memptr-4;
	//printf("free list head: %p; size of mem is: %lu\n",handler->free_list, *(long *)mem);
	
	switch(handler->flags){
		case (FREE1_ALLOC): //first fit
			return first_fit(handler, handler->free_list, n_bytes,full_mem);
			break;
		case (FREE2_ALLOC): //next fit
			return next_fit(handler, n_bytes);
			break;
		case (FREE3_ALLOC): //best fit
			return best_fit(handler, n_bytes);
			break;
		case (FREE4_ALLOC): //worst fit
			return worst_fit(handler, n_bytes);
			break;
		default: //should never happen
			return NULL;
	}
	
	printf("Not enough memory to allocate\n");
	return NULL;
}
Esempio n. 2
0
/*
 * @brief Détermine la bonne méthode d'allocation
 *
 * @param kv descripteur d'accès à la base
 * @param key clé
 * @param val valeur
 * @param offset index dans le .kv modifié par effet de bord
 */
int kv_put_dkv(KV *kv, const kv_datum *key, const kv_datum *val, len_t *offset)
{
    if(kv->alloc == 0)
    {
        return first_fit(kv, key, val, offset);
    }
    else if(kv->alloc == 1)
    {
        return worst_fit(kv, key, val, offset);
    }
    else if(kv->alloc == 2)
    {
        return best_fit(kv, key, val, offset);
    }
    else
    {
        errno = EINVAL;
        return -1;
    }
}