tag:blogger.com,1999:blog-47590349588746375562023-11-15T06:00:42.857-08:00Stanislav Vitvitskiy's blogStanislav Vitvitskiyhttp://www.blogger.com/profile/07534939991293903525noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-4759034958874637556.post-90131134575862007152009-10-21T07:01:00.000-07:002009-10-21T07:36:52.791-07:00SSE vector operations in GCCThanks to GCC vector types one can do SSE vector operations without having to write any assembly.
The below code snippet:
<pre name="code" class="javascript">
#include <stdio.h>
typedef int v4si __attribute__ ((mode(V4SI)));
int main(int argc, char** argv) {
v4si a[10], b[10], c[10];
for(int i = 0; i < 10; i++) {
c[i] = a[i] + b[i];
}
}
</pre>
translates into the following assembly:
<pre name="code" class="c">
movdqa (%eax), %xmm1
...
movdqa (%eax), %xmm0
paddd %xmm1, %xmm0
</pre>Stanislav Vitvitskiyhttp://www.blogger.com/profile/07534939991293903525noreply@blogger.com0tag:blogger.com,1999:blog-4759034958874637556.post-83352090737525609732009-10-21T04:54:00.000-07:002009-10-21T04:57:45.710-07:00Linux profiling experienceAfter rambling around for a while I finally settled on Valgrind with kcachegdind GUI for profiling my C/C++ code under Linux.
On my Ubuntu box:
<pre>
sudo apt-get install valgrind
sudo apt-get install kcachegrind
valgrind --tool=callgrind <your program>
kcachegrind
</pre>
That's it!Stanislav Vitvitskiyhttp://www.blogger.com/profile/07534939991293903525noreply@blogger.com0tag:blogger.com,1999:blog-4759034958874637556.post-72322863786916400252009-05-08T14:35:00.000-07:002009-05-08T14:59:22.215-07:00Some XULRunner traps on Mac OS XRecently I've been fighting for quite a while with my XUL (Gecko runtime aka GRE) application on Mac OS X trying to make is 'speak' to java via LiveConnect.
I had couple of problems which other people might also run into:
<ul>
<li>I accidentally downloaded a PowerPC version of XULRunner to my Intel Mac. While it runs perfectly well (thanks to <a href="http://www.apple.com/rosetta/">Rosetta</a>) it implies that java is also a PowerPC version. To check which platform your xulrunner supports simply type:
<pre name="code" class="javascript">
file /Library/Frameworks/XUL.framework/Versions/Current/xulrunner-bin
</pre>
The output should contain something except PowerPC ('ppc').
</li>
<li>You may still experience problems connecting to java due to outdated java plugin. In this case you may wish to download <a href="http://javaplugin.sourceforge.net/">Java Embedding Plugin</a> which utilizes Apple's Safari plugin to do the job.</li>
<li>Because XULRunner is distributed as x86 32 bit binary you'll have to use an appropriate java compiled for x86 32 bit. This is either default Leopard Sun JDK 1.5.x or <a href="http://landonf.bikemonkey.org/static/soylatte/">Soylatte</a> ( an independent OpenJDK port for OS X).</li>
</ul>Stanislav Vitvitskiyhttp://www.blogger.com/profile/07534939991293903525noreply@blogger.com2tag:blogger.com,1999:blog-4759034958874637556.post-7516989759358231742009-04-30T14:24:00.000-07:002009-05-05T02:50:06.056-07:00Calling java from XUL applications<a href="https://developer.mozilla.org/en/XULRunner">XULRunner</a> is a technology from Mozilla that allows to create cross-platform GUI applications just as easy as a websites.
Today I was trying to use my java libraries inside of a XUL application. This is basically to be able to load a jar file and call java functions with my javascript code.
It turns out that the only ( and the best ) way to accomplish this is by using <a href="https://jdk6.dev.java.net/plugin2/liveconnect/">LiveConnect</a>, a feature of java plugin that allows to 'talk' to java. LiveConnect among other things is responsible for communication with java applets.
With LiveConnect one can instanciate arbitrary java class and use it in javascript. Any java objects returned from method calls will be accessible in javascript as well as any javascript objects passed to java methods will be accessible in java. To achieve this effect LiveConnect does two-way wrapping of objects, i.e. it wraps java object with javascript and the other way around.
More precisely, to instanciate a JVM object simply do this:
<pre name="code" class="javascript">
var javaObject = new java.lang.String('Hello');
</pre>
So to load classes from a library simply instanciate a classloader and then use it to instanciate the classes. Something like this:
<pre name="code" class="javascript">
function getAppPath(appName) {
var chromeRegistry = Components
.classes["@mozilla.org/chrome/chrome-registry;1"]
.getService(Components.interfaces.nsIChromeRegistry);
var uri =
Components.classes["@mozilla.org/network/standard-url;1"]
.createInstance(Components.interfaces.nsIURI);
uri.spec = "chrome://" + appName + "/content/";
var path = chromeRegistry.convertChromeURL(uri);
if (typeof(path) == "object") {
path = path.spec;
}
path = path.substring(0, path.indexOf("/chrome/") + 1);
return path;
};
var basePath = getAppPath('myapp');
var url = new java.net.URL(basePath + 'java/my.jar');
var cl = new java.net.URLClassLoader( [ url ] );
var aClass = java.lang.Class.forName("com.stan.XULDemo", true, cl);
var inst = aClass.newInstance();
// Calling 'sum' function from java
var a = inst.sum(2, 3);
alert(a); // Will alert '5'
// A function which we'll pass to java
// and have java call it later with a parameter
var callback = function(str) {
alert(str);
};
inst.callMeLater(callback, 'test');
</pre>
The code of com.stan.XULDemo.java will be the following:
<pre name="code" class="java">
package com.stan;
public calss XULDemo {
public int sum(int a, int b) {
return a + b;
}
public void callMeLater(final JSObject func, final String text) {
new Thread() {
public void run() {
try {
Thread.sleep(5000); // Sleep 5 seconds
} catch(Exception e) { }
// We are passed a javascript Function Object
// which has method 'call' used to actually call the
// function. The first parameter is 'this',
// followed by a set
// of actual function parameters
func.call("call", new Object[] { null, text });
}
}.start();
}
}
</pre>
This works out of the box with XULRunner.
Note. Currently there are troubles with LiveConnect 1.9 on Mac (jdk 1.6 u3) which I am looking at right now.Stanislav Vitvitskiyhttp://www.blogger.com/profile/07534939991293903525noreply@blogger.com8tag:blogger.com,1999:blog-4759034958874637556.post-9605040697176140792008-12-25T07:05:00.000-08:002009-05-05T02:55:59.923-07:00Multipart post in Ruby<div style="font-family: arial;">Uploading a file to a server is a common task in web-development practice. It requires you to create a special HTML form with enctype set to 'multipart/form-data' and include an input of type 'file' into it.</div>
<div style="font-family: arial;">However sometime you want to upload a file to some third-party service from your Ruby script on background. For this you'll need to properly construct the multipart POST request and send it out via Ruby net/http library.</div>
<div style="font-family: arial;">After playing a bit I came up with the following class. A very strong advantage of it is that it works well with big files ( such as videos ).</div>
<pre name="code" class="ruby">
class Multipart
def initialize( file_names )
@file_names = file_names
end
def post( to_url )
boundary = '----RubyMultipartClient' + rand(1000000).to_s + 'ZZZZZ'
parts = []
streams = []
@file_names.each do |param_name, filepath|
pos = filepath.rindex('/')
filename = filepath[pos + 1, filepath.length - pos]
parts << StringPart.new ( "--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"" + param_name.to_s + "\"; filename=\"" + filename + "\"\r\n" +
"Content-Type: video/x-msvideo\r\n\r\n")
stream = File.open(filepath, "rb")
streams << stream
parts << StreamPart.new (stream, File.size(filepath))
end
parts << StringPart.new ( "\r\n--" + boundary + "--\r\n" )
post_stream = MultipartStream.new( parts )
url = URI.parse( to_url )
req = Net::HTTP::Post.new(url.path)
req.content_length = post_stream.size
req.content_type = 'multipart/form-data; boundary=' + boundary
req.body_stream = post_stream
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
streams.each do |stream|
stream.close();
end
res
end
end
class StreamPart
def initialize( stream, size )
@stream, @size = stream, size
end
def size
@size
end
def read ( offset, how_much )
@stream.read ( how_much )
end
end
class StringPart
def initialize ( str )
@str = str
end
def size
@str.length
end
def read ( offset, how_much )
@str[offset, how_much]
end
end
class MultipartStream
def initialize( parts )
@parts = parts
@part_no = 0;
@part_offset = 0;
end
def size
total = 0
@parts.each do |part|
total += part.size
end
total
end
def read ( how_much )
if @part_no >= @parts.size
return nil;
end
how_much_current_part = @parts[@part_no].size - @part_offset
how_much_current_part = if how_much_current_part > how_much
how_much
else
how_much_current_part
end
how_much_next_part = how_much - how_much_current_part
current_part = @parts[@part_no].read(@part_offset, how_much_current_part )
if how_much_next_part > 0
@part_no += 1
@part_offset = 0
next_part = read ( how_much_next_part )
current_part + if next_part
next_part
else
''
end
else
@part_offset += how_much_current_part
current_part
end
end
end
</pre>Stanislav Vitvitskiyhttp://www.blogger.com/profile/07534939991293903525noreply@blogger.com0