Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 7d9ac95c7d557bb0531b2c7cb563f31277a41537 @wedesoft committed Dec 2, 2009
Showing with 1,103 additions and 0 deletions.
  1. +679 −0 COPYING
  2. +51 −0 Makefile
  3. +3 −0 README
  4. +20 −0 ext/error.cc
  5. +67 −0 ext/error.hh
  6. +4 −0 ext/extconf.rb
  7. +36 −0 ext/init.cc
  8. +88 −0 ext/malloc.cc
  9. +32 −0 ext/malloc.hh
  10. +59 −0 lib/malloc_ext.rb
  11. +33 −0 malloc.gemspec
  12. +31 −0 tests/ts_malloc.rb
679 COPYING

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -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 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
+
@@ -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;
@@ -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
@@ -0,0 +1,4 @@
+require 'mkmf'
+CONFIG[ 'LDSHARED' ] = "$(CXX) -shared"
+create_makefile 'malloc'
+
@@ -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" );
+ }
+
+}
@@ -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;
+}
@@ -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
@@ -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
Oops, something went wrong.

0 comments on commit 7d9ac95

Please sign in to comment.