/* reaper.c generated by valac 0.22.1, the Vala compiler
 * generated from reaper.vala, do not modify */


#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gstdio.h>
#include <unistd.h>
#include <stdio.h>


#define TYPE_REAPER (reaper_get_type ())
#define REAPER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_REAPER, Reaper))
#define REAPER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_REAPER, ReaperClass))
#define IS_REAPER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_REAPER))
#define IS_REAPER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_REAPER))
#define REAPER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_REAPER, ReaperClass))

typedef struct _Reaper Reaper;
typedef struct _ReaperClass ReaperClass;
typedef struct _ReaperPrivate ReaperPrivate;
#define _g_source_unref0(var) ((var == NULL) ? NULL : (var = (g_source_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _Reaper {
	GObject parent_instance;
	ReaperPrivate * priv;
};

struct _ReaperClass {
	GObjectClass parent_class;
};

struct _ReaperPrivate {
	gchar** _args;
	gint _args_length1;
	gint __args_size_;
	GSource* _timeout;
};


static gpointer reaper_parent_class = NULL;

GType reaper_get_type (void) G_GNUC_CONST;
#define REAPER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_REAPER, ReaperPrivate))
enum  {
	REAPER_DUMMY_PROPERTY,
	REAPER_ARGS,
	REAPER_TIMEOUT
};
Reaper* reaper_new (gchar** args, int args_length1);
Reaper* reaper_construct (GType object_type, gchar** args, int args_length1);
gchar** reaper_get_args (Reaper* self, int* result_length1);
static void reaper_set_args (Reaper* self, gchar** value, int value_length1);
static gchar** _vala_array_dup27 (gchar** self, int length);
GSource* reaper_get_timeout (Reaper* self);
static void reaper_set_timeout (Reaper* self, GSource* value);
static GObject * reaper_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
static gboolean _reaper___lambda16_ (Reaper* self);
static gboolean __reaper___lambda16__gsource_func (gpointer self);
static void reaper_finalize (GObject* obj);
static void _vala_reaper_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);
static void _vala_reaper_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);
static gint _vala_array_length (gpointer array);


Reaper* reaper_construct (GType object_type, gchar** args, int args_length1) {
	Reaper * self = NULL;
	gchar** _tmp0_ = NULL;
	gint _tmp0__length1 = 0;
	GSource* _tmp1_ = NULL;
	GSource* _tmp2_ = NULL;
	_tmp0_ = args;
	_tmp0__length1 = args_length1;
	_tmp1_ = g_timeout_source_new ((guint) (60 * 1000));
	_tmp2_ = _tmp1_;
	self = (Reaper*) g_object_new (object_type, "args", _tmp0_, "timeout", _tmp2_, NULL);
	_g_source_unref0 (_tmp2_);
	return self;
}


Reaper* reaper_new (gchar** args, int args_length1) {
	return reaper_construct (TYPE_REAPER, args, args_length1);
}


gchar** reaper_get_args (Reaper* self, int* result_length1) {
	gchar** result;
	gchar** _tmp0_ = NULL;
	gint _tmp0__length1 = 0;
	gchar** _tmp1_ = NULL;
	gint _tmp1__length1 = 0;
	g_return_val_if_fail (IS_REAPER (self), NULL);
	_tmp0_ = self->priv->_args;
	_tmp0__length1 = self->priv->_args_length1;
	_tmp1_ = _tmp0_;
	_tmp1__length1 = _tmp0__length1;
	if (result_length1) {
		*result_length1 = _tmp1__length1;
	}
	result = _tmp1_;
	return result;
}


static gchar** _vala_array_dup27 (gchar** self, int length) {
	gchar** result;
	int i;
	result = g_new0 (gchar*, length + 1);
	for (i = 0; i < length; i++) {
		gchar* _tmp0_ = NULL;
		_tmp0_ = g_strdup (self[i]);
		result[i] = _tmp0_;
	}
	return result;
}


