core/ui/font_slots.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file font_slots.c | ||
| 3 | * @brief The decisions behind the font registry: who owns each slot's font, when a swap has | ||
| 4 | * nothing to do, and when a font can be freed. | ||
| 5 | * | ||
| 6 | * @ingroup lib_core | ||
| 7 | */ | ||
| 8 | #include "ui/font_slots.h" | ||
| 9 | |||
| 10 | #include <stddef.h> | ||
| 11 | |||
| 12 | /** | ||
| 13 | * @brief Whether a slot id is inside the tables. | ||
| 14 | * | ||
| 15 | * @param slots The registry's tables. | ||
| 16 | * @param id The slot. | ||
| 17 | * @return true when the tables hold that slot. | ||
| 18 | */ | ||
| 19 | 51 | static bool slot_in_range(const FontSlots *slots, int id) | |
| 20 | { | ||
| 21 |
4/4✓ Branch 2 → 3 taken 50 times.
✓ Branch 2 → 5 taken 1 time.
✓ Branch 3 → 4 taken 49 times.
✓ Branch 3 → 5 taken 1 time.
|
51 | return id >= 0 && id < slots->count; |
| 22 | } | ||
| 23 | |||
| 24 | 8 | void font_slots_put(const FontSlots *slots, int id, void *handle, bool owned) | |
| 25 | { | ||
| 26 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 8 times.
|
8 | if (!slot_in_range(slots, id)) |
| 27 | { | ||
| 28 | ✗ | return; | |
| 29 | } | ||
| 30 | |||
| 31 | 8 | slots->handles[id] = handle; | |
| 32 | 8 | slots->states[id] = owned ? FONT_SLOT_OWNED : FONT_SLOT_UNOWNED; | |
| 33 | } | ||
| 34 | |||
| 35 | 25 | bool font_slots_load(const FontSlots *slots, uint32_t *resources, int id, uint32_t resource, | |
| 36 | FontSlotLoad load) | ||
| 37 | { | ||
| 38 | // checked before the load, since a font loaded for a slot that does not exist could never be freed | ||
| 39 |
2/2✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 24 times.
|
25 | if (!slot_in_range(slots, id)) |
| 40 | { | ||
| 41 | 1 | return false; | |
| 42 | } | ||
| 43 | |||
| 44 | 24 | void *handle = load(resource); | |
| 45 | 24 | slots->handles[id] = handle; | |
| 46 |
2/2✓ Branch 6 → 7 taken 22 times.
✓ Branch 6 → 8 taken 2 times.
|
24 | slots->states[id] = handle ? FONT_SLOT_LOADED : FONT_SLOT_UNOWNED; |
| 47 | 24 | resources[id] = resource; | |
| 48 | 24 | return handle != NULL; | |
| 49 | } | ||
| 50 | |||
| 51 | /** | ||
| 52 | * @brief Lets go of the font in a slot before the slot takes another, freeing it if nothing else | ||
| 53 | * holds it. | ||
| 54 | * | ||
| 55 | * Only slots the app owns count as holding a font for this. A copy put in as a system font would | ||
| 56 | * otherwise keep it alive with nobody left to free it, so ownership moves to the first slot that | ||
| 57 | * held it as a system font instead. That slot then frees it like any font the app owns. Registering | ||
| 58 | * over that slot afterwards leaves the font it took over loaded, as registering over any owned slot | ||
| 59 | * does, so nothing frees it until the face does. | ||
| 60 | * | ||
| 61 | * @param slots The registry's tables. | ||
| 62 | * @param id The slot letting go. | ||
| 63 | * @param unload The call that frees a font. | ||
| 64 | */ | ||
| 65 | 15 | static void let_go(const FontSlots *slots, int id, FontSlotUnload unload) | |
| 66 | { | ||
| 67 | 15 | void *old = slots->handles[id]; | |
| 68 |
4/4✓ Branch 2 → 3 taken 8 times.
✓ Branch 2 → 4 taken 7 times.
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 7 times.
|
15 | if (!old || slots->states[id] == FONT_SLOT_UNOWNED) |
| 69 | { | ||
| 70 | 8 | return; | |
| 71 | } | ||
| 72 | |||
| 73 | 7 | int heir = -1; | |
| 74 |
2/2✓ Branch 14 → 6 taken 26 times.
✓ Branch 14 → 15 taken 6 times.
|
32 | for (int i = 0; i < slots->count; i++) |
| 75 | { | ||
| 76 |
4/4✓ Branch 6 → 7 taken 19 times.
✓ Branch 6 → 8 taken 7 times.
✓ Branch 7 → 8 taken 17 times.
✓ Branch 7 → 9 taken 2 times.
|
26 | if (i == id || slots->handles[i] != old) |
| 77 | { | ||
| 78 | 24 | continue; | |
| 79 | } | ||
| 80 | |||
| 81 |
2/2✓ Branch 9 → 10 taken 1 time.
✓ Branch 9 → 11 taken 1 time.
|
2 | if (slots->states[i] != FONT_SLOT_UNOWNED) |
| 82 | { | ||
| 83 | // another owner holds it and frees it later | ||
| 84 | 1 | return; | |
| 85 | } | ||
| 86 | |||
| 87 |
1/2✓ Branch 11 → 12 taken 1 time.
✗ Branch 11 → 13 not taken.
|
1 | if (heir < 0) |
| 88 | { | ||
| 89 | 1 | heir = i; | |
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 |
2/2✓ Branch 15 → 16 taken 1 time.
✓ Branch 15 → 17 taken 5 times.
|
6 | if (heir >= 0) |
| 94 | { | ||
| 95 | 1 | slots->states[heir] = FONT_SLOT_OWNED; | |
| 96 | 1 | return; | |
| 97 | } | ||
| 98 | |||
| 99 | 5 | unload(old); | |
| 100 | } | ||
| 101 | |||
| 102 | 18 | bool font_slots_swap(const FontSlots *slots, uint32_t *resources, int id, uint32_t resource, | |
| 103 | FontSlotLoad load, FontSlotUnload unload) | ||
| 104 | { | ||
| 105 |
2/2✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 17 times.
|
18 | if (!slot_in_range(slots, id)) |
| 106 | { | ||
| 107 | 1 | return false; | |
| 108 | } | ||
| 109 | |||
| 110 | // only a slot whose last write was a load records a resource, so a font put in by any other | ||
| 111 | // write, or a freed one whose address came back, never counts as already loaded | ||
| 112 |
6/6✓ Branch 5 → 6 taken 10 times.
✓ Branch 5 → 9 taken 7 times.
✓ Branch 6 → 7 taken 7 times.
✓ Branch 6 → 9 taken 3 times.
✓ Branch 7 → 8 taken 2 times.
✓ Branch 7 → 9 taken 5 times.
|
17 | if (slots->handles[id] && slots->states[id] == FONT_SLOT_LOADED && resources[id] == resource) |
| 113 | { | ||
| 114 | 2 | return true; | |
| 115 | } | ||
| 116 | |||
| 117 | 15 | let_go(slots, id, unload); | |
| 118 | 15 | return font_slots_load(slots, resources, id, resource, load); | |
| 119 | } | ||
| 120 | |||
| 121 | 6 | void font_slots_unload_all(const FontSlots *slots, FontSlotUnload unload) | |
| 122 | { | ||
| 123 |
2/2✓ Branch 12 → 3 taken 24 times.
✓ Branch 12 → 13 taken 6 times.
|
30 | for (int i = 0; i < slots->count; i++) |
| 124 | { | ||
| 125 | 24 | void *handle = slots->handles[i]; | |
| 126 |
4/4✓ Branch 3 → 4 taken 9 times.
✓ Branch 3 → 11 taken 15 times.
✓ Branch 4 → 5 taken 8 times.
✓ Branch 4 → 11 taken 1 time.
|
24 | if (handle && slots->states[i] != FONT_SLOT_UNOWNED) |
| 127 | { | ||
| 128 | 8 | unload(handle); | |
| 129 | |||
| 130 | // one handle can sit in several slots, so clear every copy of it before moving on | ||
| 131 | // or the next slot holding it would unload it a second time | ||
| 132 |
2/2✓ Branch 10 → 7 taken 30 times.
✓ Branch 10 → 11 taken 8 times.
|
38 | for (int j = i; j < slots->count; j++) |
| 133 | { | ||
| 134 |
2/2✓ Branch 7 → 8 taken 10 times.
✓ Branch 7 → 9 taken 20 times.
|
30 | if (slots->handles[j] == handle) |
| 135 | { | ||
| 136 | 10 | slots->handles[j] = NULL; | |
| 137 | 10 | slots->states[j] = FONT_SLOT_UNOWNED; | |
| 138 | } | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | 24 | slots->handles[i] = NULL; | |
| 143 | 24 | slots->states[i] = FONT_SLOT_UNOWNED; | |
| 144 | } | ||
| 145 | 6 | } | |
| 146 |