@@ -48,6 +48,7 @@ static void lfs_emubd_decblock(lfs_emubd_block_t *block) {
4848static lfs_emubd_block_t * lfs_emubd_mutblock (
4949 const struct lfs_config * cfg ,
5050 lfs_emubd_block_t * * block ) {
51+ lfs_emubd_t * bd = cfg -> context ;
5152 lfs_emubd_block_t * block_ = * block ;
5253 if (block_ && block_ -> rc == 1 ) {
5354 // rc == 1? can modify
@@ -56,13 +57,13 @@ static lfs_emubd_block_t *lfs_emubd_mutblock(
5657 } else if (block_ ) {
5758 // rc > 1? need to create a copy
5859 lfs_emubd_block_t * nblock = malloc (
59- sizeof (lfs_emubd_block_t ) + cfg -> block_size );
60+ sizeof (lfs_emubd_block_t ) + bd -> cfg -> erase_size );
6061 if (!nblock ) {
6162 return NULL ;
6263 }
6364
6465 memcpy (nblock , block_ ,
65- sizeof (lfs_emubd_block_t ) + cfg -> block_size );
66+ sizeof (lfs_emubd_block_t ) + bd -> cfg -> erase_size );
6667 nblock -> rc = 1 ;
6768
6869 lfs_emubd_decblock (block_ );
@@ -72,7 +73,7 @@ static lfs_emubd_block_t *lfs_emubd_mutblock(
7273 } else {
7374 // no block? need to allocate
7475 lfs_emubd_block_t * nblock = malloc (
75- sizeof (lfs_emubd_block_t ) + cfg -> block_size );
76+ sizeof (lfs_emubd_block_t ) + bd -> cfg -> erase_size );
7677 if (!nblock ) {
7778 return NULL ;
7879 }
@@ -81,10 +82,9 @@ static lfs_emubd_block_t *lfs_emubd_mutblock(
8182 nblock -> wear = 0 ;
8283
8384 // zero for consistency
84- lfs_emubd_t * bd = cfg -> context ;
8585 memset (nblock -> data ,
8686 (bd -> cfg -> erase_value != -1 ) ? bd -> cfg -> erase_value : 0 ,
87- cfg -> block_size );
87+ bd -> cfg -> erase_size );
8888
8989 * block = nblock ;
9090 return nblock ;
@@ -94,35 +94,35 @@ static lfs_emubd_block_t *lfs_emubd_mutblock(
9494
9595// emubd create/destroy
9696
97- int lfs_emubd_createcfg (const struct lfs_config * cfg , const char * path ,
97+ int lfs_emubd_create (const struct lfs_config * cfg ,
9898 const struct lfs_emubd_config * bdcfg ) {
99- LFS_EMUBD_TRACE ("lfs_emubd_createcfg(%p {.context=%p, "
100- ".read=%p, .prog=%p, .erase=%p, .sync=%p, "
101- ".read_size=%" PRIu32 ", .prog_size=%" PRIu32 ", "
102- ".block_size=%" PRIu32 ", .block_count=%" PRIu32 "}, "
103- "\"%s\", "
104- "%p {.erase_value=%" PRId32 ", .erase_cycles=%" PRIu32 ", "
99+ LFS_EMUBD_TRACE ("lfs_emubd_create(%p {.context=%p, "
100+ ".read=%p, .prog=%p, .erase=%p, .sync=%p}, "
101+ "%p {.read_size=%" PRIu32 ", .prog_size=%" PRIu32 ", "
102+ ".erase_size=%" PRIu32 ", .erase_count=%" PRIu32 ", "
103+ ".erase_value=%" PRId32 ", .erase_cycles=%" PRIu32 ", "
105104 ".badblock_behavior=%" PRIu8 ", .power_cycles=%" PRIu32 ", "
106105 ".powerloss_behavior=%" PRIu8 ", .powerloss_cb=%p, "
107106 ".powerloss_data=%p, .track_branches=%d})" ,
108107 (void * )cfg , cfg -> context ,
109108 (void * )(uintptr_t )cfg -> read , (void * )(uintptr_t )cfg -> prog ,
110109 (void * )(uintptr_t )cfg -> erase , (void * )(uintptr_t )cfg -> sync ,
111- cfg -> read_size , cfg -> prog_size , cfg -> block_size , cfg -> block_count ,
112- path , (void * )bdcfg , bdcfg -> erase_value , bdcfg -> erase_cycles ,
110+ (void * )bdcfg ,
111+ bdcfg -> read_size , bdcfg -> prog_size , bdcfg -> erase_size ,
112+ bdcfg -> erase_count , bdcfg -> erase_value , bdcfg -> erase_cycles ,
113113 bdcfg -> badblock_behavior , bdcfg -> power_cycles ,
114114 bdcfg -> powerloss_behavior , (void * )(uintptr_t )bdcfg -> powerloss_cb ,
115115 bdcfg -> powerloss_data , bdcfg -> track_branches );
116116 lfs_emubd_t * bd = cfg -> context ;
117117 bd -> cfg = bdcfg ;
118118
119119 // allocate our block array, all blocks start as uninitialized
120- bd -> blocks = malloc (cfg -> block_count * sizeof (lfs_emubd_block_t * ));
120+ bd -> blocks = malloc (bd -> cfg -> erase_count * sizeof (lfs_emubd_block_t * ));
121121 if (!bd -> blocks ) {
122- LFS_EMUBD_TRACE ("lfs_emubd_createcfg -> %d" , LFS_ERR_NOMEM );
122+ LFS_EMUBD_TRACE ("lfs_emubd_create -> %d" , LFS_ERR_NOMEM );
123123 return LFS_ERR_NOMEM ;
124124 }
125- memset (bd -> blocks , 0 , cfg -> block_count * sizeof (lfs_emubd_block_t * ));
125+ memset (bd -> blocks , 0 , bd -> cfg -> erase_count * sizeof (lfs_emubd_block_t * ));
126126
127127 // setup testing things
128128 bd -> readed = 0 ;
@@ -134,7 +134,7 @@ int lfs_emubd_createcfg(const struct lfs_config *cfg, const char *path,
134134 if (bd -> cfg -> disk_path ) {
135135 bd -> disk = malloc (sizeof (lfs_emubd_disk_t ));
136136 if (!bd -> disk ) {
137- LFS_EMUBD_TRACE ("lfs_emubd_createcfg -> %d" , LFS_ERR_NOMEM );
137+ LFS_EMUBD_TRACE ("lfs_emubd_create -> %d" , LFS_ERR_NOMEM );
138138 return LFS_ERR_NOMEM ;
139139 }
140140 bd -> disk -> rc = 1 ;
@@ -156,21 +156,21 @@ int lfs_emubd_createcfg(const struct lfs_config *cfg, const char *path,
156156 // if we're emulating erase values, we can keep a block around in
157157 // memory of just the erase state to speed up emulated erases
158158 if (bd -> cfg -> erase_value != -1 ) {
159- bd -> disk -> scratch = malloc (cfg -> block_size );
159+ bd -> disk -> scratch = malloc (bd -> cfg -> erase_size );
160160 if (!bd -> disk -> scratch ) {
161- LFS_EMUBD_TRACE ("lfs_emubd_createcfg -> %d" , LFS_ERR_NOMEM );
161+ LFS_EMUBD_TRACE ("lfs_emubd_create -> %d" , LFS_ERR_NOMEM );
162162 return LFS_ERR_NOMEM ;
163163 }
164164 memset (bd -> disk -> scratch ,
165165 bd -> cfg -> erase_value ,
166- cfg -> block_size );
166+ bd -> cfg -> erase_size );
167167
168168 // go ahead and erase all of the disk, otherwise the file will not
169169 // match our internal representation
170- for (size_t i = 0 ; i < cfg -> block_count ; i ++ ) {
170+ for (size_t i = 0 ; i < bd -> cfg -> erase_count ; i ++ ) {
171171 ssize_t res = write (bd -> disk -> fd ,
172172 bd -> disk -> scratch ,
173- cfg -> block_size );
173+ bd -> cfg -> erase_size );
174174 if (res < 0 ) {
175175 int err = - errno ;
176176 LFS_EMUBD_TRACE ("lfs_emubd_create -> %d" , err );
@@ -180,33 +180,16 @@ int lfs_emubd_createcfg(const struct lfs_config *cfg, const char *path,
180180 }
181181 }
182182
183- LFS_EMUBD_TRACE ("lfs_emubd_createcfg -> %d" , 0 );
183+ LFS_EMUBD_TRACE ("lfs_emubd_create -> %d" , 0 );
184184 return 0 ;
185185}
186186
187- int lfs_emubd_create (const struct lfs_config * cfg , const char * path ) {
188- LFS_EMUBD_TRACE ("lfs_emubd_create(%p {.context=%p, "
189- ".read=%p, .prog=%p, .erase=%p, .sync=%p, "
190- ".read_size=%" PRIu32 ", .prog_size=%" PRIu32 ", "
191- ".block_size=%" PRIu32 ", .block_count=%" PRIu32 "}, "
192- "\"%s\")" ,
193- (void * )cfg , cfg -> context ,
194- (void * )(uintptr_t )cfg -> read , (void * )(uintptr_t )cfg -> prog ,
195- (void * )(uintptr_t )cfg -> erase , (void * )(uintptr_t )cfg -> sync ,
196- cfg -> read_size , cfg -> prog_size , cfg -> block_size , cfg -> block_count ,
197- path );
198- static const struct lfs_emubd_config defaults = {.erase_value = -1 };
199- int err = lfs_emubd_createcfg (cfg , path , & defaults );
200- LFS_EMUBD_TRACE ("lfs_emubd_create -> %d" , err );
201- return err ;
202- }
203-
204187int lfs_emubd_destroy (const struct lfs_config * cfg ) {
205188 LFS_EMUBD_TRACE ("lfs_emubd_destroy(%p)" , (void * )cfg );
206189 lfs_emubd_t * bd = cfg -> context ;
207190
208191 // decrement reference counts
209- for (lfs_block_t i = 0 ; i < cfg -> block_count ; i ++ ) {
192+ for (lfs_block_t i = 0 ; i < bd -> cfg -> erase_count ; i ++ ) {
210193 lfs_emubd_decblock (bd -> blocks [i ]);
211194 }
212195 free (bd -> blocks );
@@ -237,10 +220,10 @@ int lfs_emubd_read(const struct lfs_config *cfg, lfs_block_t block,
237220 lfs_emubd_t * bd = cfg -> context ;
238221
239222 // check if read is valid
240- LFS_ASSERT (block < cfg -> block_count );
241- LFS_ASSERT (off % cfg -> read_size == 0 );
242- LFS_ASSERT (size % cfg -> read_size == 0 );
243- LFS_ASSERT (off + size <= cfg -> block_size );
223+ LFS_ASSERT (block < bd -> cfg -> erase_count );
224+ LFS_ASSERT (off % bd -> cfg -> read_size == 0 );
225+ LFS_ASSERT (size % bd -> cfg -> read_size == 0 );
226+ LFS_ASSERT (off + size <= bd -> cfg -> erase_size );
244227
245228 // get the block
246229 const lfs_emubd_block_t * b = bd -> blocks [block ];
@@ -287,10 +270,10 @@ int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
287270 lfs_emubd_t * bd = cfg -> context ;
288271
289272 // check if write is valid
290- LFS_ASSERT (block < cfg -> block_count );
291- LFS_ASSERT (off % cfg -> prog_size == 0 );
292- LFS_ASSERT (size % cfg -> prog_size == 0 );
293- LFS_ASSERT (off + size <= cfg -> block_size );
273+ LFS_ASSERT (block < bd -> cfg -> erase_count );
274+ LFS_ASSERT (off % bd -> cfg -> prog_size == 0 );
275+ LFS_ASSERT (size % bd -> cfg -> prog_size == 0 );
276+ LFS_ASSERT (off + size <= bd -> cfg -> erase_size );
294277
295278 // get the block
296279 lfs_emubd_block_t * b = lfs_emubd_mutblock (cfg , & bd -> blocks [block ]);
@@ -327,7 +310,7 @@ int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
327310 // mirror to disk file?
328311 if (bd -> disk ) {
329312 off_t res1 = lseek (bd -> disk -> fd ,
330- (off_t )block * cfg -> block_size + (off_t )off ,
313+ (off_t )block * bd -> cfg -> erase_size + (off_t )off ,
331314 SEEK_SET );
332315 if (res1 < 0 ) {
333316 int err = - errno ;
@@ -372,11 +355,11 @@ int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
372355
373356int lfs_emubd_erase (const struct lfs_config * cfg , lfs_block_t block ) {
374357 LFS_EMUBD_TRACE ("lfs_emubd_erase(%p, 0x%" PRIx32 " (%" PRIu32 "))" ,
375- (void * )cfg , block , cfg -> block_size );
358+ (void * )cfg , block , (( lfs_emubd_t * ) cfg -> context ) -> cfg -> erase_size );
376359 lfs_emubd_t * bd = cfg -> context ;
377360
378361 // check if erase is valid
379- LFS_ASSERT (block < cfg -> block_count );
362+ LFS_ASSERT (block < bd -> cfg -> erase_count );
380363
381364 // get the block
382365 lfs_emubd_block_t * b = lfs_emubd_mutblock (cfg , & bd -> blocks [block ]);
@@ -405,12 +388,12 @@ int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) {
405388
406389 // emulate an erase value?
407390 if (bd -> cfg -> erase_value != -1 ) {
408- memset (b -> data , bd -> cfg -> erase_value , cfg -> block_size );
391+ memset (b -> data , bd -> cfg -> erase_value , bd -> cfg -> erase_size );
409392
410393 // mirror to disk file?
411394 if (bd -> disk ) {
412395 off_t res1 = lseek (bd -> disk -> fd ,
413- (off_t )block * cfg -> block_size ,
396+ (off_t )block * bd -> cfg -> erase_size ,
414397 SEEK_SET );
415398 if (res1 < 0 ) {
416399 int err = - errno ;
@@ -420,7 +403,7 @@ int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) {
420403
421404 ssize_t res2 = write (bd -> disk -> fd ,
422405 bd -> disk -> scratch ,
423- cfg -> block_size );
406+ bd -> cfg -> erase_size );
424407 if (res2 < 0 ) {
425408 int err = - errno ;
426409 LFS_EMUBD_TRACE ("lfs_emubd_erase -> %d" , err );
@@ -430,7 +413,7 @@ int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) {
430413 }
431414
432415 // track erases
433- bd -> erased += cfg -> block_size ;
416+ bd -> erased += bd -> cfg -> erase_size ;
434417 if (bd -> cfg -> erase_sleep ) {
435418 int err = nanosleep (& (struct timespec ){
436419 .tv_sec = bd -> cfg -> erase_sleep /1000000000 ,
@@ -573,7 +556,7 @@ lfs_emubd_swear_t lfs_emubd_wear(const struct lfs_config *cfg,
573556 lfs_emubd_t * bd = cfg -> context ;
574557
575558 // check if block is valid
576- LFS_ASSERT (block < cfg -> block_count );
559+ LFS_ASSERT (block < bd -> cfg -> erase_count );
577560
578561 // get the wear
579562 lfs_emubd_wear_t wear ;
@@ -595,7 +578,7 @@ int lfs_emubd_setwear(const struct lfs_config *cfg,
595578 lfs_emubd_t * bd = cfg -> context ;
596579
597580 // check if block is valid
598- LFS_ASSERT (block < cfg -> block_count );
581+ LFS_ASSERT (block < bd -> cfg -> erase_count );
599582
600583 // set the wear
601584 lfs_emubd_block_t * b = lfs_emubd_mutblock (cfg , & bd -> blocks [block ]);
@@ -635,13 +618,13 @@ int lfs_emubd_copy(const struct lfs_config *cfg, lfs_emubd_t *copy) {
635618 lfs_emubd_t * bd = cfg -> context ;
636619
637620 // lazily copy over our block array
638- copy -> blocks = malloc (cfg -> block_count * sizeof (lfs_emubd_block_t * ));
621+ copy -> blocks = malloc (bd -> cfg -> erase_count * sizeof (lfs_emubd_block_t * ));
639622 if (!copy -> blocks ) {
640623 LFS_EMUBD_TRACE ("lfs_emubd_copy -> %d" , LFS_ERR_NOMEM );
641624 return LFS_ERR_NOMEM ;
642625 }
643626
644- for (size_t i = 0 ; i < cfg -> block_count ; i ++ ) {
627+ for (size_t i = 0 ; i < bd -> cfg -> erase_count ; i ++ ) {
645628 copy -> blocks [i ] = lfs_emubd_incblock (bd -> blocks [i ]);
646629 }
647630
0 commit comments