From 9132f9ebe8b46728a1515cf351c9fea5d6ea142c Mon Sep 17 00:00:00 2001 From: Dmitry Simonenko Date: Tue, 13 Feb 2018 17:14:22 +0300 Subject: [PATCH] shapito: add cache count and stream size limits --- shapito/cache.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/shapito/cache.h b/shapito/cache.h index 801a1e21..a5aee686 100644 --- a/shapito/cache.h +++ b/shapito/cache.h @@ -14,6 +14,8 @@ struct shapito_cache pthread_spinlock_t lock; int count; int count_allocated; + int limit; + int limit_size; shapito_stream_t *list; }; @@ -23,6 +25,8 @@ shapito_cache_init(shapito_cache_t *cache) cache->list = NULL; cache->count = 0; cache->count_allocated = 0; + cache->limit = 100; + cache->limit_size = 10 * 1024; pthread_spin_init(&cache->lock, PTHREAD_PROCESS_PRIVATE); } @@ -39,6 +43,18 @@ shapito_cache_free(shapito_cache_t *cache) pthread_spin_destroy(&cache->lock); } +static inline void +shapito_cache_set_limit(shapito_cache_t *cache, int limit) +{ + cache->limit = limit; +} + +static inline void +shapito_cache_set_limit_size(shapito_cache_t *cache, int size) +{ + cache->limit_size = size; +} + static inline shapito_stream_t* shapito_cache_pop(shapito_cache_t *cache) { @@ -72,6 +88,13 @@ static inline void shapito_cache_push(shapito_cache_t *cache, shapito_stream_t *stream) { pthread_spin_lock(&cache->lock); + if (cache->limit == cache->count || + cache->limit_size <= shapito_stream_size(stream)) { + pthread_spin_unlock(&cache->lock); + shapito_stream_free(stream); + free(stream); + return; + } stream->next = cache->list; cache->list = stream; cache->count++;