Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Initial commit
  • Loading branch information
wedesoft committed Dec 2, 2009
0 parents commit 7d9ac95
Show file tree
Hide file tree
Showing 12 changed files with 1,103 additions and 0 deletions.
679 changes: 679 additions & 0 deletions COPYING

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions Makefile
@@ -0,0 +1,51 @@
.SUFFIXES:
.SUFFIXES: .gem .o .cc .hh .rb .tar .gz .bz2

RUBY_VERSION = 1.8

GEM = gem$(RUBY_VERSION)
RUBY = ruby$(RUBY_VERSION)
TAR = tar
TEST = -t

MAIN = Makefile malloc.gemspec README COPYING
EXT = ext/extconf.rb $(wildcard ext/*.cc) $(wildcard ext/*.hh)
LIB = $(wildcard lib/*.rb)
TESTS = $(wildcard tests/*.rb)
DOC = $(wildcard doc/*.rb)
SOURCES = $(MAIN) $(EXT) $(LIB) $(TESTS) $(DOC)

all:: malloc-1.0.0-x86-linux.gem

check:: ext/malloc.so $(LIB) $(TESTS)
$(RUBY) -Iext -Ilib $(TESTS)

install:: malloc-1.0.0-x86-linux.gem
$(GEM) install $(TEST) $<

uninstall::
$(GEM) uninstall malloc || echo Nothing to uninstall

dist:: malloc.tar.gz

dist-gzip:: malloc.tar.gz

dist-bzip2:: malloc.tar.bz2

malloc-1.0.0-x86-linux.gem: malloc.gemspec README $(EXT) $(LIB) $(TESTS) $(DOC)
$(GEM) build malloc.gemspec

ext/Makefile: ext/extconf.rb
cd ext && $(RUBY) extconf.rb && cd ..

ext/malloc.so: ext/Makefile $(EXT)
cd ext && $(MAKE) && cd ..

malloc.tar.gz: $(SOURCES)
$(TAR) czf $@ $(SOURCES)

malloc.tar.bz2: $(SOURCES)
$(TAR) cjf $@ $(SOURCES)

clean::
rm -f *~ ext/*~ ext/*.o ext/*.so ext/Makefile lib/*~ lib/*.so tests/*~ doc/*~ *.gem
3 changes: 3 additions & 0 deletions README
@@ -0,0 +1,3 @@
http://rubyforge.org/tracker/index.php?func=detail&aid=27502&group_id=126&atid=575
http://rubyforge.org/tracker/index.php?func=detail&aid=27503&group_id=126&atid=575

20 changes: 20 additions & 0 deletions ext/error.cc
@@ -0,0 +1,20 @@
/* HornetsEye - Computer Vision with Ruby
Copyright (C) 2006, 2007, 2008, 2009 Jan Wedekind
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, see <http://www.gnu.org/licenses/>. */
#include "error.hh"

using namespace std;

string Error::temp;
67 changes: 67 additions & 0 deletions ext/error.hh
@@ -0,0 +1,67 @@
/* HornetsEye - Computer Vision with Ruby
Copyright (C) 2006, 2007, 2008, 2009 Jan Wedekind
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, see <http://www.gnu.org/licenses/>. */
/* HornetsEye - Computer Vision with Ruby
Copyright (C) 2006, 2007 Jan Wedekind
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, see <http://www.gnu.org/licenses/>. */
#ifndef ERROR_HH
#define ERROR_HH

#include <exception>
#include <sstream>
#include <string>

class Error: public std::exception
{
public:
Error(void) {}
Error( Error &e ): std::exception( e )
{ m_message << e.m_message.str(); }
virtual ~Error(void) throw() {}
template< typename T >
std::ostream &operator<<( const T &t )
{ m_message << t; return m_message; }
std::ostream &operator<<( std::ostream& (*__pf)( std::ostream&) )
{ (*__pf)( m_message ); return m_message; }
virtual const char* what(void) const throw() {
temp = m_message.str();
return temp.c_str();
return NULL;
}
protected:
std::ostringstream m_message;
static std::string temp;
};

#define ERRORMACRO( condition, class, params, message ) \
if ( !( condition ) ) { \
class _e params; \
_e << message; \
throw _e; \
};

#endif
4 changes: 4 additions & 0 deletions ext/extconf.rb
@@ -0,0 +1,4 @@
require 'mkmf'
CONFIG[ 'LDSHARED' ] = "$(CXX) -shared"
create_makefile 'malloc'

36 changes: 36 additions & 0 deletions ext/init.cc
@@ -0,0 +1,36 @@
/* HornetsEye - Computer Vision with Ruby
Copyright (C) 2006, 2007, 2008, 2009 Jan Wedekind
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, see <http://www.gnu.org/licenses/>. */
#include "malloc.hh"

#ifdef WIN32
#define DLLEXPORT __declspec(dllexport)
#define DLLLOCAL
#else
#define DLLEXPORT __attribute__ ((visibility("default")))
#define DLLLOCAL __attribute__ ((visibility("hidden")))
#endif

extern "C" DLLEXPORT void Init_malloc(void);

extern "C" {

void Init_malloc(void)
{
Malloc::init();
rb_require( "malloc_ext.rb" );
}

}
88 changes: 88 additions & 0 deletions ext/malloc.cc
@@ -0,0 +1,88 @@
/* HornetsEye - Computer Vision with Ruby
Copyright (C) 2006, 2007, 2008, 2009 Jan Wedekind
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, see <http://www.gnu.org/licenses/>. */
#include "error.hh"
#include "malloc.hh"

#include <iostream>
using namespace std;

#ifdef WIN32
#define DLLEXPORT __declspec(dllexport)
#define DLLLOCAL
#else
#define DLLEXPORT __attribute__ ((visibility("default")))
#define DLLLOCAL __attribute__ ((visibility("hidden")))
#endif

VALUE Malloc::cRubyClass = Qnil;

VALUE Malloc::init(void)
{
cRubyClass = rb_define_class( "Malloc", rb_cObject );
rb_define_singleton_method( cRubyClass, "new",
RUBY_METHOD_FUNC( mallocNew ), 1 );
rb_define_method( cRubyClass, "+",
RUBY_METHOD_FUNC( mallocPlus ), 1 );
rb_define_method( cRubyClass, "read",
RUBY_METHOD_FUNC( mallocRead ), 1 );
rb_define_method( cRubyClass, "write",
RUBY_METHOD_FUNC( mallocWrite ), 1 );
return cRubyClass;
}

VALUE Malloc::mallocNew( VALUE rbClass, VALUE rbSize )
{
VALUE retVal = Qnil;
try {
unsigned int size = NUM2UINT( rbSize );
char *self = ALLOC_N( char, size );
ERRORMACRO( self != NULL, Error, , "Failed to allocate " << size
<< " bytes of memory" );
retVal = Data_Wrap_Struct( rbClass, 0, xfree, (void *)self );
} catch( std::exception &e ) {
rb_raise( rb_eRuntimeError, e.what() );
};
return retVal;
}

VALUE Malloc::mallocPlus( VALUE rbSelf, VALUE rbOffset )
{
VALUE retVal = Qnil;
try {
char *self; Data_Get_Struct( rbSelf, char, self );
int offset = NUM2INT( rbOffset );
ERRORMACRO( offset >= 0, Error, , "Offset must be non-negative (but was "
<< offset << ')' );
retVal = Data_Wrap_Struct( cRubyClass, 0, 0, (void *)( self + offset ) );
} catch( std::exception &e ) {
rb_raise( rb_eRuntimeError, e.what() );
};
return retVal;
}

VALUE Malloc::mallocRead( VALUE rbSelf, VALUE rbLength )
{
char *self; Data_Get_Struct( rbSelf, char, self );
unsigned int length = NUM2UINT( rbLength );
return rb_str_new( self, length );
}

VALUE Malloc::mallocWrite( VALUE rbSelf, VALUE rbString )
{
char *self; Data_Get_Struct( rbSelf, char, self );
memcpy( self, StringValuePtr( rbString ), RSTRING_LEN( rbString ) );
return rbString;
}
32 changes: 32 additions & 0 deletions ext/malloc.hh
@@ -0,0 +1,32 @@
/* HornetsEye - Computer Vision with Ruby
Copyright (C) 2006, 2007, 2008, 2009 Jan Wedekind
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, see <http://www.gnu.org/licenses/>. */
#ifndef MALLOC_HH
#define MALLOC_HH

#include <ruby.h>

class Malloc
{
public:
static VALUE cRubyClass;
static VALUE init(void);
static VALUE mallocNew( VALUE rbClass, VALUE rbSize );
static VALUE mallocPlus( VALUE rbSelf, VALUE rbOffset );
static VALUE mallocRead( VALUE rbSelf, VALUE rbLength );
static VALUE mallocWrite( VALUE rbSelf, VALUE rbString );
};

#endif
59 changes: 59 additions & 0 deletions lib/malloc_ext.rb
@@ -0,0 +1,59 @@
class String

unless method_defined? :bytesize

def bytesize
size
end

end

end

class Malloc

class << self

alias_method :orig_new, :new

def new( size )
retval = orig_new size
retval.instance_eval { @size = size }
retval
end

end

attr_reader :size

alias_method :orig_plus, :+

def +( offset )
if offset > @size
raise "Offset must not be more than #{@size} (but was #{offset})"
end
mark, size = self, @size - offset
retval = orig_plus offset
retval.instance_eval { @mark, @size = mark, size }
retval
end

alias_method :orig_read, :read

def read( length )
raise "Only #{@size} bytes of memory left to read " +
"(illegal attempt to read #{length} bytes)" if length > @size
orig_read length
end

alias_method :orig_write, :write

def write( string )
if string.bytesize > @size
raise "Must not write more than #{@size} bytes of memory " +
"(illegal attempt to write #{string.bytesize} bytes)"
end
orig_write string
end

end

0 comments on commit 7d9ac95

Please sign in to comment.