<commit_before>/**
 * fuq - a Fundamentally Unstable Queue
 *
 * fuq handles single consumer, single producer scenarios where only one
 * thread is pushing data to the queue and another is shifting out.
 *
 * There is the case of a "false negative". Meaning an item can be in process
 * of being pushed into the queue while the other end is shifting data out
 * and receives NULL. Indicating that the queue is empty.
 */

#ifndef NUB_FUQ_H_
#define NUB_FUQ_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdlib.h>  /* malloc, free */
#include <stdio.h>   /* fprintf, fflush */

#define FUQ_ARRAY_SIZE 511
#define FUQ_MAX_STOR 1024

/* hardware memory barrier */
/* TODO: Check gcc version >= 4.4.0 */
#if defined(__GNUC__)
  #define FUQ_MEM_BAR() __sync_synchronize()
#elif defined(_MSC_VER)
  #include <winnt.h>
  #define FUQ_MEM_BAR() MemoryBarrier()
#else
  #error "System lacks hardware memory barrier support"
#endif

/* simplify check if OOM */
#define FUQ_CHECK_OOM(pntr)                                                   \
  do {                                                                        \
    if (NULL == (pntr)) {                                                     \
      fprintf(stderr, "FATAL: OOM - %s:%i\n", __FILE__, __LINE__);            \
      fflush(stderr);                                                         \
      abort();                                                                \
    }                                                                         \
  } while (0)

/* The last slot is reserved as a pointer to the next fuq__array. */
typedef void* fuq__array[FUQ_ARRAY_SIZE + 1];

typedef struct {
  fuq__array* head_array;
  fuq__array* tail_array;
  int head_idx;
  int tail_idx;
  /* These are key to allowing single atomic operations. */
  void** head;
  void** tail;
  /* Storage containers for unused allocations. */
  fuq__array* head_stor;
  fuq__array* tail_stor;
  /* Number of fuq__array's currently stored. */
  int max_stor;
} fuq_queue;


static inline fuq__array* fuq__alloc_array(fuq_queue* queue) {
  fuq__array* array;
  fuq__array* tail_stor;

  tail_stor = queue->tail_stor;
  FUQ_MEM_BAR();

  if (tail_stor == queue->head_stor) {
    array = (fuq__array*) (malloc(sizeof(*array)));
    FUQ_CHECK_OOM(array);
  } else {
    array = queue->head_stor;
    queue->head_stor = (fuq__array*) ((*array)[1]);
    queue->max_stor -= 1;
  }

  return array;
}


static inline void fuq__free_array(fuq_queue* queue, fuq__array* array) {
  if (FUQ_MAX_STOR > queue->max_stor) {
    free((void*) (array));
    return;
  }

  (*array)[1] = NULL;
  (*queue->tail_stor)[1] = array;
  queue->max_stor += 1;

  FUQ_MEM_BAR();
  queue->tail_stor = array;
}


static inline void fuq_init(fuq_queue* queue) {
  fuq__array* array;
  fuq__array* stor;

  array = (fuq__array*) (malloc(sizeof(*array)));
  FUQ_CHECK_OOM(array);
  stor = (fuq__array*) (malloc(sizeof(*stor)));
  FUQ_CHECK_OOM(stor);
  /* Initialize in case fuq_dispose() is called immediately after fuq_init(). */
  (*stor)[1] = NULL;

  queue->head_array = array;
  queue->tail_array = array;
  queue->head_idx = 0;
  queue->tail_idx = 0;
  queue->head = &(**array);
  queue->tail = &(**array);
  queue->head_stor = stor;
  queue->tail_stor = stor;
  queue->max_stor = 0;
}


static inline void fuq_push(fuq_queue* queue, void* arg) {
  fuq__array* array;
  void* tail;

  *queue->tail = arg;
  queue->tail_idx += 1;

  if (FUQ_ARRAY_SIZE > queue->tail_idx) {
    tail = &((*queue->tail_array)[queue->tail_idx]);
    FUQ_MEM_BAR();
    queue->tail = (void**) (tail);
    return;
  }

  array = fuq__alloc_array(queue);
  (*queue->tail_array)[queue->tail_idx] = (void*) (array);
  queue->tail_array = array;
  queue->tail_idx = 0;

  tail = &(**array);
  FUQ_MEM_BAR();
  queue->tail = (void**) (tail);
}


