In this task, you are required to implement a ring buffer. We have provided a template for you here. You should implement all functions in ringbuffer.h
. We also provide you with a main.c
, which contains several simple testcases.
Make sure that the compilation of the source code generates no errors/warnings and conformity to C89 standard. We have provided a Makefile
, you can run make
to compile your code. You can run make memcheck
to check whether there is any memory leak (you will need to install valgrind first). You can also run make clean
to delete all compiled files.
You are required to implement a ring buffer with the following rules:
The initial size of ring buffer is defined by RING_BUFFER_INIT_SIZE
, which is equal to 8.
When your buffer is full and you need to insert more data, you should increase the capacity of your buffer. When current capacity < 1024, new capacity should be current capacity * RING_BUFFER_GROW_FACTOR1
. When current capacity >= 1024, new capacity should be current capacity * RING_BUFFER_GROW_FACTOR2
.
When a larger buffer is allocated, you should copy data from the old buffer to the new one with all data arranged in correct order. By correct order we mean the data pointed by head pointer should be put at the first slot. Here is a simple example of what the ring buffer would be like before and after increasing capacity, where ^
is used to represent head and tail pointers.
before inserting 9
| 4 | 5 | 6 | 7 | 8 | 1 | 2 | 3 |
^ ^
after inserting 9
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | | | | | | | |
^ ^
If the input is invalid for any operation, you should return false. Make sure your program will not crash when input is invalid.
For all tasks assume that ringbuffer.h
will be provided in the working directory and your ringbuffer.c
will be copied to the current working directory.
Write a shell script named staticlib.sh
that when invoked, produces a static library named libringbuffer.a
from the source files ringbuffer.c
and ringbuffer.h
.
Then create a static-linked executable staticringbuffer
from libringbuffer.a
and test.c
. Do not execute the program in the shell script.
You have to use the command ar
to create the static library. Then use the command ld
to create the executable file.
Write a shell script named dynamiclib.sh
that when invoked, produces a dynamic-linked library named libringbuffer.so
from ringbuffer.c
.
You should use gcc to create a test.o
from test.c
, and use ld to link test.o
and libringbuffer.so
to create an executable called dynamicringbuffer
. But that might be a little difficult. So it is also just fine to just use gcc
for this task (2.2). Do not execute the program in the shell script.
You should submit a compressed file named as hw2.tar
to autolab.
The directory tree of your submission should look like the following :
├── ringbuffer.c
├── dynamiclib.sh
└── staticlib.sh
You can create your submission with make submit
, and it will create hw2.tar
for you, including all three files needed. DO NOT include main.c
in your submission.
The test environment on autolab is Ubuntu 16.04 with gcc 5.x