static void reaper_set_args (Reaper* self, gchar** value, int value_length1) {
	gchar** _tmp0_ = NULL;
	gint _tmp0__length1 = 0;
	gchar** _tmp1_ = NULL;
	gint _tmp1__length1 = 0;
	g_return_if_fail (IS_REAPER (self));
	_tmp0_ = value;
	_tmp0__length1 = value_length1;
	_tmp1_ = (_tmp0_ != NULL) ? _vala_array_dup27 (_tmp0_, _tmp0__length1) : ((gpointer) _tmp0_);
	_tmp1__length1 = _tmp0__length1;
	self->priv->_args = (_vala_array_free (self->priv->_args, self->priv->_args_length1, (GDestroyNotify) g_free), NULL);
	self->priv->_args = _tmp1_;
	self->priv->_args_length1 = _tmp1__length1;
	self->priv->__args_size_ = self->priv->_args_length1;
	g_object_notify ((GObject *) self, "args");
}


GSource* reaper_get_timeout (Reaper* self) {
	GSource* result;
	GSource* _tmp0_ = NULL;
	g_return_val_if_fail (IS_REAPER (self), NULL);
	_tmp0_ = self->priv->_timeout;
	result = _tmp0_;
	return result;
}


static gpointer _g_source_ref0 (gpointer self) {
	return self ? g_source_ref (self) : NULL;
}


static void reaper_set_timeout (Reaper* self, GSource* value) {
	GSource* _tmp0_ = NULL;
	GSource* _tmp1_ = NULL;
	g_return_if_fail (IS_REAPER (self));
	_tmp0_ = value;
	_tmp1_ = _g_source_ref0 (_tmp0_);
	_g_source_unref0 (self->priv->_timeout);
	self->priv->_timeout = _tmp1_;
	g_object_notify ((GObject *) self, "timeout");
}


static gboolean _reaper___lambda16_ (Reaper* self) {
	gboolean result = FALSE;
	GError * _inner_error_ = NULL;
	{
		gchar* status = NULL;
		gchar* _tmp0_ = NULL;
		glong pages = 0L;
		const gchar* _tmp1_ = NULL;
		gchar** _tmp2_ = NULL;
		gchar** _tmp3_ = NULL;
		gchar** _tmp4_ = NULL;
		gint _tmp4__length1 = 0;
		const gchar* _tmp5_ = NULL;
		glong _tmp6_ = 0L;
		glong _tmp7_ = 0L;
		glong pagesize = 0L;
		glong _tmp8_ = 0L;
		glong _tmp9_ = 0L;
		glong _tmp10_ = 0L;
		g_file_get_contents ("/proc/self/statm", &_tmp0_, NULL, &_inner_error_);
		_g_free0 (status);
		status = _tmp0_;
		if (_inner_error_ != NULL) {
			_g_free0 (status);
			goto __catch14_g_error;
		}
		_tmp1_ = status;
		_tmp3_ = _tmp2_ = g_strsplit (_tmp1_, " ", 0);
		_tmp4_ = _tmp3_;
		_tmp4__length1 = _vala_array_length (_tmp2_);
		_tmp5_ = _tmp4_[1];
		_tmp6_ = atol (_tmp5_);
		_tmp7_ = _tmp6_;
		_tmp4_ = (_vala_array_free (_tmp4_, _tmp4__length1, (GDestroyNotify) g_free), NULL);
		pages = _tmp7_;
		_tmp8_ = sysconf (_SC_PAGESIZE);
		pagesize = _tmp8_;
		_tmp9_ = pagesize;
		_tmp10_ = pages;
		if ((_tmp9_ * _tmp10_) > ((glong) ((50 * 1000) * 1000))) {
			gchar** _tmp11_ = NULL;
			gint _tmp11__length1 = 0;
			gchar** _tmp12_ = NULL;
			gint _tmp12__length1 = 0;
			const gchar* _tmp13_ = NULL;
			gchar** _tmp14_ = NULL;
			gint _tmp14__length1 = 0;
			gchar** _tmp15_ = NULL;
			gint _tmp15__length1 = 0;
			_tmp11_ = reaper_get_args (self, &_tmp11__length1);
			_tmp12_ = _tmp11_;
			_tmp12__length1 = _tmp11__length1;
			_tmp13_ = _tmp12_[0];
			_tmp14_ = reaper_get_args (self, &_tmp14__length1);
			_tmp15_ = _tmp14_;
			_tmp15__length1 = _tmp14__length1;
			execvp (_tmp13_, _tmp15_);
		}
		_g_free0 (status);
	}
	goto __finally14;
	__catch14_g_error:
	{
		GError* e = NULL;
		FILE* _tmp16_ = NULL;
		GError* _tmp17_ = NULL;
		const gchar* _tmp18_ = NULL;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp16_ = stderr;
		_tmp17_ = e;
		_tmp18_ = _tmp17_->message;
		fprintf (_tmp16_, "Could not determine memory use: %s\n", _tmp18_);
		_g_error_free0 (e);
	}
	__finally14:
	if (_inner_error_ != NULL) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return FALSE;
	}
	result = TRUE;
	return result;
}


static gboolean __reaper___lambda16__gsource_func (gpointer self) {
	gboolean result;
	result = _reaper___lambda16_ (self);
	return result;
}


static GObject * reaper_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	GObjectClass * parent_class;
	Reaper * self;
	GSource* _tmp0_ = NULL;
	GSource* _tmp1_ = NULL;
	parent_class = G_OBJECT_CLASS (reaper_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_REAPER, Reaper);
	_tmp0_ = self->priv->_timeout;
	g_source_attach (_tmp0_, NULL);
	_tmp1_ = self->priv->_timeout;
	g_source_set_callback (_tmp1_, __reaper___lambda16__gsource_func, g_object_ref (self), g_object_unref);
	return obj;
}


static void reaper_class_init (ReaperClass * klass) {
	reaper_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (ReaperPrivate));
	G_OBJECT_CLASS (klass)->get_property = _vala_reaper_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_reaper_set_property;
	G_OBJECT_CLASS (klass)->constructor = reaper_constructor;
	G_OBJECT_CLASS (klass)->finalize = reaper_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), REAPER_ARGS, g_param_spec_boxed ("args", "args", "args", G_TYPE_STRV, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
	g_object_class_install_property (G_OBJECT_CLASS (klass), REAPER_TIMEOUT, g_param_spec_pointer ("timeout", "timeout", "timeout", G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
}


static void reaper_instance_init (Reaper * self) {
	self->priv = REAPER_GET_PRIVATE (self);
}


static void reaper_finalize (GObject* obj) {
	Reaper * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_REAPER, Reaper);
	self->priv->_args = (_vala_array_free (self->priv->_args, self->priv->_args_length1, (GDestroyNotify) g_free), NULL);
	_g_source_unref0 (self->priv->_timeout);
	G_OBJECT_CLASS (reaper_parent_class)->finalize (obj);
}


/******************************************************************************
 * Copyright (C) 2013  Michael Hofmann <mh21@mh21.de>                         *
 *                                                                            *
 * This program 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 of the License, or          *
 * (at your option) any later version.                                        *
 *                                                                            *
 * This program 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 this program; if not, write to the Free Software Foundation, Inc.,    *
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                *
 ******************************************************************************/
GType reaper_get_type (void) {
	static volatile gsize reaper_type_id__volatile = 0;
	if (g_once_init_enter (&reaper_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (ReaperClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) reaper_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Reaper), 0, (GInstanceInitFunc) reaper_instance_init, NULL };
		GType reaper_type_id;
		reaper_type_id = g_type_register_static (G_TYPE_OBJECT, "Reaper", &g_define_type_info, 0);
		g_once_init_leave (&reaper_type_id__volatile, reaper_type_id);
	}
	return reaper_type_id__volatile;
}


static void _vala_reaper_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	Reaper * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, TYPE_REAPER, Reaper);
	switch (property_id) {
		case REAPER_ARGS:
		{
			int length;
			g_value_set_boxed (value, reaper_get_args (self, &length));
		}
		break;
		case REAPER_TIMEOUT:
		g_value_set_pointer (value, reaper_get_timeout (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void _vala_reaper_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec) {
	Reaper * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, TYPE_REAPER, Reaper);
	switch (property_id) {
		case REAPER_ARGS:
		{
			gpointer boxed;
			boxed = g_value_get_boxed (value);
			reaper_set_args (self, boxed, g_strv_length (boxed));
		}
		break;
		case REAPER_TIMEOUT:
		reaper_set_timeout (self, g_value_get_pointer (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	if ((array != NULL) && (destroy_func != NULL)) {
		int i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}


static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}


static gint _vala_array_length (gpointer array) {
	int length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}



