LexLeo 0.0.0-dev+f8e5087-dirty
Technical documentation
Loading...
Searching...
No Matches
stream_factory.c
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-3.0-or-later
2 * Copyright (C) 2026 Sylvain Labopin
3 */
4
19
21
22#include "osal/mem/osal_mem.h"
23
25
27 const stream_registry_t *reg,
28 stream_key_t key)
29{
30 if (!reg || !reg->entries || !reg->count || !key) return NULL;
31
32 for (size_t i = 0; i < reg->count; i++) {
33 const stream_branch_t *e = &reg->entries[i];
34 if (e->key && osal_strcmp(e->key, key) == 0) return e;
35 }
36 return NULL;
37}
38
40 const stream_registry_t *reg,
41 stream_key_t key,
42 const void *args,
43 stream_t **out_stream)
44{
45 if (!out_stream) return STREAM_STATUS_INVALID;
46 *out_stream = NULL;
47
48 const stream_branch_t *e = stream_registry_find(reg, key);
49 if (!e || !e->ctor) return STREAM_STATUS_NO_BACKEND;
50
51 return e->ctor(e->ud, args, out_stream);
52}
53
55 stream_factory_t **out,
56 const stream_factory_cfg_t *cfg,
57 const stream_env_t *env )
58{
59 if (
60 !out
61 || !cfg
62 || !env
63 || !env->mem
64 || !env->mem->calloc
65 || !env->mem->free )
67
68 stream_factory_t *f = env->mem->calloc(1, sizeof(*f));
69 if (!f) return STREAM_STATUS_OOM;
70
71 f->mem = env->mem;
72 f->reg.entries = NULL;
73 f->reg.count = 0;
74 f->reg.cap = 0;
75
76 if (cfg->fact_cap > 0) {
77 f->reg.entries = env->mem->calloc(cfg->fact_cap, sizeof(*f->reg.entries));
78 if (!f->reg.entries) {
79 env->mem->free(f);
80 return STREAM_STATUS_OOM;
81 }
82 f->reg.cap = cfg->fact_cap;
83 }
84
85 *out = f;
86 return STREAM_STATUS_OK;
87}
88
90{
91 if (!fact || !*fact) return;
92
93 stream_factory_t *f = *fact;
94 *fact = NULL;
95
96 const osal_mem_ops_t *mem = f->mem;
97 if (!mem || !mem->free) return;
98
99 if (f->reg.entries) {
100 for (size_t i = 0; i < f->reg.count; ++i) {
101 stream_branch_t *e = &f->reg.entries[i];
102 if (e->ud_dtor) e->ud_dtor(e->ud, mem);
103 }
104 mem->free(f->reg.entries);
105 }
106 mem->free(f);
107}
108
110 stream_factory_t *fact,
111 const stream_adapter_desc_t *desc )
112{
113 if (
114 !fact
115 || !desc
116 || !desc->key
117 || *desc->key == '\0'
118 || !desc->ctor
119 || (desc->ud && !desc->ud_dtor) )
121
122 stream_registry_t *reg = &fact->reg;
123
124 if (!reg->entries || reg->cap == 0) {
126 }
127
128 if (reg->count >= reg->cap) {
129 return STREAM_STATUS_FULL;
130 }
131
132 // Enforce uniqueness
133 for (size_t i = 0; i < reg->count; ++i) {
134 if (
135 reg->entries[i].key
136 && osal_strcmp(reg->entries[i].key, desc->key) == 0 ) {
137 return STREAM_STATUS_ALREADY_EXISTS; // duplicate key
138 }
139 }
140
141 reg->entries[reg->count++] = (stream_branch_t){
142 .key = desc->key,
143 .ctor = desc->ctor,
144 .ud = desc->ud,
145 .ud_dtor = desc->ud_dtor
146 };
147
148 return STREAM_STATUS_OK;
149}
150
152 const stream_factory_t *f,
153 stream_key_t key,
154 const void *args,
155 stream_t **out )
156{
157 if (!out || !f || !args || !key || *key == '\0')
159
160 if (!stream_registry_find(&f->reg, key))
162
163 stream_t *tmp = NULL;
164 stream_status_t st = stream_registry_create(&f->reg, key, args, &tmp);
165
166 if (st == STREAM_STATUS_OK) *out = tmp;
167
168 return st;
169}
int osal_strcmp(const char *s1, const char *s2)
Definition osal_mem.c:40
stream_status_t stream_create_factory(stream_factory_t **out, const stream_factory_cfg_t *cfg, const stream_env_t *env)
Create a stream factory.
stream_status_t stream_factory_add_adapter(stream_factory_t *fact, const stream_adapter_desc_t *desc)
Register an adapter descriptor into a stream factory.
static const stream_branch_t * stream_registry_find(const stream_registry_t *reg, stream_key_t key)
stream_status_t stream_factory_create_stream(const stream_factory_t *f, stream_key_t key, const void *args, stream_t **out)
Create a stream from a registered adapter key.
void stream_destroy_factory(stream_factory_t **fact)
Destroy a stream factory.
static stream_status_t stream_registry_create(const stream_registry_t *reg, stream_key_t key, const void *args, stream_t **out_stream)
Composition Root factory API for the stream port.
Private factory handle definition for the stream port.
const char * stream_key_t
Public identifier type for a registered stream adapter.
stream_status_t
Public status codes used by the stream port.
@ STREAM_STATUS_INVALID
@ STREAM_STATUS_OK
@ STREAM_STATUS_FULL
@ STREAM_STATUS_NOT_FOUND
@ STREAM_STATUS_OOM
@ STREAM_STATUS_ALREADY_EXISTS
@ STREAM_STATUS_NO_BACKEND
void *(* calloc)(size_t nmemb, size_t size)
void(* free)(void *ptr)
Public descriptor used to register a concrete stream adapter.
stream_key_t key
Public key used to identify the adapter.
const void * ud
Optional opaque user data bound to the constructor.
ud_dtor_fn_t ud_dtor
Optional destructor for ud.
stream_ctor_fn_t ctor
Adapter constructor used to create a stream_t.
Private registered adapter entry.
stream_ctor_fn_t ctor
Runtime environment for the stream port.
Definition stream_env.h:35
const osal_mem_ops_t * mem
Memory operations used by the stream port.
Definition stream_env.h:43
Configuration for stream_factory_t.
Private handle structure for a stream_factory_t.
stream_registry_t reg
const osal_mem_ops_t * mem
Private adapter registry used by stream_factory_t.
stream_branch_t * entries
Private handle structure for a stream_t.