avformat/cache: Extend cache entries if possible instead of creating new ones
This reduces the number of cache entries and should significantly reduce memory requirements Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
This commit is contained in:
parent
312151bb9a
commit
7c210c4424
@ -94,14 +94,9 @@ static int add_entry(URLContext *h, const unsigned char *buf, int size)
|
|||||||
Context *c= h->priv_data;
|
Context *c= h->priv_data;
|
||||||
int64_t pos = -1;
|
int64_t pos = -1;
|
||||||
int ret;
|
int ret;
|
||||||
CacheEntry *entry = av_malloc(sizeof(*entry));
|
CacheEntry *entry = NULL, *next[2] = {NULL, NULL};
|
||||||
CacheEntry *entry_ret;
|
CacheEntry *entry_ret;
|
||||||
struct AVTreeNode *node = av_tree_node_alloc();
|
struct AVTreeNode *node = NULL;
|
||||||
|
|
||||||
if (!entry || !node) {
|
|
||||||
ret = AVERROR(ENOMEM);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
//FIXME avoid lseek
|
//FIXME avoid lseek
|
||||||
pos = lseek(c->fd, 0, SEEK_END);
|
pos = lseek(c->fd, 0, SEEK_END);
|
||||||
@ -118,11 +113,26 @@ static int add_entry(URLContext *h, const unsigned char *buf, int size)
|
|||||||
av_log(h, AV_LOG_ERROR, "write in cache failed\n");
|
av_log(h, AV_LOG_ERROR, "write in cache failed\n");
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
c->cache_pos += ret;
|
||||||
|
|
||||||
|
entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);
|
||||||
|
|
||||||
|
if (!entry)
|
||||||
|
entry = next[0];
|
||||||
|
|
||||||
|
if (!entry ||
|
||||||
|
entry->logical_pos + entry->size != c->logical_pos ||
|
||||||
|
entry->physical_pos + entry->size != pos
|
||||||
|
) {
|
||||||
|
entry = av_malloc(sizeof(*entry));
|
||||||
|
node = av_tree_node_alloc();
|
||||||
|
if (!entry || !node) {
|
||||||
|
ret = AVERROR(ENOMEM);
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
entry->logical_pos = c->logical_pos;
|
entry->logical_pos = c->logical_pos;
|
||||||
entry->physical_pos = pos;
|
entry->physical_pos = pos;
|
||||||
entry->size = ret;
|
entry->size = ret;
|
||||||
c->cache_pos = entry->physical_pos + entry->size;
|
|
||||||
|
|
||||||
entry_ret = av_tree_insert(&c->root, entry, cmp, &node);
|
entry_ret = av_tree_insert(&c->root, entry, cmp, &node);
|
||||||
if (entry_ret && entry_ret != entry) {
|
if (entry_ret && entry_ret != entry) {
|
||||||
@ -130,6 +140,8 @@ static int add_entry(URLContext *h, const unsigned char *buf, int size)
|
|||||||
av_log(h, AV_LOG_ERROR, "av_tree_insert failed\n");
|
av_log(h, AV_LOG_ERROR, "av_tree_insert failed\n");
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
} else
|
||||||
|
entry->size += ret;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
fail:
|
fail:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user