/* SPDX-License-Identifier: MIT */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "ringbuf.h"

#ifdef DEBUG
#define DEBUG_PRINT(fmt, ...) printf(fmt, __VA_ARGS__)
#else
#define DEBUG_PRINT(fmt, ...)
#endif

void
rb_init(struct RingBuf *rb, rb_size_t capacity, ALLOC_T malloc_fn,
        rb_size_t struct_size)
{
  rb->struct_size = struct_size;
  rb->capacity = capacity;
  rb->buffer = malloc_fn(capacity * struct_size); /* Calloc? */
  rb->buffer_end = rb->buffer + (capacity * struct_size);
  rb->count = 0;
  rb->write_head = rb->buffer;
  rb->read_head = rb->buffer;

  // Read from buffer at max position to force a segfault if theres an issue
  DEBUG_PRINT("Reading from buffer at position %d\n",
              rb->capacity * rb->struct_size);
  void *top = rb->buffer + (rb->capacity * rb->struct_size);
  DEBUG_PRINT("Buffer top successfully read at virtual address: %p\n", &top);

  DEBUG_PRINT(
      "Initialized ring buffer. Capacit: %d, struct_size: %d, total: %d\n",
      rb->capacity, rb->struct_size, rb->capacity * rb->struct_size);

  DEBUG_PRINT("Size of RB: %lu\n", sizeof(struct RingBuf));
}

void
rb_destroy(struct RingBuf *rb, void(free)())
{
  free(rb->buffer);
}

enum WriteResult
rb_push_back(struct RingBuf *rb, const void *item, MEMCPY_T memcpy_fn)
{
  if(rb->count == rb->capacity)
    return Full;

  memcpy_fn(rb->write_head, item, rb->struct_size);

  // Advance the write head
  rb->write_head = (char *)rb->write_head + rb->struct_size;

  if(rb->write_head == rb->buffer_end)
    rb->write_head = rb->buffer;

  rb->count++;
  return WriteOk;
}

enum ReadResult
rb_pop_front(struct RingBuf *rb, void *item)
{
  if(rb->count == 0)
    return Empty;

  memcpy(item, rb->read_head, rb->struct_size);

  // Advance the read head
  rb->read_head = (char *)rb->read_head + rb->struct_size;

  if(rb->read_head == rb->buffer_end)
    rb->read_head = rb->buffer;

  rb->count--;
  return ReadOk;
}