summaryrefslogtreecommitdiff
path: root/libgpython/runtime/py-garbage.c
blob: f98ad783724cf0c1ecc19d71cb73661da9bc8e4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

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

#include <stdarg.h>
#include <stdbool.h>

#include <gmp.h>
#include <mpfr.h>

#include <gpython/gpython.h>
#include <gpython/objects.h>
#include <gpython/vectors.h>
#include <gpython/garbage.h>

gpy_vector_t * gpy_garbage_vec;

void gpy_garbage_invoke (void)
{
  if( gpy_garbage_vec )
    {
      debug("garbage collector running...\n");
      gpy_object_t * p_obj = NULL_OBJECT;

      while ((p_obj = (gpy_object_t *)
	      gpy_vec_pop( gpy_garbage_vec )))
	{
	  gpy_garbage_free_obj( p_obj );
	}
      gpy_vec_free( gpy_garbage_vec );
      gpy_garbage_vec = NULL;
    }
}

void gpy_garbage_mark_obj__ (gpy_object_t * const sym)
{
  if( sym )
    {
      if( gpy_garbage_vec )
        {
	  gpy_vec_push( gpy_garbage_vec, sym );
        }
      else
        {
          gpy_garbage_vec = (gpy_vector_t *)
            gpy_malloc( sizeof(gpy_vector_t) );
          gpy_vec_init( gpy_garbage_vec );

          gpy_vec_push( gpy_garbage_vec, sym );
        }
    }
}

void gpy_garbage_invoke_sweep (gpy_vector_t * const context)
{
  signed long ctx_l = context->length;
  if( context )
    {
      debug("sweeping context table for garbage length <%i>...\n", ctx_l);
      gpy_hash_tab_t * ctx_idx = NULL;
      signed long idx = (ctx_l - 1);

      while (idx >= 0)
	{
	  ctx_idx = context->vector[ idx ];
	  gpy_hash_entry_t * s_arr = ctx_idx->array;

	  int i = 0; int len = (ctx_idx->length);
	  debug("table length <%i>!\n", len );
	  for( ; i<len; ++i )
	    {
	      gpy_hash_entry_t oe = s_arr[i];
	      if (oe.data)
		{
		  gpy_object_t * o = oe.data;
		  gpy_assert (o->T == TYPE_OBJECT_STATE);
		  debug ("object <%p> has ref count <%i>!\n",
			 (void *) o, o->o.object_state->ref_count);
		  
		  // If no references remain
		  if (o->o.object_state->ref_count <= 0)
		    {
		      gpy_garbage_mark_obj( o );
		      s_arr[i] = (gpy_hash_entry_t) { 0, NULL };
		    }
		}
	    }
	  idx--;
	}
    }
  gpy_garbage_invoke ();
}

void gpy_garbage_free_obj (gpy_object_t * x)
{
  gpy_assert(x);
  debug("deleting garbage object <%p>!\n", (void*)x );
  
  switch (x->T)
    {
    case TYPE_OBJECT_LIT:
      break;

    case TYPE_OBJECT_STATE:
      break;

    case TYPE_NULL:
      break;
    }

  gpy_free (x);
}

/* Cleanup the program for exit! */
void gpy_cleanup (void)
{
  debug("cleanup.......\n");

  gpy_garbage_invoke_sweep (gpy_namespace_vec);

  gpy_vec_free (gpy_primitives);
  gpy_vec_free (gpy_namespace_vec);

  mpfr_free_cache ();
}