@@ -151,15 +151,23 @@ int k_mem_domain_init(struct k_mem_domain *domain, uint8_t num_parts,
151151 return ret ;
152152}
153153
154- void k_mem_domain_add_partition (struct k_mem_domain * domain ,
155- struct k_mem_partition * part )
154+ int k_mem_domain_add_partition (struct k_mem_domain * domain ,
155+ struct k_mem_partition * part )
156156{
157157 int p_idx ;
158158 k_spinlock_key_t key ;
159+ int ret = 0 ;
159160
160- __ASSERT_NO_MSG (domain != NULL );
161- __ASSERT (check_add_partition (domain , part ),
162- "invalid partition %p" , part );
161+ CHECKIF (domain == NULL ) {
162+ ret = - EINVAL ;
163+ goto out ;
164+ }
165+
166+ CHECKIF (!check_add_partition (domain , part )) {
167+ LOG_ERR ("invalid partition %p" , part );
168+ ret = - EINVAL ;
169+ goto out ;
170+ }
163171
164172 key = k_spin_lock (& z_mem_domain_lock );
165173
@@ -170,8 +178,11 @@ void k_mem_domain_add_partition(struct k_mem_domain *domain,
170178 }
171179 }
172180
173- __ASSERT (p_idx < max_partitions ,
174- "no free partition slots available" );
181+ CHECKIF (!(p_idx < max_partitions )) {
182+ LOG_ERR ("no free partition slots available" );
183+ ret = - ENOSPC ;
184+ goto unlock_out ;
185+ }
175186
176187 LOG_DBG ("add partition base %lx size %zu to domain %p\n" ,
177188 part -> start , part -> size , domain );
@@ -185,17 +196,25 @@ void k_mem_domain_add_partition(struct k_mem_domain *domain,
185196#ifdef CONFIG_ARCH_MEM_DOMAIN_SYNCHRONOUS_API
186197 arch_mem_domain_partition_add (domain , p_idx );
187198#endif
199+
200+ unlock_out :
188201 k_spin_unlock (& z_mem_domain_lock , key );
202+
203+ out :
204+ return ret ;
189205}
190206
191- void k_mem_domain_remove_partition (struct k_mem_domain * domain ,
207+ int k_mem_domain_remove_partition (struct k_mem_domain * domain ,
192208 struct k_mem_partition * part )
193209{
194210 int p_idx ;
195211 k_spinlock_key_t key ;
212+ int ret = 0 ;
196213
197- __ASSERT_NO_MSG (domain != NULL );
198- __ASSERT_NO_MSG (part != NULL );
214+ CHECKIF ((domain == NULL ) || (part == NULL )) {
215+ ret = - EINVAL ;
216+ goto out ;
217+ }
199218
200219 key = k_spin_lock (& z_mem_domain_lock );
201220
@@ -207,7 +226,11 @@ void k_mem_domain_remove_partition(struct k_mem_domain *domain,
207226 }
208227 }
209228
210- __ASSERT (p_idx < max_partitions , "no matching partition found" );
229+ CHECKIF (!(p_idx < max_partitions )) {
230+ LOG_ERR ("no matching partition found" );
231+ ret = - ENOENT ;
232+ goto unlock_out ;
233+ }
211234
212235 LOG_DBG ("remove partition base %lx size %zu from domain %p\n" ,
213236 part -> start , part -> size , domain );
@@ -221,7 +244,11 @@ void k_mem_domain_remove_partition(struct k_mem_domain *domain,
221244
222245 domain -> num_partitions -- ;
223246
247+ unlock_out :
224248 k_spin_unlock (& z_mem_domain_lock , key );
249+
250+ out :
251+ return ret ;
225252}
226253
227254static void add_thread_locked (struct k_mem_domain * domain ,
@@ -301,7 +328,9 @@ static int init_mem_domain_module(const struct device *arg)
301328 __ASSERT (ret == 0 , "failed to init default mem domain" );
302329
303330#ifdef Z_LIBC_PARTITION_EXISTS
304- k_mem_domain_add_partition (& k_mem_domain_default , & z_libc_partition );
331+ ret = k_mem_domain_add_partition (& k_mem_domain_default ,
332+ & z_libc_partition );
333+ __ASSERT (ret == 0 , "failed to add default libc mem partition" );
305334#endif /* Z_LIBC_PARTITION_EXISTS */
306335
307336 return 0 ;
0 commit comments