Skip to content

Commit fd6e912

Browse files
authored
Merge pull request ARMmbed#7648 from davidsaada/david_init_ref_count
Add init reference count to all block devices
2 parents b154fd8 + 2fa1563 commit fd6e912

13 files changed

+96
-12
lines changed

features/TESTS/filesystem/util_block_device/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,6 @@ void test_slicing() {
9292
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
9393
}
9494

95-
err = slice1.deinit();
96-
TEST_ASSERT_EQUAL(0, err);
97-
98-
9995
// Test with second slice of block device
10096
err = slice2.init();
10197
TEST_ASSERT_EQUAL(0, err);
@@ -109,6 +105,10 @@ void test_slicing() {
109105
write_block[i] = 0xff & rand();
110106
}
111107

108+
// Deinitialize slice1 here, to check whether init reference count works
109+
err = slice1.deinit();
110+
TEST_ASSERT_EQUAL(0, err);
111+
112112
// Write, sync, and read the block
113113
err = slice2.program(write_block, 0, BLOCK_SIZE);
114114
TEST_ASSERT_EQUAL(0, err);

features/filesystem/bd/BufferedBlockDevice.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "BufferedBlockDevice.h"
1818
#include "mbed_assert.h"
19+
#include "mbed_critical.h"
1920
#include <algorithm>
2021
#include <string.h>
2122

@@ -25,7 +26,7 @@ static inline uint32_t align_down(bd_size_t val, bd_size_t size)
2526
}
2627

2728
BufferedBlockDevice::BufferedBlockDevice(BlockDevice *bd)
28-
: _bd(bd), _bd_program_size(0), _curr_aligned_addr(0), _flushed(true), _cache(0)
29+
: _bd(bd), _bd_program_size(0), _curr_aligned_addr(0), _flushed(true), _cache(0), _init_ref_count(0)
2930
{
3031
}
3132

@@ -36,6 +37,12 @@ BufferedBlockDevice::~BufferedBlockDevice()
3637