static inline void* fuq_shift(fuq_queue* queue) {
  fuq__array* next_array;
  void** tail;
  void* ret;

  tail = queue->tail;
  FUQ_MEM_BAR();

  if (queue->head == tail)
    return NULL;

  ret = *queue->head;
  queue->head_idx += 1;
  queue->head = &((*queue->head_array)[queue->head_idx]);

  if (FUQ_ARRAY_SIZE > queue->head_idx)
    return ret;

  next_array = (fuq__array*) (*queue->head);
  fuq__free_array(queue, queue->head_array);
  queue->head = &(**next_array);
  queue->head_array = next_array;
  queue->head_idx = 0;

  return ret;
}


/* Useful for cleanup at end of applications life to make valgrind happy. */
static inline void fuq_dispose(fuq_queue* queue) {
  void* next_array;

  while (queue->head_array != queue->tail_array) {
    next_array = (*queue->head_array)[FUQ_ARRAY_SIZE];
    free((void*) (queue->head_array));
    queue->head_array = (fuq__array*) next_array;
  }

  free((void*) (queue->head_array));

  if (NULL == queue->head_stor)
    return;

  do {
    next_array = (*queue->head_stor)[1];
    free((void*) (queue->head_stor));
    queue->head_stor = (fuq__array*) next_array;
  } while (NULL != next_array);
}


#undef FUQ_GCC_VERSION
#undef FUQ_ARRAY_SIZE
#undef FUQ_MAX_STOR
#undef FUQ_MEM_BAR
#undef FUQ_CHECK_OOM

#ifdef __cplusplus
}
#endif
#endif  /* NUB_FUQ_H_ */<commit_msg>Code cleanup<commit_after>/**
 * fuq - a Fundamentally Unstable Queue
 *
 * fuq handles single consumer, single producer scenarios where only one
 * thread is pushing data to the queue and another is shifting out.
 *
 * There is the case of a "false negative". Meaning an item can be in process
 * of being pushed into the queue while the other end is shifting data out
 * and receives NULL. Indicating that the queue is empty.
 */

#ifndef NUB_FUQ_H_
#define NUB_FUQ_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdlib.h>  /* malloc, free */
#include <stdio.h>   /* fprintf, fflush */

#define FUQ_ARRAY_SIZE 511
#define FUQ_MAX_STOR 1024

/* hardware memory barrier */
/* TODO: Check gcc version >= 4.4.0 */
#if defined(__GNUC__)
  #define FUQ_MEM_BAR() __sync_synchronize()
#elif defined(_MSC_VER)
  #include <winnt.h>
  #define FUQ_MEM_BAR() MemoryBarrier()
#else
  #error "Hardware memory barrier support not implemented on this system"
#endif

/* Simplify check if OOM */
#define FUQ_CHECK_OOM(pntr)                                                   \
  do {                                                                        \
    if (NULL == (pntr)) {                                                     \
      fprintf(stderr, "FATAL: OOM - %s:%i\n", __FILE__, __LINE__);            \
      fflush(stderr);                                                         \
      abort();                                                                \
    }                                                                         \
  } while (0)

/* The last slot is reserved as a pointer to the next fuq__array. */
typedef void* fuq__array[FUQ_ARRAY_SIZE + 1];

typedef struct {
  fuq__array* head_array;
  fuq__array* tail_array;
  int head_idx;
  int tail_idx;
  /* These are key to allowing single atomic operations. */
  void** head;
  void** tail;
  /* Storage containers for unused allocations. */
  fuq__array* head_stor;
  fuq__array* tail_stor;
  /* Number of fuq__array's currently stored. */
  int max_stor;
} fuq_queue;


