[libbluray-devel] [Git][videolan/libbluray][master] 2 commits: Add refcnt_calloc()
Petri Hintukainen
gitlab at videolan.org
Sat Jan 30 17:42:40 UTC 2021
Petri Hintukainen pushed to branch master at VideoLAN / libbluray
Commits:
1763ead9 by hpi1 at 2021-01-30T19:32:43+02:00
Add refcnt_calloc()
- - - - -
31672e48 by hpi1 at 2021-01-30T19:38:31+02:00
disc: add object cache
- - - - -
4 changed files:
- src/libbluray/disc/disc.c
- src/libbluray/disc/disc.h
- src/util/refcnt.c
- src/util/refcnt.h
Changes:
=====================================
src/libbluray/disc/disc.c
=====================================
@@ -26,6 +26,7 @@
#include "dec.h"
#include "properties.h"
+#include "util/refcnt.h"
#include "util/logging.h"
#include "util/macro.h"
#include "util/mutex.h"
@@ -58,6 +59,14 @@ struct bd_disc {
char *properties_file; /* NULL if not yet used */
int8_t avchd; /* -1 - unknown. 0 - no. 1 - yes */
+
+ /* disc cache */
+ BD_MUTEX cache_mutex;
+ size_t cache_size;
+ struct {
+ char name[11];
+ const void *data;
+ } *cache;
};
/*
@@ -269,6 +278,7 @@ static BD_DISC *_disc_init()
if (p) {
bd_mutex_init(&p->ovl_mutex);
bd_mutex_init(&p->properties_mutex);
+ bd_mutex_init(&p->cache_mutex);
/* default file access functions */
p->fs_handle = (void*)p;
@@ -354,8 +364,11 @@ void disc_close(BD_DISC **pp)
p->pf_fs_close(p->fs_handle);
}
+ disc_cache_clean(p, NULL);
+
bd_mutex_destroy(&p->ovl_mutex);
bd_mutex_destroy(&p->properties_mutex);
+ bd_mutex_destroy(&p->cache_mutex);
X_FREE(p->disc_root);
X_FREE(p->properties_file);
@@ -823,3 +836,105 @@ int disc_pseudo_id(BD_DISC *p, uint8_t *id/*[20]*/)
return r > 0;
}
+
+/*
+ *
+ */
+
+const void *disc_cache_get(BD_DISC *p, const char *name)
+{
+ const void *data = NULL;
+
+ bd_mutex_lock(&p->cache_mutex);
+ if (p->cache) {
+ size_t i;
+ for (i = 0; p->cache[i].data; i++) {
+ if (!strcmp(p->cache[i].name, name)) {
+ data = refcnt_inc(p->cache[i].data);
+ break;
+ }
+ }
+ }
+ bd_mutex_unlock(&p->cache_mutex);
+
+ return data;
+}
+
+void disc_cache_put(BD_DISC *p, const char *name, const void *data)
+{
+ if (strlen(name) >= sizeof(p->cache[0].name)) {
+ BD_DEBUG(DBG_FILE|DBG_CRIT, "disc_cache_put: key %s too large\n", name);
+ return;
+ }
+ if (!data) {
+ BD_DEBUG(DBG_FILE|DBG_CRIT, "disc_cache_put: NULL for key %s ignored\n", name);
+ return;
+ }
+
+ bd_mutex_lock(&p->cache_mutex);
+
+ if (!p->cache) {
+ p->cache_size = 128;
+ p->cache = calloc(p->cache_size, sizeof(*p->cache));
+ }
+
+ if (p->cache && p->cache[p->cache_size - 2].data) {
+ void *tmp = realloc(p->cache, 2 * p->cache_size * sizeof(p->cache[0]));
+ if (tmp) {
+ p->cache = tmp;
+ memset(&p->cache[p->cache_size], 0, p->cache_size * sizeof(p->cache[0]));
+ p->cache_size *= 2;
+ }
+ }
+
+ if (p->cache && !p->cache[p->cache_size - 2].data) {
+ size_t i;
+ for (i = 0; p->cache[i].data; i++) {
+ if (!strcmp(p->cache[i].name, name)) {
+ BD_DEBUG(DBG_FILE|DBG_CRIT, "disc_cache_put(): duplicate key %s\n", name);
+ refcnt_dec(p->cache[i].data);
+ break;
+ }
+ }
+ strcpy(p->cache[i].name, name);
+ p->cache[i].data = refcnt_inc(data);
+ if (p->cache[i].data) {
+ BD_DEBUG(DBG_FILE, "disc_cache_put: added %s (%p)\n", name, data);
+ } else {
+ BD_DEBUG(DBG_FILE|DBG_CRIT, "disc_cache_put: error adding %s (%p): Invalid object type\n", name, data);
+ }
+ } else {
+ BD_DEBUG(DBG_FILE|DBG_CRIT, "disc_cache_put: error adding %s (%p): Out of memory\n", name, data);
+ }
+
+ bd_mutex_unlock(&p->cache_mutex);
+}
+
+void disc_cache_clean(BD_DISC *p, const char *name)
+{
+ bd_mutex_lock(&p->cache_mutex);
+
+ if (p->cache) {
+ size_t i;
+ if (name == NULL) {
+ for (i = 0; p->cache[i].data; i++) {
+ refcnt_dec(p->cache[i].data);
+ }
+ X_FREE(p->cache);
+ p->cache_size = 0;
+ } else {
+ for (i = 0; p->cache[i].data; i++) {
+ if (!strcmp(p->cache[i].name, name)) {
+ BD_DEBUG(DBG_FILE, "disc_cache_clean: dropped %s (%p)\n", name, p->cache[i].data);
+ refcnt_dec(p->cache[i].data);
+ break;
+ }
+ }
+ for (; p->cache[i].data; i++) {
+ p->cache[i] = p->cache[i + 1];
+ }
+ }
+ }
+
+ bd_mutex_unlock(&p->cache_mutex);
+}
=====================================
src/libbluray/disc/disc.h
=====================================
@@ -124,5 +124,16 @@ enum {
BD_PRIVATE void disc_event(BD_DISC *, uint32_t event, uint32_t param);
+/*
+ * cache
+ *
+ * Cache can hold any reference-counted objects (= allocated with refcnt_*).
+ *
+ */
+
+BD_PRIVATE const void *disc_cache_get(BD_DISC *, const char *key);
+BD_PRIVATE void disc_cache_put(BD_DISC *, const char *key, const void *data);
+BD_PRIVATE void disc_cache_clean(BD_DISC *, const char *key); /* NULL key == drop all */
+
#endif /* _BD_DISC_H_ */
=====================================
src/util/refcnt.c
=====================================
@@ -108,6 +108,15 @@ void refcnt_dec(const void *obj)
free(ref);
}
+void *refcnt_calloc(size_t sz, void (*cleanup)(void *))
+{
+ void *p = refcnt_realloc(NULL, sz, cleanup);
+ if (p) {
+ memset(p, 0, sz);
+ }
+ return p;
+}
+
void *refcnt_realloc(void *obj, size_t sz, void (*cleanup)(void *))
{
sz += sizeof(BD_REFCNT);
=====================================
src/util/refcnt.h
=====================================
@@ -50,6 +50,7 @@ extern "C" {
*
*/
+BD_PRIVATE void *refcnt_calloc(size_t sz, void (*cleanup)(void *));
BD_PRIVATE void *refcnt_realloc(void *obj, size_t sz, void (*cleanup)(void *));
BD_PRIVATE const void *refcnt_inc(const void *obj) BD_USED;
View it on GitLab: https://code.videolan.org/videolan/libbluray/-/compare/c8c6de162afa8c22d276e2e462bb28aec778279f...31672e488749da3b230e169c07a4f2ee01f7b64b
--
View it on GitLab: https://code.videolan.org/videolan/libbluray/-/compare/c8c6de162afa8c22d276e2e462bb28aec778279f...31672e488749da3b230e169c07a4f2ee01f7b64b
You're receiving this email because of your account on code.videolan.org.
More information about the libbluray-devel
mailing list