Commit 3950ae72 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Implement garbage collection of old sources.

Since this is a rather costly operation, we only do it when we overflow
the source table.
parent b9056d3d
...@@ -27,6 +27,8 @@ THE SOFTWARE. ...@@ -27,6 +27,8 @@ THE SOFTWARE.
#include "babel.h" #include "babel.h"
#include "util.h" #include "util.h"
#include "source.h" #include "source.h"
#include "network.h"
#include "route.h"
struct source srcs[MAXSRCS]; struct source srcs[MAXSRCS];
int numsrcs = 0; int numsrcs = 0;
...@@ -36,7 +38,7 @@ find_source(const unsigned char *a, const unsigned char *p, unsigned char plen, ...@@ -36,7 +38,7 @@ find_source(const unsigned char *a, const unsigned char *p, unsigned char plen,
int create, unsigned short seqno) int create, unsigned short seqno)
{ {
struct source *src; struct source *src;
int i; int i, rc;
for(i = 0; i < numsrcs; i++) { for(i = 0; i < numsrcs; i++) {
if(!srcs[i].valid) if(!srcs[i].valid)
...@@ -54,6 +56,8 @@ find_source(const unsigned char *a, const unsigned char *p, unsigned char plen, ...@@ -54,6 +56,8 @@ find_source(const unsigned char *a, const unsigned char *p, unsigned char plen,
if(!create) if(!create)
return NULL; return NULL;
again:
src = NULL; src = NULL;
for(i = 0; i < numsrcs; i++) { for(i = 0; i < numsrcs; i++) {
...@@ -65,6 +69,9 @@ find_source(const unsigned char *a, const unsigned char *p, unsigned char plen, ...@@ -65,6 +69,9 @@ find_source(const unsigned char *a, const unsigned char *p, unsigned char plen,
if(!src) { if(!src) {
if(numsrcs >= MAXSRCS) { if(numsrcs >= MAXSRCS) {
rc = flush_old_sources();
if(rc)
goto again;
fprintf(stderr, "Too many sources.\n"); fprintf(stderr, "Too many sources.\n");
return NULL; return NULL;
} }
...@@ -136,3 +143,35 @@ update_source(struct source *src, ...@@ -136,3 +143,35 @@ update_source(struct source *src,
} }
src->time = now.tv_sec; src->time = now.tv_sec;
} }
int
flush_old_sources()
{
int i, j, changed;
changed = 0;
for(i = 0; i < numsrcs; i++) {
if(!srcs[i].valid)
continue;
if(srcs[i].time >= now.tv_sec - SOURCE_GC_TIME)
continue;
for(j = 0; j < numroutes; j++) {
if(routes[j].src == &srcs[i])
goto nocando;
}
flush_source(&srcs[i]);
changed = 1;
nocando:
;
}
if(changed) {
while(numsrcs > 0 && !srcs[numsrcs - 1].valid) {
numsrcs--;
VALGRIND_MAKE_MEM_UNDEFINED(&srcs[numsrcs], sizeof(srcs[numsrcs]));
}
}
return changed;
}
...@@ -43,3 +43,4 @@ struct source *find_recent_source(const unsigned char *p, ...@@ -43,3 +43,4 @@ struct source *find_recent_source(const unsigned char *p,
unsigned char plen); unsigned char plen);
void update_source(struct source *src, void update_source(struct source *src,
unsigned short seqno, unsigned short metric); unsigned short seqno, unsigned short metric);
int flush_old_sources(void);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment