+ 2

Refactor - Circle Buffer

Made some adjustment to the circle buffer file. Changed the buffer from a struct with a pointer to the buffer to a struct using a Flexable Array Member to keep the memory inline. How else can we fix this? https://code.sololearn.com/cpBWFOKyi6mL

27th Feb 2022, 9:54 PM
William Owens
William Owens - avatar
5 Answers
+ 3
Looks great. The type is opaque, typedef'ed, internal functions have a restricted visibility, code is clear and verbose in and of itself ... A few notes: - some symbols that you export are rather generic (read, write) which may cause problems - assert means to me to assert a condition that is always true. That a buffer with positive capacity is requested it not always true and subject to data provided that you cannot control. A return NULL may be an option there. The rest would probably be micro optimisation. - Put it to a valgrind test to monitor memory footprint and find leaks - run a heavy load test using a profiler to find performance bottlenecks, - be sure most likely branches are close to the test condition so that jumps in the code segment are avoided. (You can also hint the compiler which branch is likely to optimize compilation). But for all that you should first ascertain that there are performance bottlenecks and a necessity for improvement. Otherwise I'd say Well done! šŸ˜€šŸ‘šŸ‘Œ
28th Feb 2022, 8:10 AM
Ani Jona šŸ•Š
Ani Jona šŸ•Š - avatar
+ 2
Sorry mate, whilst I have the enthusiasm to help on this, I have neither the knowledge nor the aptitude to make any sort of meaningful improvement!!! Just one question: what is it supposed to do?
28th Feb 2022, 4:14 AM
HungryTradie
HungryTradie - avatar
+ 2
Ani Jona Thank you for the suggestions and comments. I do have one question. I heard this before but do not understand it. 'type is opaque'. What does that mean? Hungry Tradie It is the functions to create and use a circular buffer data structure. ''' [ _, _, _] [a, _, _] [a, b, _] [a, b, c] [d, b, c] [d, e, c] [d, e, f] ''' of any size but lets say 3. The first data entered is the first data written over (FIFO) if the buffer is full and overwrite is permitted. To my knowledge they are used where memory is limited or for something like keeping latest data only and not needing to save the older data ie Keyboard buffer, ethernet frames are some good examples. <- That pesky '\n' trouble with scanf. Also a read of the data would 'delete' the data: ''' r [_, _, _] w r [a, _, _] <- writes a to [0] w r [a, b, _] <- writes b to [1] w r [_, b, _] <- reads a from [0] w '''
28th Feb 2022, 1:27 PM
William Owens
William Owens - avatar
+ 2
Opaque type just means that one cannot look inside it :). The details of the structure are hidden, manipulation of state happens through designated functions that come with it. The advantage is that other code does not depend on the inner structure of the type which provides some independence. The exported functions are like an interface into the library binary. The disadvantage is less flexibility. You can only use the type in the way the exported functions allow.
28th Feb 2022, 3:35 PM
Ani Jona šŸ•Š
Ani Jona šŸ•Š - avatar
+ 1
Thanks Brian. I think that is very likely to be in my immediate future. My upcoming training module is working with limited memory logic controllers, like in a domestic microwave. Seems like the concept of a bit shift register in a PLC. We use a individual bit to represent a logic state, eg presence of a part in an automated conveyor/machine. As the part advances through the stations, eg from stamping to shaping, the bit advances through the byte. 0 0 0 1 0 0 0 1 would be an object in station 5 and another entering at the first station. Often done with proximity sensors or other non-contact detection. When you read from the buffer as in your example, would it shift remaining values backwards, or would it leave a hole?
28th Feb 2022, 8:13 PM
HungryTradie
HungryTradie - avatar