diff --git a/CMakeLists.txt b/CMakeLists.txt index acb6548d159663d50cc9c4bb686b8a5ffd487123..83aacb135b543ed0c14b21479ad5d94a50789e5e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,5 +2,6 @@ cmake_minimum_required(VERSION 3.21) project(ring_buffer C) set(CMAKE_C_STANDARD 99) +link_libraries(pmemobj) add_executable(ring_buffer main.c) diff --git a/main.c b/main.c index a1afbfb9306481b2f76e02698d7bdbfd1f9a93ca..9f7394d8be9c6252831506a93e79479997f975b1 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,8 @@ #include <stdio.h> #include <stdbool.h> -#include <malloc.h> +#include <libpmemobj/types.h> +#include <libpmemobj/pool_base.h> +#include <unistd.h> struct ring_buffer{ unsigned int head; @@ -9,7 +11,17 @@ struct ring_buffer{ char buffer[4]; }; -bool buffer_has_space(struct ring_buffer *rb){ +struct pmem_obj_root{ + struct ring_buffer rb; +}; + +struct variaed_lenght_command{ + char *command_payload; + int command_lenght; +}; + + +bool buffer_has_space_for_command(struct ring_buffer *rb, int command_lenght){ signed int next_index = (rb->head+1) % rb->size; if (next_index == rb->tail){ return 1; @@ -21,6 +33,9 @@ bool buffer_is_empty(struct ring_buffer *rb){ return rb->head == rb->tail; } +/* TODO + * Update to get the number of commands for commands without fixed size. + * */ unsigned int get_number_of_entries(struct ring_buffer *rb){ signed int result = rb->head - rb->tail; if(result > 0){ @@ -33,6 +48,9 @@ unsigned int get_number_of_entries(struct ring_buffer *rb){ return 0; } +/* TODO + * Update to get the number of commands for commands without fixed size. + * */ void print_buffer_content(struct ring_buffer *rb){ unsigned int entries = get_number_of_entries(rb); printf("Number of elements: %d\n", entries); @@ -40,7 +58,9 @@ void print_buffer_content(struct ring_buffer *rb){ printf("Buffer entry: %c\n", rb->buffer[i]); } } - +/* TODO + * Update to get the number of commands for commands without fixed size. + * */ int insert(struct ring_buffer *rb, char command){ if(buffer_has_space(rb) == 0){ rb->buffer[rb->head] = command; @@ -51,32 +71,46 @@ int insert(struct ring_buffer *rb, char command){ return 1; } -char retrieve_command(struct ring_buffer *rb){ +/* TODO + * Update to get the number of commands for commands without fixed size. + * */ +char retrieve_command_at_index(struct ring_buffer *rb, int index){ + char tmp = rb->buffer[index]; if(buffer_is_empty(rb)){ printf("Buffer is empty\n"); return -1; } - char tmp = rb->buffer[rb->tail]; - rb->tail = rb->tail + 1; + rb->tail = index; return tmp; +} +bool file_exists(const char *path){ + return access(path, F_OK) != 0; } -char retrieve_command_at_index(struct ring_buffer *rb, int index){ - char tmp = rb->buffer[index]; - if(buffer_is_empty(rb)){ - printf("Buffer is empty\n"); - return -1; +struct ring_buffer * initialise_ring_buffer_on_persistent_memory(PMEMobjpool *pop){ + char *path_to_pmem = "/mnt/dax/test_outputs/pmem_log_ring_buffer"; + if (file_exists((path_to_pmem)) != 0) { + if ((pop = pmemobj_create(path_to_pmem, POBJ_LAYOUT_NAME(list),PMEMOBJ_MIN_POOL, 0666)) == NULL) + perror("failed to create pool\n"); + } else { + if ((pop = pmemobj_open(path_to_pmem, POBJ_LAYOUT_NAME(list))) == NULL) { + perror("failed to open pool\n"); + } } - rb->tail = index; - return tmp; + PMEMoid pool_root = pmemobj_root(pop, sizeof(struct pmem_obj_root)); + struct pmem_obj_root *rootp = pmemobj_direct(pool_root); + + return &rootp->rb; } int main() { - struct ring_buffer *rb = malloc(sizeof(struct ring_buffer)); + PMEMobjpool *pop; + struct ring_buffer *rb = initialise_ring_buffer_on_persistent_memory(pop); + rb->size =sizeof(rb->buffer); rb->head = 0; rb->tail = 0; - rb->size = 4; + printf("Initialized ring_buffer\n"); // When we establish communication we send the information about ring buffer to the consumer; //Consumer sends request to read command at the specific index