Plan 9 from Bell Labs’s /usr/web/sources/contrib/mjl/wip/deluge/bite.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include "deluge.h"


Bite *
bitenew(int n, int piecen, int offset, int length)
{
	Bite *b;

	b = emalloc(sizeof b[0]);
	b->n = n;
	b->piecen = piecen;
	b->offset = offset;
	b->length = length;
	b->next = nil;

	return b;
}

void
bitefree(Bite *b)
{
	free(b);
}

void
biteappend(Bite **bp, Bite *b)
{
	Bite *p;

	b->next = nil;
	if(*bp == nil){
		*bp = b;
		return;
	}

	for(p = *bp; p->next; p = p->next)
		;
	p->next = b;
}

Bite *
bitepop(Bite **bp)
{
	Bite *b;

	if(*bp == nil)
		return nil;
	b = *bp;
	*bp = b->next;
	b->next = nil;
	return b;
}

void
biteprepend(Bite **bp, Bite *b)
{
	Bite *p;

	p = *bp;
	*bp = b;
	b->next = p;
}


Bite *
bitefind(Bite *b, int piecen, int offset, int length)
{
	while(b){
		if(b->piecen == piecen && b->offset == offset && b->length == length)
			return b;
		b = b->next;
	}
	return nil;
}

int
biteremove(Bite **bp, Bite *b)
{
	Bite *p;

	if(*bp == b){
		*bp = b->next;
		b->next = nil;
		return 1;
	}
	for(p = *bp; p->next; p = p->next){
		if(p->next == b){
			p->next = b->next;
			b->next = nil;
			return 1;
		}
	}
	DEBUG(2, "bitremove: could not remove bite piecen=%d n=%d\n", b->piecen, b->n);
	return 0;
}

int
bitehave(Torrent *t, Bite *b)
{
	Piece *pc;

	pc = &t->pieces[b->piecen];

	if(bitget(pc->bites, b->n))
		return 1;
	return 0;
}

int
bitelen(Bite *b)
{
	int i = 0;
	while(b){
		b = b->next;
		i++;
	}
	return i;
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.