-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cache: Add reference counter to caches
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
Showing
3 changed files
with
30 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> | ||
*/ | ||
|
||
/** | ||
|
@@ -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)); | ||
|
||
|
@@ -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. | ||
|
@@ -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); | ||
} | ||
|
||
/** @} */ | ||
|