Skip to content

Commit

Permalink
cache: Add reference counter to caches
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Graf <[email protected]>
(cherry picked from commit c658a6e)

Conflicts:
	include/netlink-types.h
	lib/cache.c

Signed-off-by: Thomas Graf <[email protected]>
  • Loading branch information
tgraf committed Dec 20, 2012
1 parent 3ddde65 commit 349a87f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/netlink-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ struct nl_cache
int c_nitems;
int c_iarg1;
int c_iarg2;
int c_refcnt;
struct nl_cache_ops * c_ops;
};

Expand Down
1 change: 1 addition & 0 deletions include/netlink/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ extern struct nl_cache * nl_cache_alloc_name(const char *);
extern struct nl_cache * nl_cache_subset(struct nl_cache *,
struct nl_object *);
extern void nl_cache_clear(struct nl_cache *);
extern void nl_cache_get(struct nl_cache *);
extern void nl_cache_free(struct nl_cache *);

/* Cache modification */
Expand Down
32 changes: 28 additions & 4 deletions lib/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* Copyright (c) 2003-2006 Thomas Graf <[email protected]>
* Copyright (c) 2003-2012 Thomas Graf <[email protected]>
*/

/**
Expand Down Expand Up @@ -179,6 +179,7 @@ struct nl_cache *nl_cache_alloc(struct nl_cache_ops *ops)

nl_init_list_head(&cache->c_items);
cache->c_ops = ops;
cache->c_refcnt = 1;

NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));

Expand Down Expand Up @@ -248,6 +249,23 @@ void nl_cache_clear(struct nl_cache *cache)
nl_cache_remove(obj);
}

static void __nl_cache_free(struct nl_cache *cache)
{
nl_cache_clear(cache);

NL_DBG(1, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
free(cache);
}

/**
* Increase reference counter of cache
* @arg cache Cache
*/
void nl_cache_get(struct nl_cache *cache)
{
cache->c_refcnt++;
}

/**
* Free a cache.
* @arg cache Cache to free.
Expand All @@ -258,9 +276,15 @@ void nl_cache_clear(struct nl_cache *cache)
*/
void nl_cache_free(struct nl_cache *cache)
{
nl_cache_clear(cache);
NL_DBG(1, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
free(cache);
if (!cache)
return;

cache->c_refcnt--;
NL_DBG(4, "Returned cache reference %p, %d remaining\n",
cache, cache->c_refcnt);

if (cache->c_refcnt <= 0)
__nl_cache_free(cache);
}

/** @} */
Expand Down

0 comments on commit 349a87f

Please sign in to comment.