common: inlist code refactoring

introduced INLIST_FOREACH() / INLIST_SAFE_FOREACH()
for common implementation
This commit is contained in:
Hermet Park 2024-12-02 14:54:09 +09:00 committed by Hermet Park
parent 1422c4ca5f
commit f33b63abad
3 changed files with 20 additions and 21 deletions

View file

@ -36,6 +36,11 @@ struct Inlist
T* head = nullptr; T* head = nullptr;
T* tail = nullptr; T* tail = nullptr;
~Inlist()
{
free();
}
void free() void free()
{ {
while (head) { while (head) {
@ -106,6 +111,14 @@ struct Inlist
} }
}; };
#define INLIST_FOREACH(inlist, cur) \
for (auto cur = inlist.head; cur; cur = cur->next)
#define INLIST_SAFE_FOREACH(inlist, cur) \
auto cur = inlist.head; \
auto next = cur ? cur->next : nullptr; \
for (; cur; cur = next, next = (cur ? cur->next : nullptr))
} }
#endif // _TVG_INLIST_H_ #endif // _TVG_INLIST_H_

View file

@ -217,8 +217,6 @@ void LottieBuilder::updateGroup(LottieGroup* parent, LottieObject** child, float
contexts.back(new RenderContext(*ctx, propagator, group->mergeable())); contexts.back(new RenderContext(*ctx, propagator, group->mergeable()));
updateChildren(group, frameNo, contexts); updateChildren(group, frameNo, contexts);
contexts.free();
} }

View file

@ -206,14 +206,11 @@ static LoadModule* _findFromCache(const char* filename)
{ {
ScopedLock lock(key); ScopedLock lock(key);
auto loader = _activeLoaders.head; INLIST_FOREACH(_activeLoaders, loader) {
while (loader) {
if (loader->pathcache && !strcmp(loader->hashpath, filename)) { if (loader->pathcache && !strcmp(loader->hashpath, filename)) {
++loader->sharing; ++loader->sharing;
return loader; return loader;
} }
loader = loader->next;
} }
return nullptr; return nullptr;
} }
@ -225,16 +222,13 @@ static LoadModule* _findFromCache(const char* data, uint32_t size, const char* m
if (type == FileType::Unknown) return nullptr; if (type == FileType::Unknown) return nullptr;
ScopedLock lock(key); ScopedLock lock(key);
auto loader = _activeLoaders.head;
auto key = HASH_KEY(data); auto key = HASH_KEY(data);
while (loader) { INLIST_FOREACH(_activeLoaders, loader) {
if (loader->type == type && loader->hashkey == key) { if (loader->type == type && loader->hashkey == key) {
++loader->sharing; ++loader->sharing;
return loader; return loader;
} }
loader = loader->next;
} }
return nullptr; return nullptr;
} }
@ -253,15 +247,12 @@ bool LoaderMgr::init()
bool LoaderMgr::term() bool LoaderMgr::term()
{ {
auto loader = _activeLoaders.head;
//clean up the remained font loaders which is globally used. //clean up the remained font loaders which is globally used.
while (loader && loader->type == FileType::Ttf) { INLIST_SAFE_FOREACH(_activeLoaders, loader) {
if (loader->type != FileType::Ttf) break;
auto ret = loader->close(); auto ret = loader->close();
auto tmp = loader; _activeLoaders.remove(loader);
loader = loader->next; if (ret) delete(loader);
_activeLoaders.remove(tmp);
if (ret) delete(tmp);
} }
return true; return true;
} }
@ -340,14 +331,11 @@ bool LoaderMgr::retrieve(const char* filename)
LoadModule* LoaderMgr::loader(const char* key) LoadModule* LoaderMgr::loader(const char* key)
{ {
auto loader = _activeLoaders.head; INLIST_FOREACH(_activeLoaders, loader) {
while (loader) {
if (loader->pathcache && strstr(loader->hashpath, key)) { if (loader->pathcache && strstr(loader->hashpath, key)) {
++loader->sharing; ++loader->sharing;
return loader; return loader;
} }
loader = loader->next;
} }
return nullptr; return nullptr;
} }