Loading...
Searching...
No Matches
font_slots.h File Reference

The decisions behind the font registry: who owns each slot's font, when a swap has nothing to do, and when a font can be freed. More...

#include <stdbool.h>
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  FontSlots
 The registry's tables, one entry a slot in each. More...

Typedefs

typedef void *(*) FontSlotLoad(uint32_t resource)
 Loads a font from its resource.
typedef void(*) FontSlotUnload(void *handle)
 Frees a font the app loaded.

Enumerations

enum  FontSlotState { FONT_SLOT_UNOWNED , FONT_SLOT_OWNED , FONT_SLOT_LOADED }
 What a slot holds and who frees it. Kept in a uint8_t a slot. More...

Functions

void font_slots_put (const FontSlots *slots, int id, void *handle, bool owned)
 Puts a font in a slot, overwriting whatever it held without freeing it.
bool font_slots_load (const FontSlots *slots, uint32_t *resources, int id, uint32_t resource, FontSlotLoad load)
 Loads a font into a slot and records the resource it came from.
bool font_slots_swap (const FontSlots *slots, uint32_t *resources, int id, uint32_t resource, FontSlotLoad load, FontSlotUnload unload)
 Swaps a slot's font for another resource, freeing the old one when nothing else holds it.
void font_slots_unload_all (const FontSlots *slots, FontSlotUnload unload)
 Frees every font the app owns, each one once, and empties every slot.

Detailed Description

The decisions behind the font registry: who owns each slot's font, when a swap has nothing to do, and when a font can be freed.

The registry in ui/fonts.c keeps the tables and hands in the SDK's load and unload calls, so everything that decides whether a font is freed runs here, off the watch. Freeing the wrong font leaves another slot drawing with memory that is gone, and freeing none leaks a font a swap at a time, and neither shows until the heap runs out.

A handle is a plain pointer here, since this file cannot see GFont. Each slot carries a state beside its handle. Every write to a slot sets that state, so a record of where a font came from only lasts while the slot still holds that font, and a freed handle whose address comes back for another font is never mistaken for the old one.