// SPDX-License-Identifier: MPL-2.0
#include "std/queue.h"
#include "std/malloc.h"
#include "std/smp.h"
SH_STATUS sh_queue_event_init(sh_queue_KBD_EVENT *q,sh_uint32 capacity) {
  if (q==SH_NULLPTR || capacity==0) return SH_STATUS_INVALID_PARAMETER;
  q->buffer=(sh_kbd_EVENT*)sh_malloc(sizeof(sh_kbd_EVENT)*capacity);
  if (!q->buffer) return SH_STATUS_ERROR_NULLPTR_RETURNED;
  q->capacity=capacity;
  q->write_index=0;
  return SH_STATUS_SUCCESS;
}
SH_STATUS sh_queue_event_push(sh_queue_KBD_EVENT *q,sh_kbd_EVENT ev) {
  if (q==SH_NULLPTR || q->buffer==SH_NULLPTR) return SH_STATUS_INVALID_PARAMETER;
  sh_uint32 idx=q->write_index % q->capacity;
  q->buffer[idx]=ev;
  sh_mb();
  q->write_index++;
  return SH_STATUS_SUCCESS;
}
SH_STATUS sh_queue_event_destroy(sh_queue_KBD_EVENT *q) {
  if (q==SH_NULLPTR || q->buffer==SH_NULLPTR) return SH_STATUS_INVALID_PARAMETER;
  sh_free(q->buffer);
  if (sh_status_error(sh_malloc_get_last_status())) return sh_malloc_get_last_status();
  q->capacity=0;
  q->write_index=0;
  return SH_STATUS_SUCCESS;
}
