From: alex Date: Fri, 4 Mar 2022 22:56:28 +0000 (-0800) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=e7a88ed4d93eed7408e40018e6f78f2ebb309523;p=seeder ... --- diff --git a/Makefile.am b/Makefile.am index 2af3989..65f17a3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -45,6 +45,7 @@ seederd_SOURCES = \ src/opt/set.c \ src/opt/watch.c \ src/opt/worker.c \ + src/pqueue.c \ src/rss/entry.c \ src/rss/footer.c \ src/rss/free.c \ @@ -83,6 +84,7 @@ seederd_SOURCES += \ inc/meta.h \ inc/net.h \ inc/opt.h \ + inc/pqueue.h \ inc/rss.h \ inc/session.h \ inc/setup.h \ diff --git a/inc/pqueue.h b/inc/pqueue.h new file mode 100644 index 0000000..f442908 --- /dev/null +++ b/inc/pqueue.h @@ -0,0 +1,25 @@ +#ifndef __PQUEUE_H_ +#define __PQUEUE_H_ + +#include + +struct priority_queue_entry { + void *p; + int priority; + size_t index; +}; + +struct priority_queue { + struct priority_queue_entry **queue; + size_t size; + + struct hash_map *map; +}; + +int pqueue_access(struct priority_queue*,const void*,size_t); +int pqueue_add(struct priority_queue*,const void*,size_t,void*); +void *pqueue_find(struct priority_queue*,const void*,size_t); +void pqueue_free(struct priority_queue*); +int pqueue_init(struct priority_queue**,size_t); + +#endif diff --git a/src/peer/handshake.c b/src/peer/handshake.c index 24d3135..6325b62 100644 --- a/src/peer/handshake.c +++ b/src/peer/handshake.c @@ -8,12 +8,12 @@ int peer_handshake(int sock, const uint8_t *infohash, size_t infohash_size const assert(HANDSHAKE_INFOHASH_SIZE==infohash_size); assert(PEER_ID_SIZE==peer_id_size); + + if(sock<0) { return -1; } memcpy(&(header[24]),infohash,HANDSHAKE_INFOHASH_SIZE); memcpy(&(header[44]),peer_id,PEER_ID_SIZE); - if(sock<0) { return -1; } - // send header return -1; diff --git a/src/pqueue.c b/src/pqueue.c new file mode 100644 index 0000000..e749de0 --- /dev/null +++ b/src/pqueue.c @@ -0,0 +1,122 @@ +#include + +static int pqueue_swap(struct priority_queue*,size_t,size_t); + +int pqueue_access(struct priority_queue *queue, const void *key, size_t key_size) { + struct priority_queue_entry *p; + size_t index; + + if(NULL==queue) { return -1; } + if((NULL==key)||(0==key_size)) { return -1; } + + p = hashmap_find(queue->map,key,key_size); + if(NULL==p) { return 0; } + + p->priority++; + + index = p->index; + while(index>0) { + if(queue->queue[index-1]->prioritypriority) { + index--; + } + } + + if(p->index!=index) { + pqueue_swap(queue,index,p->index); + } + + return 1; +} + +int pqueue_add(struct priority_queue *queue, const void *key, size_t key_size, void *value) { + struct priority_queue_entry *p; + + if(NULL==queue) { return -1; } + if((NULL==key)||(0==key_size)) { return -1; } + + if(hashmap_find(queue->map,key,key_size)!=NULL) { return 0; } + + if(priority_queue_entry_init(&p)<0) { return -1; } + p->p = value; + + for(size_t i=0;isize;i++) { + if(queue->queue[i]!=NULL) { + queue->queue[i] = p; + p->index = i; + return 1; + } + } + + return -1; +} + +void *pqueue_evict(struct priority_queue *queue, const void *key, size_t key_size) { + struct priority_queue_entry *entry; + void *p; + + entry = hashmap_find(queue->map,key,key_size); + if(entry!=NULL) { + p = entry->p; + + for(size_t i=entry->index;i<(queue->size-1);i++) { + if(NULL==queue->queue[i+1]) { + pqueue_swap(queue,entry->index,i+1); + break; + } + } + + free(entry); + return p; + } + + return NULL; +} + +void *pqueue_find(struct priority_queue *queue, const void *key, size_t key_size) { + return NULL; +} + +void pqueue_free(struct priority_queue *queue) { + if(queue->map!=NULL) { hashmap_free(queue->map); } + if(queue->queue!=NULL) { free(queue->queue); } + + free(queue); +} + +int pqueue_init(struct priority_queue **p, size_t size) { + if(p==NULL) { return -1; } + if(0==size) { return -1; } + + *p = malloc(sizeof(struct priority_queue)); + if(NULL==(*p)) { + perror("malloc"); + return -1; + } + + (*p)->queue = NULL; + (*p)->size = size; + (*p)->map = NULL; + + if(hashmap_init(&((*p)->map),size)<0) { pqueue_free(*p); } + + (*p)->queue = malloc(sizeof(void*)*size); + if(NULL==(*p)->queue) { pqueue_free(*p); } + + for(size_t i=0;iqueue[i] = NULL; + } + + return 1; +} + +static void pqueue_swap(struct priority_queue *queue, size_t a, size_t b) { + struct priority_queue_entry *p; + + queue->queue[a] = p; + + queue->queue[a] = queue->queue[b]; + queue->queue[a]->index = a; + + queue->queue[b] = p; + queue->queue[b]->index = b; +}