/** \defgroup pw_map Map * *\briefAmapthatholdspointerstoobjectsindexedbyid * *Themapisasparseversionofthe\refpw_array"pw_array"thatmanagesthe *indicesofelementsforthecaller.Addingitemswith *pw_map_insert_new()returnstheassignedindexforthatitem;ifitems *areremovedthemapre-usesindicestokeepthearrayattheminimum *requiredsize. * *\code{.c} *structpw_mapmap=PW_MAP_INIT(4); * *idx1=pw_map_insert_new(&map,ptr1); *idx2=pw_map_insert_new(&map,ptr2); *// the map is now [ptr1, ptr2], size 2 *pw_map_remove(&map,idx1); *// the map is now [<unused>, ptr2], size 2 *pw_map_insert_new(&map,ptr3); *// the map is now [ptr3, ptr2], size 2 *\endcode
*/
/** *\addtogrouppw_map *\{
*/
/** \private *Anentryinthemap.Thisisusedinternallyonly.Eachelementinthe *backingpw_arrayisaunionpw_map_item.Forrealitems,thedatapointer *pointstotheitem.Ifanelementhasbeenremoved,pw_map->free_list *istheindexofthemostrecentlyremoveditem.Thatitemcontains *theindexofthenextremoveditemuntilitem->nextisSPA_ID_INVALID. * *Thefreelistisprependedonly,thelastitemtoberemovedwillbethe *firstitemtogetre-usedonthenextinsert.
*/ union pw_map_item {
uintptr_t next; /* next free index */ void *data; /* data of this item, must be an even address */
};
/** A map. This struct should be treated as opaque by the caller. */ struct pw_map { struct pw_array items; /* an array with the map items */
uint32_t free_list; /* first free index */
};
/** \param extend the amount of bytes to grow the map with when needed */ #define PW_MAP_INIT(extend) ((struct pw_map) { PW_ARRAY_INIT(extend), SPA_ID_INVALID })
/** *Getthenumberofcurrentlyallocatedelementsinthemap. *\notepw_map_get_size()returnsthecurrentlyallocatednumberof *elementsinthemap,notthenumberofactuallysetelements. *\returnthenumberofavailableelementsbeforethemapneedstogrow
*/ #define pw_map_get_size(m) pw_array_get_len(&(m)->items, union pw_map_item) #define pw_map_get_item(m,id) pw_array_get_unchecked(&(m)->items,id,union pw_map_item) #define pw_map_item_is_free(item) ((item)->next & 0x1) #define pw_map_id_is_free(m,id) (pw_map_item_is_free(pw_map_get_item(m,id))) /** \return true if the id fits within the current map size */ #define pw_map_check_id(m,id) ((id) < pw_map_get_size(m)) /** \return true if there is a valid item at \a id */ #define pw_map_has_item(m,id) (pw_map_check_id(m,id) && !pw_map_id_is_free(m, id)) #define pw_map_lookup_unchecked(m,id) pw_map_get_item(m,id)->data
/** Convert an id to a pointer that can be inserted into the map */ #define PW_MAP_ID_TO_PTR(id) (SPA_UINT32_TO_PTR((id)<<1)) /** Convert a pointer to an id that can be retrieved from the map */ #define PW_MAP_PTR_TO_ID(p) (SPA_PTR_TO_UINT32(p)>>1)
/** Clear a map and free the data storage. All previously returned ids *mustbetreatedasinvalid.
*/
PW_API_MAP void pw_map_clear(struct pw_map *map)
{
pw_array_clear(&map->items);
}
/** Reset a map but keep previously allocated storage. All previously *returnedidsmustbetreatedasinvalid.
*/
PW_API_MAP void pw_map_reset(struct pw_map *map)
{
pw_array_reset(&map->items);
map->free_list = SPA_ID_INVALID;
}
/** Insert data in the map. This function causes the map to grow if required. *\parammapthemaptoinsertinto *\paramdatatheitemtoadd *\returntheidwheretheitemwasinsertedorSPA_ID_INVALIDwhenthe *itemcannotbeinserted.
*/
PW_API_MAP uint32_t pw_map_insert_new(struct pw_map *map, void *data)
{ union pw_map_item *start, *item;
uint32_t id;
/** Replace the data in the map at an index. * *\parammapthemaptoinsertinto *\paramidtheindextoinsertat,mustbelessorequaltopw_map_get_size() *\paramdatathedatatoinsert *\return0onsuccess,-ENOSPCvaluewhentheindexisinvalidoranegativeerrno
*/
PW_API_MAP int pw_map_insert_at(struct pw_map *map, uint32_t id, void *data)
{
size_t size = pw_map_get_size(map); union pw_map_item *item;
if (id > size) return -ENOSPC; elseif (id == size) {
item = (union pw_map_item *) pw_array_add(&map->items, sizeof(union pw_map_item)); if (item == NULL) return -errno;
} else {
item = pw_map_get_item(map, id); if (pw_map_item_is_free(item)) return -EINVAL;
}
item->data = data; return0;
}
/** Remove an item at index. The id may get re-used in the future. * *\parammapthemaptoremovefrom *\paramidtheindextoremove
*/
PW_API_MAP void pw_map_remove(struct pw_map *map, uint32_t id)
{ if (pw_map_id_is_free(map, id)) return;
/** Find an item in the map *\parammapthemaptouse *\paramidtheindextolookat *\returntheitemat\aidorNULLwhennosuchitemexists
*/
PW_API_MAP void *pw_map_lookup(conststruct pw_map *map, uint32_t id)
{ if (SPA_LIKELY(pw_map_check_id(map, id))) { union pw_map_item *item = pw_map_get_item(map, id); if (!pw_map_item_is_free(item)) return item->data;
} return NULL;
}
/** Iterate all map items *\parammapthemaptoiterate *\paramfuncthefunctiontocallforeachitem,theitemdataand\adatais *passedtothefunction.When\afuncreturnsanon-zeroresult, *iterationendsandtheresultisreturned. *\paramdatadatatopassto\afunc *\returntheresultofthelastcallto\afuncor0whenallcallbacksreturned0.
*/
PW_API_MAP int pw_map_for_each(conststruct pw_map *map, int (*func) (void *item_data, void *data), void *data)
{ union pw_map_item *item; int res = 0;
pw_array_for_each(item, &map->items) { if (!pw_map_item_is_free(item)) if ((res = func(item->data, data)) != 0) break;
} return res;
}
/** *\}
*/
#ifdef __cplusplus
} /* extern "C" */ #endif
#endif/* PIPEWIRE_MAP_H */
Messung V0.5 in Prozent
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.29Bemerkung:
(vorverarbeitet am 2026-08-25)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.