#include "mm_mark_and_sweep.h"
#include "external/tinycthread.h"
#include "utils.h"

/*
Copyright (c) 2013-2016 Gustav Hartvigsson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */

/* ************************************************************************** */

/* Since this has to be a totaly seperate system from the rest of libssts
 * we can not use SDynamicArray or SLinkedList. We have to re-invent the wheel
 * for this. :-)
 */

typedef struct SMMMarkAndSweep {
  spointer * array; /* The array that holds the pointers */
  size_t len;
  sint last;
  mtx_t * mutex;
} SMMMarkAndSweep;

static SMMMarkAndSweep *
_internal_s_mm_mark_and_sweep_object = NULL;

void
s_mm_mark_and_sweep_init () {
  if (_internal_s_mm_mark_and_sweep_object != NULL) {
    return;
  }
  SMMMarkAndSweep * self = malloc (sizeof (SMMMarkAndSweep));
  self->array = malloc (sizeof (spointer) * S_MM_MARK_AND_SWEEP_ARRAY_LEN);
  self->len = S_MM_MARK_AND_SWEEP_ARRAY_LEN;
  self->last = -1;
  self->mutex = malloc (sizeof (mtx_t));
  
  mtx_init (self->mutex, mtx_plain);
  
  _internal_s_mm_mark_and_sweep_object = self;
}

void
s_mm_mark_and_sweep_cleanup () {
  if (_internal_s_mm_mark_and_sweep_object == NULL) {
    return;
  }
  SMMMarkAndSweep * self = _internal_s_mm_mark_and_sweep_object;
  
  s_mm_mark_and_sweep_sweep ();
  
  free (self->array);
  mtx_destroy (self->mutex);
  free (self->mutex);
  free (self);
}


void
s_mm_mark_and_sweep_add_pointer_to_list (spointer pointer) {
  if (_internal_s_mm_mark_and_sweep_object == NULL) {
    return;
  }
  SMMMarkAndSweep * self = _internal_s_mm_mark_and_sweep_object;
  
  self->last = self->last + 1;
  
  mtx_lock (self->mutex);
  
  /*
   * we have to re allocate the array to a bigger value.
   */
  if (self->last >= self->len) {
    self->len = self->len + (self->len / 2);
    self->array = realloc (self->array, self->len); 
  }
  self->array[self->last] = pointer;
  
  mtx_unlock (self->mutex);
}

void
s_mm_mark_and_sweep_sweep () {
  if (_internal_s_mm_mark_and_sweep_object == NULL) {
    return;
  }
  SMMMarkAndSweep * self = _internal_s_mm_mark_and_sweep_object;
  
  mtx_lock (self->mutex);
  for (/* self->last */;self->last >= 0; self->last = self->last - 1) {
    // s_dbg_print ("Sweeping Momory!\n");
    free (self->array[self->last]);
  }
  mtx_unlock (self->mutex);
}





