Skip to content

Commit 7f16770

Browse files
guohao15xiaoxiang781216
authored andcommitted
bch:alloc bch->buffer when offset not aligned
Signed-off-by: guohao15 <[email protected]>
1 parent 458bab9 commit 7f16770

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

drivers/bch/bchlib_cache.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
****************************************************************************/
2424

2525
#include <nuttx/config.h>
26+
#include <nuttx/kmalloc.h>
2627

2728
#include <sys/types.h>
2829
#include <stdbool.h>
@@ -112,7 +113,7 @@ int bchlib_flushsector(FAR struct bchlib_s *bch, bool discard)
112113
* media.
113114
*/
114115

115-
if (bch->dirty)
116+
if (bch->dirty && bch->buffer != NULL)
116117
{
117118
inode = bch->inode;
118119

@@ -168,6 +169,20 @@ int bchlib_readsector(FAR struct bchlib_s *bch, size_t sector)
168169
FAR struct inode *inode;
169170
ssize_t ret = OK;
170171

172+
if (bch->buffer == NULL)
173+
{
174+
#if CONFIG_BCH_BUFFER_ALIGNMENT != 0
175+
bch->buffer = kmm_memalign(CONFIG_BCH_BUFFER_ALIGNMENT, bch->sectsize);
176+
#else
177+
bch->buffer = kmm_malloc(bch->sectsize);
178+
#endif
179+
if (bch->buffer == NULL)
180+
{
181+
ferr("Failed to allocate sector buffer\n");
182+
return -ENOMEM;
183+
}
184+
}
185+
171186
if (bch->sector != sector)
172187
{
173188
inode = bch->inode;

drivers/bch/bchlib_setup.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,6 @@ int bchlib_setup(const char *blkdev, bool readonly, FAR void **handle)
110110
bch->sectsize = geo.geo_sectorsize;
111111
bch->sector = (size_t)-1;
112112
bch->readonly = readonly;
113-
114-
/* Allocate the sector I/O buffer */
115-
116-
#if CONFIG_BCH_BUFFER_ALIGNMENT != 0
117-
bch->buffer = kmm_memalign(CONFIG_BCH_BUFFER_ALIGNMENT, bch->sectsize);
118-
#else
119-
bch->buffer = kmm_malloc(bch->sectsize);
120-
#endif
121-
if (!bch->buffer)
122-
{
123-
ferr("ERROR: Failed to allocate sector buffer\n");
124-
ret = -ENOMEM;
125-
goto errout_with_bch;
126-
}
127-
128113
*handle = bch;
129114
return OK;
130115

0 commit comments

Comments
 (0)