Skip to content

Commit 27d5d3f

Browse files
author
Kristian Kinderlöv
committed
Change to have only one return out from a function
1 parent 899d004 commit 27d5d3f

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

circularbuffer.c

+16-6
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ int32_t CircularBufferPushBack(CircularBufferContext *ctx, const void *val) {
7575
ASSERT(ctx);
7676
ASSERT(val);
7777

78+
int32_t retval = 0;
7879
const size_t write_pos = (ctx->write_pos + 1) & ctx->max_size;
7980

8081
// Check if full
@@ -85,16 +86,20 @@ int32_t CircularBufferPushBack(CircularBufferContext *ctx, const void *val) {
8586
memcpy(&ctx->buf[ctx->write_pos * ctx->element_size], val, ctx->element_size);
8687
ctx->write_pos = write_pos;
8788

88-
return 0;
89+
goto success;
8990

9091
fail:
91-
return -1;
92+
retval = -1;
93+
success:
94+
return retval;
9295
}
9396

9497
int32_t CircularBufferPopFront(CircularBufferContext *ctx, void *val) {
9598
ASSERT(ctx);
9699
ASSERT(val);
97100

101+
int32_t retval = 0;
102+
98103
// Check if empty
99104
if (ctx->read_pos == ctx->write_pos) {
100105
goto fail;
@@ -104,15 +109,18 @@ int32_t CircularBufferPopFront(CircularBufferContext *ctx, void *val) {
104109

105110
ctx->read_pos = (ctx->read_pos + 1) & ctx->max_size;
106111

107-
return 0;
112+
goto success;
108113

109114
fail:
110-
return -1;
115+
retval = -1;
116+
success:
117+
return retval;
111118
}
112119

113120
int32_t CircularBufferPeek(const CircularBufferContext *ctx, size_t num, void **elem) {
114121
ASSERT(ctx);
115122

123+
int32_t retval = 0;
116124
const size_t write_pos = ctx->write_pos;
117125
const size_t read_pos = ctx->read_pos;
118126
const size_t size = ((write_pos - read_pos) & ctx->max_size);
@@ -126,10 +134,12 @@ int32_t CircularBufferPeek(const CircularBufferContext *ctx, size_t num, void **
126134
const size_t element_pos = ((read_pos + num) & ctx->max_size);
127135
*elem = &ctx->buf[element_pos * ctx->element_size];
128136

129-
return 0;
137+
goto success;
130138

131139
fail:
132-
return -1;
140+
retval = -1;
141+
success:
142+
return retval;
133143
}
134144

135145
size_t CircularBufferSize(const CircularBufferContext *ctx) {

0 commit comments

Comments
 (0)