Lines Matching refs:iscache
57 int pt_iscache_init(struct pt_image_section_cache *iscache, const char *name) in pt_iscache_init() argument
59 if (!iscache) in pt_iscache_init()
62 memset(iscache, 0, sizeof(*iscache)); in pt_iscache_init()
63 iscache->limit = UINT64_MAX; in pt_iscache_init()
65 iscache->name = dupstr(name); in pt_iscache_init()
66 if (!iscache->name) in pt_iscache_init()
74 errcode = mtx_init(&iscache->lock, mtx_plain); in pt_iscache_init()
83 void pt_iscache_fini(struct pt_image_section_cache *iscache) in pt_iscache_fini() argument
85 if (!iscache) in pt_iscache_fini()
88 (void) pt_iscache_clear(iscache); in pt_iscache_fini()
89 free(iscache->name); in pt_iscache_fini()
93 mtx_destroy(&iscache->lock); in pt_iscache_fini()
98 static inline int pt_iscache_lock(struct pt_image_section_cache *iscache) in pt_iscache_lock() argument
100 if (!iscache) in pt_iscache_lock()
107 errcode = mtx_lock(&iscache->lock); in pt_iscache_lock()
116 static inline int pt_iscache_unlock(struct pt_image_section_cache *iscache) in pt_iscache_unlock() argument
118 if (!iscache) in pt_iscache_unlock()
125 errcode = mtx_unlock(&iscache->lock); in pt_iscache_unlock()
139 static int pt_iscache_expand(struct pt_image_section_cache *iscache) in pt_iscache_expand() argument
144 if (!iscache) in pt_iscache_expand()
147 capacity = iscache->capacity; in pt_iscache_expand()
154 entries = realloc(iscache->entries, target * sizeof(*entries)); in pt_iscache_expand()
158 iscache->capacity = target; in pt_iscache_expand()
159 iscache->entries = entries; in pt_iscache_expand()
163 static int pt_iscache_find_locked(struct pt_image_section_cache *iscache, in pt_iscache_find_locked() argument
169 if (!iscache || !filename) in pt_iscache_find_locked()
172 end = iscache->size; in pt_iscache_find_locked()
179 entry = &iscache->entries[idx]; in pt_iscache_find_locked()
230 static int pt_iscache_lru_prune(struct pt_image_section_cache *iscache, in pt_iscache_lru_prune() argument
236 if (!iscache || !tail) in pt_iscache_lru_prune()
239 limit = iscache->limit; in pt_iscache_lru_prune()
242 pnext = &iscache->lru; in pt_iscache_lru_prune()
250 iscache->used = used - lru->size; in pt_iscache_lru_prune()
267 static int pt_isache_lru_new(struct pt_image_section_cache *iscache, in pt_isache_lru_new() argument
274 if (!iscache) in pt_isache_lru_new()
284 limit = iscache->limit; in pt_isache_lru_new()
301 lru->next = iscache->lru; in pt_isache_lru_new()
302 iscache->lru = lru; in pt_isache_lru_new()
304 used = iscache->used; in pt_isache_lru_new()
309 iscache->used = total; in pt_isache_lru_new()
320 static int pt_iscache_lru_add(struct pt_image_section_cache *iscache, in pt_iscache_lru_add() argument
325 if (!iscache) in pt_iscache_lru_add()
328 pnext = &iscache->lru; in pt_iscache_lru_add()
336 lru->next = iscache->lru; in pt_iscache_lru_add()
337 iscache->lru = lru; in pt_iscache_lru_add()
343 return pt_isache_lru_new(iscache, section); in pt_iscache_lru_add()
351 static int pt_iscache_lru_remove(struct pt_image_section_cache *iscache, in pt_iscache_lru_remove() argument
356 if (!iscache) in pt_iscache_lru_remove()
359 pnext = &iscache->lru; in pt_iscache_lru_remove()
381 static int pt_iscache_lru_resize(struct pt_image_section_cache *iscache, in pt_iscache_lru_resize() argument
388 if (!iscache) in pt_iscache_lru_resize()
391 status = pt_iscache_lru_add(iscache, section); in pt_iscache_lru_resize()
395 lru = iscache->lru; in pt_iscache_lru_resize()
407 if (iscache->limit < memsize) in pt_iscache_lru_resize()
420 used = iscache->used; in pt_iscache_lru_resize()
424 iscache->used = used; in pt_iscache_lru_resize()
426 return (iscache->limit < used) ? 1 : 0; in pt_iscache_lru_resize()
435 static int pt_iscache_lru_clear(struct pt_image_section_cache *iscache) in pt_iscache_lru_clear() argument
440 errcode = pt_iscache_lock(iscache); in pt_iscache_lru_clear()
444 lru = iscache->lru; in pt_iscache_lru_clear()
445 iscache->lru = NULL; in pt_iscache_lru_clear()
446 iscache->used = 0ull; in pt_iscache_lru_clear()
448 errcode = pt_iscache_unlock(iscache); in pt_iscache_lru_clear()
463 pt_iscache_find_section_locked(const struct pt_image_section_cache *iscache, in pt_iscache_find_section_locked() argument
471 if (!iscache || !filename) in pt_iscache_find_section_locked()
475 match = end = iscache->size; in pt_iscache_find_section_locked()
480 entry = &iscache->entries[idx]; in pt_iscache_find_section_locked()
524 int pt_iscache_add(struct pt_image_section_cache *iscache, in pt_iscache_add() argument
532 if (!iscache || !section) in pt_iscache_add()
572 errcode = pt_section_attach(section, iscache); in pt_iscache_add()
576 errcode = pt_iscache_lock(iscache); in pt_iscache_add()
601 match = pt_iscache_find_section_locked(iscache, filename, in pt_iscache_add()
609 if (iscache->size <= match) in pt_iscache_add()
612 entry = &iscache->entries[match]; in pt_iscache_add()
622 errcode = pt_iscache_unlock(iscache); in pt_iscache_add()
626 errcode = pt_section_detach(section, iscache); in pt_iscache_add()
651 errcode = pt_iscache_unlock(iscache); in pt_iscache_add()
657 errcode = pt_section_detach(section, iscache); in pt_iscache_add()
663 errcode = pt_section_attach(sec, iscache); in pt_iscache_add()
669 errcode = pt_iscache_lock(iscache); in pt_iscache_add()
683 errcode = pt_iscache_lru_remove(iscache, section); in pt_iscache_add()
707 if (iscache->capacity <= iscache->size) { in pt_iscache_add()
709 if (iscache->capacity < iscache->size) { in pt_iscache_add()
714 errcode = pt_iscache_expand(iscache); in pt_iscache_add()
719 if (iscache->capacity <= iscache->size) { in pt_iscache_add()
730 idx = iscache->size++; in pt_iscache_add()
732 iscache->entries[idx].section = section; in pt_iscache_add()
733 iscache->entries[idx].laddr = laddr; in pt_iscache_add()
735 errcode = pt_iscache_unlock(iscache); in pt_iscache_add()
742 (void) pt_iscache_unlock(iscache); in pt_iscache_add()
745 (void) pt_section_detach(section, iscache); in pt_iscache_add()
748 (void) pt_iscache_lru_clear(iscache); in pt_iscache_add()
756 int pt_iscache_find(struct pt_image_section_cache *iscache, in pt_iscache_find() argument
762 errcode = pt_iscache_lock(iscache); in pt_iscache_find()
766 isid = pt_iscache_find_locked(iscache, filename, offset, size, laddr); in pt_iscache_find()
768 errcode = pt_iscache_unlock(iscache); in pt_iscache_find()
775 int pt_iscache_lookup(struct pt_image_section_cache *iscache, in pt_iscache_lookup() argument
781 if (!iscache || !section || !laddr) in pt_iscache_lookup()
793 errcode = pt_iscache_lock(iscache); in pt_iscache_lookup()
797 if (iscache->size <= index) in pt_iscache_lookup()
802 entry = &iscache->entries[index]; in pt_iscache_lookup()
809 errcode = pt_iscache_unlock(iscache); in pt_iscache_lookup()
816 int pt_iscache_clear(struct pt_image_section_cache *iscache) in pt_iscache_clear() argument
823 if (!iscache) in pt_iscache_clear()
826 errcode = pt_iscache_lock(iscache); in pt_iscache_clear()
830 entries = iscache->entries; in pt_iscache_clear()
831 end = iscache->size; in pt_iscache_clear()
832 lru = iscache->lru; in pt_iscache_clear()
834 iscache->entries = NULL; in pt_iscache_clear()
835 iscache->capacity = 0; in pt_iscache_clear()
836 iscache->size = 0; in pt_iscache_clear()
837 iscache->lru = NULL; in pt_iscache_clear()
838 iscache->used = 0ull; in pt_iscache_clear()
840 errcode = pt_iscache_unlock(iscache); in pt_iscache_clear()
856 errcode = pt_section_detach(section, iscache); in pt_iscache_clear()
871 struct pt_image_section_cache *iscache; in pt_iscache_alloc() local
873 iscache = malloc(sizeof(*iscache)); in pt_iscache_alloc()
874 if (iscache) in pt_iscache_alloc()
875 pt_iscache_init(iscache, name); in pt_iscache_alloc()
877 return iscache; in pt_iscache_alloc()
880 void pt_iscache_free(struct pt_image_section_cache *iscache) in pt_iscache_free() argument
882 if (!iscache) in pt_iscache_free()
885 pt_iscache_fini(iscache); in pt_iscache_free()
886 free(iscache); in pt_iscache_free()
889 int pt_iscache_set_limit(struct pt_image_section_cache *iscache, uint64_t limit) in pt_iscache_set_limit() argument
894 if (!iscache) in pt_iscache_set_limit()
900 errcode = pt_iscache_lock(iscache); in pt_iscache_set_limit()
904 iscache->limit = limit; in pt_iscache_set_limit()
905 if (limit < iscache->used) in pt_iscache_set_limit()
906 status = pt_iscache_lru_prune(iscache, &tail); in pt_iscache_set_limit()
908 errcode = pt_iscache_unlock(iscache); in pt_iscache_set_limit()
916 const char *pt_iscache_name(const struct pt_image_section_cache *iscache) in pt_iscache_name() argument
918 if (!iscache) in pt_iscache_name()
921 return iscache->name; in pt_iscache_name()
924 int pt_iscache_add_file(struct pt_image_section_cache *iscache, in pt_iscache_add_file() argument
931 if (!iscache || !filename) in pt_iscache_add_file()
934 errcode = pt_iscache_lock(iscache); in pt_iscache_add_file()
938 match = pt_iscache_find_section_locked(iscache, filename, offset, in pt_iscache_add_file()
941 (void) pt_iscache_unlock(iscache); in pt_iscache_add_file()
952 if (match < iscache->size) { in pt_iscache_add_file()
955 entry = &iscache->entries[match]; in pt_iscache_add_file()
957 errcode = pt_iscache_unlock(iscache); in pt_iscache_add_file()
968 (void) pt_iscache_unlock(iscache); in pt_iscache_add_file()
972 errcode = pt_iscache_unlock(iscache); in pt_iscache_add_file()
978 errcode = pt_iscache_unlock(iscache); in pt_iscache_add_file()
989 isid = pt_iscache_add(iscache, section, vaddr); in pt_iscache_add_file()
1002 int pt_iscache_read(struct pt_image_section_cache *iscache, uint8_t *buffer, in pt_iscache_read() argument
1009 if (!iscache || !buffer || !size) in pt_iscache_read()
1012 errcode = pt_iscache_lookup(iscache, §ion, &laddr, isid); in pt_iscache_read()
1050 int pt_iscache_notify_map(struct pt_image_section_cache *iscache, in pt_iscache_notify_map() argument
1058 errcode = pt_iscache_lock(iscache); in pt_iscache_notify_map()
1062 status = pt_iscache_lru_add(iscache, section); in pt_iscache_notify_map()
1064 status = pt_iscache_lru_prune(iscache, &tail); in pt_iscache_notify_map()
1066 errcode = pt_iscache_unlock(iscache); in pt_iscache_notify_map()
1074 int pt_iscache_notify_resize(struct pt_image_section_cache *iscache, in pt_iscache_notify_resize() argument
1082 errcode = pt_iscache_lock(iscache); in pt_iscache_notify_resize()
1086 status = pt_iscache_lru_resize(iscache, section, memsize); in pt_iscache_notify_resize()
1088 status = pt_iscache_lru_prune(iscache, &tail); in pt_iscache_notify_resize()
1090 errcode = pt_iscache_unlock(iscache); in pt_iscache_notify_resize()