@@ -60,14 +60,17 @@ void *am_onesize_allocate(struct am_onesize *hnd, int size) {
6060 return NULL ;
6161 }
6262
63+ hnd -> crit_enter ();
6364 if (am_slist_is_empty (& hnd -> fl )) {
65+ hnd -> crit_exit ();
6466 return NULL ;
6567 }
6668
6769 struct am_slist_item * elem = am_slist_pop_front (& hnd -> fl );
6870 AM_ASSERT (elem );
6971
7072 am_onesize_run_stats (hnd , 1 , 0 );
73+ hnd -> crit_exit ();
7174
7275 return elem ;
7376}
@@ -82,15 +85,17 @@ void am_onesize_free(struct am_onesize *hnd, const void *ptr) {
8285
8386 struct am_slist_item * p = AM_CAST (struct am_slist_item * , ptr );
8487
88+ hnd -> crit_enter ();
8589 am_slist_push_front (& hnd -> fl , p );
8690 am_onesize_run_stats (hnd , 0 , 1 );
91+ hnd -> crit_exit ();
8792}
8893
8994/**
9095 * Internal initialization routine.
9196 * @param hnd the allocator
9297 */
93- static void am_onesize_init_internal (struct am_onesize * hnd ) {
98+ static void am_onesize_ctor_internal (struct am_onesize * hnd ) {
9499 am_slist_init (& hnd -> fl );
95100
96101 char * ptr = (char * )hnd -> pool .ptr ;
@@ -108,9 +113,11 @@ void am_onesize_free_all(struct am_onesize *hnd) {
108113
109114 struct am_onesize * s = (struct am_onesize * )hnd ;
110115
116+ hnd -> crit_enter ();
111117 int minfree = hnd -> minfree ;
112- am_onesize_init_internal (s );
118+ am_onesize_ctor_internal (s );
113119 hnd -> minfree = minfree ; /* cppcheck-suppress redundantAssignment */
120+ hnd -> crit_exit ();
114121}
115122
116123void am_onesize_iterate_over_allocated (
@@ -128,17 +135,21 @@ void am_onesize_iterate_over_allocated(
128135 }
129136 int iterated = 0 ;
130137 num = AM_MIN (num , total );
138+ hnd -> crit_enter ();
131139 for (int i = 0 ; (i < total ) && (iterated < num ); i ++ ) {
132140 AM_ASSERT (AM_ALIGNOF_PTR (ptr ) >= AM_ALIGNOF (struct am_slist_item ));
133141 struct am_slist_item * item = AM_CAST (struct am_slist_item * , ptr );
134142 if (am_slist_owns (& impl -> fl , item )) {
135143 continue ; /* the item is free */
136144 }
137145 /* the item is allocated */
146+ hnd -> crit_exit ();
138147 cb (ctx , iterated , (char * )item , impl -> block_size );
148+ hnd -> crit_enter ();
139149 ptr += impl -> block_size ;
140150 iterated ++ ;
141151 }
152+ hnd -> crit_exit ();
142153}
143154
144155int am_onesize_get_nfree (const struct am_onesize * hnd ) {
@@ -161,31 +172,42 @@ int am_onesize_get_nblocks(const struct am_onesize *hnd) {
161172 return hnd -> ntotal ;
162173}
163174
164- void am_onesize_init (
165- struct am_onesize * hnd , struct am_blk * pool , int block_size , int alignment
166- ) {
175+ static void am_onesize_crit_enter (void ) {}
176+ static void am_onesize_crit_exit (void ) {}
177+
178+ void am_onesize_ctor (struct am_onesize * hnd , struct am_onesize_cfg * cfg ) {
167179 AM_ASSERT (hnd );
168- AM_ASSERT (pool );
169- AM_ASSERT (pool -> ptr );
170- AM_ASSERT (pool -> size > 0 );
171- AM_ASSERT (pool -> size >= block_size );
172- AM_ASSERT (alignment >= AM_ALIGNOF (struct am_slist_item ));
180+ AM_ASSERT (cfg );
181+ AM_ASSERT (cfg -> pool );
182+ AM_ASSERT (cfg -> pool -> ptr );
183+ AM_ASSERT (cfg -> pool -> size > 0 );
184+ AM_ASSERT (cfg -> pool -> size >= cfg -> block_size );
185+ AM_ASSERT (cfg -> alignment >= AM_ALIGNOF (struct am_slist_item ));
173186
174187 memset (hnd , 0 , sizeof (* hnd ));
175188
176- void * alignedptr = AM_ALIGN_PTR_UP (pool -> ptr , alignment );
177- int affix = (int )((uintptr_t )alignedptr - (uintptr_t )pool -> ptr );
178- AM_ASSERT (affix < pool -> size );
179- pool -> size -= affix ;
180- pool -> ptr = alignedptr ;
189+ void * alignedptr = AM_ALIGN_PTR_UP (cfg -> pool -> ptr , cfg -> alignment );
190+ int affix = (int )((uintptr_t )alignedptr - (uintptr_t )cfg -> pool -> ptr );
191+ AM_ASSERT (affix < cfg -> pool -> size );
192+ cfg -> pool -> size -= affix ;
193+ cfg -> pool -> ptr = alignedptr ;
181194
182- block_size = AM_MAX (block_size , (int )sizeof (struct am_slist_item ));
183- block_size = AM_MAX (block_size , alignment );
195+ cfg -> block_size =
196+ AM_MAX (cfg -> block_size , (int )sizeof (struct am_slist_item ));
197+ cfg -> block_size = AM_MAX (cfg -> block_size , cfg -> alignment );
184198
185- AM_ASSERT (pool -> size >= block_size );
199+ AM_ASSERT (cfg -> pool -> size >= cfg -> block_size );
186200
187- hnd -> pool = * pool ;
188- hnd -> block_size = block_size ;
201+ hnd -> pool = * cfg -> pool ;
202+ hnd -> block_size = cfg -> block_size ;
203+
204+ if (cfg -> crit_enter && cfg -> crit_exit ) {
205+ hnd -> crit_enter = cfg -> crit_enter ;
206+ hnd -> crit_exit = cfg -> crit_exit ;
207+ } else {
208+ hnd -> crit_enter = am_onesize_crit_enter ;
209+ hnd -> crit_exit = am_onesize_crit_exit ;
210+ }
189211
190- am_onesize_init_internal (hnd );
212+ am_onesize_ctor_internal (hnd );
191213}
0 commit comments