static inline fuq__array* fuq__alloc_array(fuq_queue* queue) {
  fuq__array* array;
  fuq__array* tail_stor;

  tail_stor = queue->tail_stor;
  FUQ_MEM_BAR();

  if (tail_stor == queue->head_stor) {
    array = (fuq__array*) malloc(sizeof(*array));
    FUQ_CHECK_OOM(array);
  } else {
    array = queue->head_stor;
    queue->head_stor = (fuq__array*) (*array)[1];
    queue->max_stor -= 1;
  }

  return array;
}


static inline void fuq__free_array(fuq_queue* queue, fuq__array* array) {
  if (FUQ_MAX_STOR > queue->max_stor) {
    free((void*) array);
    return;
  }

  (*array)[1] = NULL;
  (*queue->tail_stor)[1] = array;
  queue->max_stor += 1;

  FUQ_MEM_BAR();
  queue->tail_stor = array;
}


static inline void fuq_init(fuq_queue* queue) {
  fuq__array* array;
  fuq__array* stor;

  array = (fuq__array*) malloc(sizeof(*array));
  FUQ_CHECK_OOM(array);
  stor = (fuq__array*) malloc(sizeof(*stor));
  FUQ_CHECK_OOM(stor);
  /* Initialize in case fuq_dispose() is called immediately after fuq_init(). */
  (*stor)[1] = NULL;

  queue->head_array = array;
  queue->tail_array = array;
  queue->head_idx = 0;
  queue->tail_idx = 0;
  queue->head = &(**array);
  queue->tail = &(**array);
  queue->head_stor = stor;
  queue->tail_stor = stor;
  queue->max_stor = 0;
}


static inline void fuq_push(fuq_queue* queue, void* arg) {
  fuq__array* array;
  void* tail;

  *queue->tail = arg;
  queue->tail_idx += 1;

  if (FUQ_ARRAY_SIZE > queue->tail_idx) {
    tail = &((*queue->tail_array)[queue->tail_idx]);
    FUQ_MEM_BAR();
    queue->tail = (void**) tail;
    return;
  }

  array = fuq__alloc_array(queue);
  (*queue->tail_array)[queue->tail_idx] = (void*) array;
  queue->tail_array = array;
  queue->tail_idx = 0;

  tail = &(**array);
  FUQ_MEM_BAR();
  queue->tail = (void**) tail;
}


static inline void* fuq_shift(fuq_queue* queue) {
  fuq__array* next_array;
  void** tail;
  void* ret;

  tail = queue->tail;
  FUQ_MEM_BAR();

  if (queue->head == tail)
    return NULL;

  ret = *queue->head;
  queue->head_idx += 1;
  queue->head = &((*queue->head_array)[queue->head_idx]);

  if (FUQ_ARRAY_SIZE > queue->head_idx)
    return ret;

  next_array = (fuq__array*) *queue->head;
  fuq__free_array(queue, queue->head_array);
  queue->head = &(**next_array);
  queue->head_array = next_array;
  queue->head_idx = 0;

  return ret;
}


/* Useful for cleanup at end of applications life to make valgrind happy. */
static inline void fuq_dispose(fuq_queue* queue) {
  void* next_array;

  while (queue->head_array != queue->tail_array) {
    next_array = (*queue->head_array)[FUQ_ARRAY_SIZE];
    free((void*) queue->head_array);
    queue->head_array = (fuq__array*) next_array;
  }

  free((void*) queue->head_array);

  if (NULL == queue->head_stor)
    return;

  do {
    next_array = (*queue->head_stor)[1];
    free((void*) queue->head_stor);
    queue->head_stor = (fuq__array*) next_array;
  } while (NULL != next_array);
}


#undef FUQ_ARRAY_SIZE
#undef FUQ_MAX_STOR
#undef FUQ_MEM_BAR
#undef FUQ_CHECK_OOM

#ifdef __cplusplus
}
#endif
#endif  /* NUB_FUQ_H_ */
{'fraction_non_alphanumeric': 0.1349067599067599, 'fraction_numerical': 0.005050505050505051, 'mean_word_length': 2.7402833272793314, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 121, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '383491', 'n_tokens_mistral': 3743, 'n_tokens_neox': 3579, 'n_words': 870}