3738
int BufferedBlockDevice::init()
3839
{
40+
uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1);
41+
42+
if (val != 1) {
43+
return BD_ERROR_OK;
44+
}
45+
3946
int err = _bd->init();
4047
if (err) {
4148
return err;
@@ -55,6 +62,12 @@ int BufferedBlockDevice::init()
5562

5663
int BufferedBlockDevice::deinit()
5764
{
65+
uint32_t val = core_util_atomic_decr_u32(&_init_ref_count, 1);
66+
67+
if (val) {
68+
return BD_ERROR_OK;
69+
}
70+
5871
delete[] _cache;
5972
_cache = 0;
6073
return _bd->deinit();

features/filesystem/bd/BufferedBlockDevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ class BufferedBlockDevice : public BlockDevice {
153153
bd_size_t _curr_aligned_addr;
154154
bool _flushed;
155155
uint8_t *_cache;
156+
uint32_t _init_ref_count;
156157

157158
/** Flush data in cache
158159
*

features/filesystem/bd/ChainingBlockDevice.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616

1717
#include "ChainingBlockDevice.h"
18+
#include "mbed_critical.h"
1819

1920

2021
ChainingBlockDevice::ChainingBlockDevice(BlockDevice **bds, size_t bd_count)
2122
: _bds(bds), _bd_count(bd_count)
2223
, _read_size(0), _program_size(0), _erase_size(0), _size(0)
23-
, _erase_value(-1)
24+
, _erase_value(-1), _init_ref_count(0)
2425
{
2526
}
2627

@@ -31,6 +32,12 @@ static bool is_aligned(uint64_t x, uint64_t alignment)
3132

3233
int ChainingBlockDevice::init()
3334
{
35+
uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1);
36+
37+
if (val != 1) {
38+
return BD_ERROR_OK;
39+
}
40+
3441
_read_size = 0;
3542
_program_size = 0;
3643
_erase_size = 0;
@@ -83,6 +90,12 @@ int ChainingBlockDevice::init()
8390

8491
int ChainingBlockDevice::deinit()
8592
{
93+
uint32_t val = core_util_atomic_decr_u32(&_init_ref_count, 1);
94+
95+
if (val) {
96+
return BD_ERROR_OK;
97+
}
98+
8699
for (size_t i = 0; i < _bd_count; i++) {
87100
int err = _bds[i]->deinit();
88101
if (err) {

features/filesystem/bd/ChainingBlockDevice.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class ChainingBlockDevice : public BlockDevice
6464
template <size_t Size>
6565
ChainingBlockDevice(BlockDevice *(&bds)[Size])
6666
: _bds(bds), _bd_count(sizeof(bds) / sizeof(bds[0]))
67-
, _read_size(0), _program_size(0), _erase_size(0), _size(0)
67+
, _read_size(0), _program_size(0), _erase_size(0), _size(0), _init_ref_count(0)
6868
{
6969
}
7070

@@ -175,6 +175,7 @@ class ChainingBlockDevice : public BlockDevice
175175
bd_size_t _erase_size;
176176
bd_size_t _size;
177177
int _erase_value;
178+
uint32_t _init_ref_count;
178179
};
179180

180181

features/filesystem/bd/ExhaustibleBlockDevice.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616

1717
#include "ExhaustibleBlockDevice.h"
1818
#include "mbed.h"
19+
#include "mbed_critical.h"
1920

2021

2122
ExhaustibleBlockDevice::ExhaustibleBlockDevice(BlockDevice *bd, uint32_t erase_cycles)
22-
: _bd(bd), _erase_array(NULL), _erase_cycles(erase_cycles)
23+
: _bd(bd), _erase_array(NULL), _erase_cycles(erase_cycles), _init_ref_count(0)
2324
{
2425
}
2526

@@ -30,6 +31,12 @@ ExhaustibleBlockDevice::~ExhaustibleBlockDevice()
3031

3132
int ExhaustibleBlockDevice::init()
3233
{
34+
uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1);
35+
36+
if (val != 1) {
37+
return BD_ERROR_OK;
38+
}
39+
3340
int err = _bd->init();
3441
if (err) {
3542
return err;
@@ -48,6 +55,12 @@ int ExhaustibleBlockDevice::init()
4855

4956
int ExhaustibleBlockDevice::deinit()
5057
{
58+
core_util_atomic_decr_u32(&_init_ref_count, 1);
59+
60+
if (_init_ref_count) {
61+
return BD_ERROR_OK;
62+
}
63+
5164
// _erase_array is lazily cleaned up in destructor to allow
5265
// data to live across de/reinitialization
5366
return _bd->deinit();

features/filesystem/bd/ExhaustibleBlockDevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ class ExhaustibleBlockDevice : public BlockDevice
154154
BlockDevice *_bd;
155155
uint32_t *_erase_array;
156156
uint32_t _erase_cycles;
157+
uint32_t _init_ref_count;
157158
};
158159

159160

features/filesystem/bd/FlashSimBlockDevice.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "FlashSimBlockDevice.h"
1818
#include "mbed_assert.h"
19+
#include "mbed_critical.h"
1920
#include <algorithm>
2021
#include <stdlib.h>
2122
#include <string.h>
@@ -29,7 +30,7 @@ static inline uint32_t align_up(bd_size_t val, bd_size_t size)
2930

3031
FlashSimBlockDevice::FlashSimBlockDevice(BlockDevice *bd, uint8_t erase_value) :
3132
_erase_value(erase_value), _blank_buf_size(0),
32-
_blank_buf(0), _bd(bd)
33+
_blank_buf(0), _bd(bd), _init_ref_count(0)
3334
{
3435
}
3536

@@ -41,6 +42,12 @@ FlashSimBlockDevice::~FlashSimBlockDevice()
4142

4243
int FlashSimBlockDevice::init()
4344
{
45+
uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1);
46+
47+
if (val != 1) {
48+
return BD_ERROR_OK;
49+
}
50+
4451
int ret = _bd->init();
4552
if (ret) {
4653
return ret;
@@ -55,6 +62,12 @@ int FlashSimBlockDevice::init()
5562

5663
int FlashSimBlockDevice::deinit()
5764
{
65+
uint32_t val = core_util_atomic_decr_u32(&_init_ref_count, 1);
66+
67+
if (val) {
68+
return BD_ERROR_OK;
69+
}
70+
5871
return _bd->deinit();
5972
}
6073

features/filesystem/bd/FlashSimBlockDevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class FlashSimBlockDevice : public BlockDevice {
135135
bd_size_t _blank_buf_size;
136136
uint8_t *_blank_buf;
137137
BlockDevice *_bd;
138+
uint32_t _init_ref_count;
138139
};
139140

140141
#endif

features/filesystem/bd/HeapBlockDevice.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@
1515
*/
1616

1717
#include "HeapBlockDevice.h"
18+
#include "mbed_critical.h"
1819

1920

2021
HeapBlockDevice::HeapBlockDevice(bd_size_t size, bd_size_t block)
2122
: _read_size(block), _program_size(block), _erase_size(block)
22-
, _count(size / block), _blocks(0)
23+
, _count(size / block), _blocks(0), _init_ref_count(0)
2324
{
2425
MBED_ASSERT(_count * _erase_size == size);
2526
}
2627

2728
HeapBlockDevice::HeapBlockDevice(bd_size_t size, bd_size_t read, bd_size_t program, bd_size_t erase)
2829
: _read_size(read), _program_size(program), _erase_size(erase)
29-
, _count(size / erase), _blocks(0)
30+
, _count(size / erase), _blocks(0), _init_ref_count(0)
3031
{
3132
MBED_ASSERT(_count * _erase_size == size);
3233
}
@@ -45,6 +46,12 @@ HeapBlockDevice::~HeapBlockDevice()
4546

4647
int HeapBlockDevice::init()
4748
{
49+
uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1);
50+
51+
if (val != 1) {
52+
return BD_ERROR_OK;
53+
}
54+
4855
if (!_blocks) {
4956
_blocks = new uint8_t*[_count];
5057
for (size_t i = 0; i < _count; i++) {
@@ -57,6 +64,12 @@ int HeapBlockDevice::init()
5764

5865
int HeapBlockDevice::deinit()
5966
{
67+
uint32_t val = core_util_atomic_decr_u32(&_init_ref_count, 1);
68+
69+
if (val) {
70+
return BD_ERROR_OK;
71+
}
72+
6073
MBED_ASSERT(_blocks != NULL);
6174
// Memory is lazily cleaned up in destructor to allow
6275
// data to live across de/reinitialization

features/filesystem/bd/HeapBlockDevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ class HeapBlockDevice : public BlockDevice
150150
bd_size_t _erase_size;
151151
bd_size_t _count;
152152
uint8_t **_blocks;
153+
uint32_t _init_ref_count;
153154
};
154155

155156

features/filesystem/bd/MBRBlockDevice.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include "MBRBlockDevice.h"
18+
#include "mbed_critical.h"
1819
#include <algorithm>
1920

2021

@@ -194,13 +195,19 @@ int MBRBlockDevice::partition(BlockDevice *bd, int part, uint8_t type,
194195
}
195196

196197
MBRBlockDevice::MBRBlockDevice(BlockDevice *bd, int part)
197-
: _bd(bd), _part(part)
198+
: _bd(bd), _part(part), _init_ref_count(0)
198199
{
199200
MBED_ASSERT(_part >= 1 && _part <= 4);
200201
}
201202

202203
int MBRBlockDevice::init()
203204
{
205+
uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1);
206+
207+
if (val != 1) {
208+
return BD_ERROR_OK;
209+
}
210+
204211
int err = _bd->init();
205212
if (err) {
206213
return err;
@@ -252,6 +259,12 @@ int MBRBlockDevice::init()
252259

253260
int MBRBlockDevice::deinit()
254261
{
262+
uint32_t val = core_util_atomic_decr_u32(&_init_ref_count, 1);
263+
264+
if (val) {
265+
return BD_ERROR_OK;
266+
}
267+
255268
return _bd->deinit();
256269
}
257270

features/filesystem/bd/MBRBlockDevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ class MBRBlockDevice : public BlockDevice
252252
bd_size_t _size;
253253
uint8_t _type;
254254
uint8_t _part;
255+
uint32_t _init_ref_count;
255256
};
256257

257258

0 commit comments

Comments
 (0)