Apostolos\’ Daily Plate

Don’t use malloc with C++

Posted in Πληροφορική by asyropoulos on Δεκεμβρίου 8, 2012

OK this may not be news for experienced C++ programmers, but as a word of advice never use malloc in C++ programs. In particular, if one wants to implement dynamic data structures using pointers, she should use new instead of malloc. For example, consider the following declarations:

typedef struct {
  string env_name;
  int lineno;
} env_data;

struct node {
  env_data data;
  struct node *link;
};

typedef struct {
  struct node *head;
  struct node *list_node;
} Stack;

Then the following is not the way to allocate memory for a node

stack->list_node = (node *) malloc(sizeof(node));

The proper way to do this is simply:

stack->list_node = new node;

As an exercise I wrote a simple program that utilized a stack and initally I used malloc to create nodes, but the program crashed most of the times. When I replaced malloc with new, the program stopped crashing and gave the expected results. So don’t use malloc in C++, it’s for C programmers only.

Using pygame with Python 3.2.3

Posted in Πληροφορική, OpenIndiana by asyropoulos on Αυγούστου 1, 2012

Yesterday I tried to compile Python 3.2.3 on OpenIndiana. There was absolutely no problem. Then I tried to compile pygame but it failed. The reason is that certain symbols are no longer defined in Python 3.2.0 and later versions. To solve this problem one has to apply the following patch:

diff -crB pygame-1.9.1release.orig/src/pygame.h pygame-1.9.1release.new/src/pygame.h
*** pygame-1.9.1release.orig/src/pygame.h	2009-07-06 19:09:50.000000000 +0300
--- pygame-1.9.1release.new/src/pygame.h	2012-08-01 19:26:45.902639760 +0300
***************
*** 130,135 ****
--- 130,141 ----
  typedef getcharbufferproc charbufferproc;
  #endif

+ #if PY_VERSION_HEX >= 0x3020000
+ #define PyCObject_AsVoidPtr(P) (PyCapsule_GetPointer(P, 0))
+ #define PyCObject_FromVoidPtr(X, P) (PyCapsule_New(X, 0, P))
+ #define PyCObject_Check(P) (PyCapsule_CheckExact(P))
+ #endif
+ 
  #define PyType_Init(x) (((x).ob_type) = &PyType_Type)
  #define PYGAMEAPI_LOCAL_ENTRY "_PYGAME_C_API"

This solves all problems and pygame compiles and works just fine.

Installing/Upgrading OpenIndiana 151a with Greek as the Default Language

Posted in Πληροφορική, OpenIndiana by asyropoulos on Ιανουαρίου 12, 2012

When Openindiana 151a was released I tried to upgrade the OS that was running on my netbook. I was using Openindiana 148. Although the upgrade procedure went fine, it was not possible to boot the new system. The problem was that I was using the el_GR.UTF8 locale and somehow /usr/sbin/bootadm does not like this locale and so it core dumps. The solution was easier than I thought, but I had to do a fresh and clean install of 151a on an another machine in to order to see how to temporaly solve the problem. When I installed Openindiana 151a on the second system, I chose English as the default language. So the system booted with using the settings of the en_US.UTF-8 locale.  Then I installed the files described in Disabling IIIMF and  rebooted the system. To my surprise the Greek keyboard was fully functional! Then I just re-logged in using the Greek language as the default system language and the system worked as expected! In a nutshell, I had what I wanted!

I told this story to a friend of mine and he told me to check the files under /etc and see whether there is one that defines a locale. Indeed, file /etc/default/init contains roughly the following

TZ=Europe/Athens
CMASK=022
LANG=el_GR.UTF-8

So I just changed the value of the lanst parameter on the system of my netbook from el_GR.UTF-8  to en_US.UTF-8 and OI_151a booted without any problem! Now I am happily using 151a!

Compiling Perl 5.12.3 on OpenIndiana

Posted in Πληροφορική, OpenIndiana by asyropoulos on Μαρτίου 11, 2011

If one plans to compile Perl i386-pc-solaris2.11 there is nothing special to say: just run the Configure script as shown below

./Configure -Dcc=XXX

where XXX should be either gcc or cc,
and in most, if not all, cases just accept the suggested answers. However, things get a little bit tricky when trying to compile for   x86_64-pc-solaris2.11. First one needs to make shell script to call the C compiler with the -m64 option. In my system the corresponding scripts are shown below:

$ more /usr/bin/cc64 
#! /bin/bash
exec /usr/bin/cc -m64 "$@"
$ more /usr/bin/gcc64 
#! /bin/bash
exec /usr/bin/gcc -m64 "$@"
$

Obviously when invoking Configure the XXX parameter must be either gcc64 or cc64. If you want to build with GCC (I am using version 4.5.2) you need to correctly answer a question asked by Configure. IN particular when Configure asks

Any special flags to pass to x86_64-pc-solaris2.11-gcc to 
create a dynamically loaded library? [ -G -m64 -fstack-protector]

do not accept the suggested reply but instead enter:

-shared -m64 -fstack-protector

For solstudio12.2 there is nothing to say: it compiles just fine. The only annoying thing is that during compilation one sees times the following warning

cc: Warning: -xarch=generic64 is deprecated, use -m64 to create 64-bit programs

Someone must inform the Perl developers to update the configuration scripts.

How to format USB flash drives with FAT32

Posted in OpenIndiana, OpenSolaris by asyropoulos on Δεκεμβρίου 20, 2010

First find the device name that corresponds to the USB flash drive:

# rmformat
Looking for devices...
1. Logical Node: /dev/rdsk/c0t0d0p0
Physical Node: /pci@0,0/pci1458,5004@13,2/storage@1/disk@0,0
Connected Device: Kingston DataTraveler 2.0 6.16
Device Type: Removable
Bus: USB
Size: 979,0 MB
Label: <None>
Access permissions: Medium is not write protected.
2. Logical Node: /dev/rdsk/c13t1d0p0
Physical Node: /pci@0,0/pci1458,b002@11/cdrom@1,0
Connected Device: TSSTcorp CDDVDW SH-S203D  SB01
Device Type: DVD Reader/Writer
Bus: <Unknown>
Size: <Unknown>
Label: <Unknown>
Access permissions: <Unknown>

Second create a FAT32 partition using the following command:

fdisk /dev/rdsk/c0t0d0p0

Finally, construct the file system using the following command:

mkfs -F pcfs -o fat=32,b=apostolos /dev/rdsk/c0t0d0p0:c

Here what follows the b= part will become the name of the disk.

Disabling IIIMF

Posted in OpenIndiana, OpenSolaris by asyropoulos on Οκτωβρίου 23, 2010

OpenSolaris and the current version of OpenIndiana uses IIIMF (Internet Intranet Input Method Framework). Unfortunately, this tool does not function properly in OpenIndiana especially when Greek is the basic language. If one wants to get reed of IIIMF, then there are a number of things that must be done:

  1. Disable IIIMF from the System ▸ Preferences  ▸ Startup Applications Preferences.
  2. Create file /etc/hal/fdi/policy/10-x11-input.fdi that roughly contains the following:
    <?xml version="1.0" encoding="UTF-8"?>
    <deviceinfo version="0.2">
    <device>
    <match key="info.capabilities" contains="input.keys">
    <merge key="input.x11_options.XkbRules" type="string">base</merge>
    <merge key="input.x11_options.XkbLayout" type="string">us,gr</merge>
    <merge key="input.x11_options.XkbVariant" type="string">,polytonic</merge>
    <merge key="input.x11_options.XkbOptions"
     type="string">grp:alt_shift_toggle,terminate:ctrl_alt_bksp,kpdl:comma</merge>
    </match>
    </device>
    </deviceinfo>
  3. Replace file /usr/share/X11/locale/el_GR.UTF-8/Compose and file /usr/share/X11/locale/en_US.UTF-8/Compose with the actual Xorg Compose files from http://cgit.freedesktop.org/xorg/lib/libX11/ In particular, download the latest libX11 source and find the corresponding files in folder nls. Note that this folder contains files named Compose.pre. These files are not directly usable. One needs to “translate” all XCOMM strings to #s.
  4. Reboot your system and you are ready!

Περί απογραφής

Posted in Πληροφορική by asyropoulos on Ιουλίου 12, 2010

Ξεκίνησε σήμερα η ηλεκτρονική απογραφή των δημοσίων υπαλλήλων. Υπήρξαν δυσκολίες λόγω φόρτου αλλά αυτό ήταν κάτι αναμενόμενο. Προφανώς δεν μπορούμε να κατηγορήσουμε του υπεύθυνους για αυτό καθώς έχουν συγκεκριμένη υποδομή σε συγκεκριμένες εποχές… Όμως δεν δικαιολογούνται που η σελίδα «παίζει»μόνο με Internet Explorer και όχι με Firefox.  Επικοινώνησα μαζί τους και τους ανάφερα το πρόβλημα και μου απάντησαν, ευγενικά πρέπει να ομολογήσω, πως το έχουν ελέγξει και «παίζει» κανονικά. Σήμερα όμως που το κοίταξα, διαπίστωσα πως δεν «παίζει»! Το πρόβλημα νομίζω πως ξεκινάει από την υπόθεση πως ο μέσος χρήστης έχει Windows (XP κατά προτίμηση) και φυσικά Internet Exploder (όπως χαριτολογώντας τον έλεγε ένας φίλος). Αν όμως άλλαζε λίγο αυτή η υπόθεση και η ανάπτυξη είχε γίνει με την υπόθεση ότι ο χρήστης έχει Firefox (και ας αναφέρονται ρητά αυτό στην εισαγωγική σελίδα!) , θα εξυπηρετούνταν καλήτερα η «διαφήμιση» ότι η σελίδα φτιάχτηκε με ανοικτό λογισμικό…

ΥΓ Τελικά διορθώθηκε το πρόβλημα με τον Firefox. Φαίνεται πως επιτέλους κάποιοι ακούνε σε αυτή τη χώρα!

OpenSolaris: Compiling FFmpeg for 64-bit

Posted in OpenSolaris by asyropoulos on Ιουνίου 25, 2010

Compiling the SVN version of FFmpeg for 64-bit is not difficult, but one must a little bit careful. First of all, one needs to compile x264, xvid, and libnut  among others. In all cases, one must ensure that the libraries are compiled with the -fPIC -DPIC swicthes (even SunStudio recognizes these switches). For some reason, it is necessary to produce position-independent code (PIC)  and these switches ensure this. Once these additional libraries have been installed in the proper folders (e.g., /usr/local/lib/amd64), we need to compile FFmpeg. In order to configure, I have used the following command:

CFLAGS="-m64 -I/opt/gnu/include" \
LDFLAGS="-m64 -L/opt/gnu/lib/amd64 -R/opt/gnu/lib/amd64" \
./configure --prefix=/opt/gnu --bindir=/opt/gnu/bin/amd64
--shlibdir=/opt/gnu/lib/amd64 --libdir=/opt/gnu/lib/amd64 --enable-libopencore-amrnb\
--enable-libopencore-amrwb --enable-libfaac --enable-libmp3lame  --enable-libnut \
--enable-libspeex --enable-libtheora --enable-libvorbis --enable-libx264  --enable-libxvid \
--enable-zlib --enable-bzlib  --cc=cc --enable-pic --enable-shared --enable-version3 \
--enable-gpl --enable-nonfree --enable-x11grab  --disable-mmx --enable-postproc \
--extra-cflags="-fPIC -DPIC" --enable-pthreads

Note that here cc is Sun C 5.11 SunOS_i386 Aten 2010/05/10 , that is, the latest version of the SunStudio. The configuration process generates a number of files but two of them must be modified.  First, file config.h must be modified using the following patch:

--- config.h.orig	Παρ Ιουν 25 23:26:05 2010
+++ config.h	Τετ Ιουν 23 17:03:05 2010
@@ -28,7 +28,7 @@
 #define ARCH_SPARC 0
 #define ARCH_SPARC64 0
 #define ARCH_TOMI 0
-#define ARCH_X861
+#define ARCH_X86 0
 #define ARCH_X86_32 0
 #define ARCH_X86_64 1
 #define HAVE_ALTIVEC 0
@@ -45,7 +45,7 @@
 #define HAVE_NEON 0
 #define HAVE_PPC4XX 0
 #define HAVE_SSE 0
-#define HAVE_SSSE3 0
+#define HAVE_SSSE3 1
 #define HAVE_VIS 0
 #define HAVE_BIGENDIAN 0
 #define HAVE_PTHREADS 1

Also, file config.mak must be modified using the following patch:

--- config.mak.orig	Παρ Ιουν 25 23:34:57 2010
+++ config.mak	Τετ Ιουν 23 17:02:47 2010
@@ -12,7 +12,7 @@
 SRC_PATH="/opt/sources/BACKUP/MPLAYER/ffmpeg"
 SRC_PATH_BARE=/opt/sources/BACKUP/MPLAYER/ffmpeg
 BUILD_ROOT="/opt/sources/BACKUP/MPLAYER/ffmpeg"
-ARCH=x86
+ARCH=x86_64
 CC=cc
 AS=cc
 LD=cc
@@ -30,7 +30,7 @@
 CC_O=-o $@
 LDFLAGS= -m64 -L/opt/gnu/lib/amd64 -R/opt/gnu/lib/amd64
 FFSERVERLDFLAGS=
-SHFLAGS=-mimpure-text -shared -Wl,-h,$$(@F)
+SHFLAGS=-shared -Wl,-h,$$(@F)
 YASMFLAGS=-f elf -DARCH_X86_64 -m amd64 -DPIC -g dwarf2
 BUILDSUF=
 FULLNAME=$(NAME)$(BUILDSUF)

Once these steps have been taken, compilation should complete with no errors.

OpenSolaris: Compiling clisp-2.48 for 64bit

Posted in OpenSolaris by asyropoulos on Απριλίου 19, 2010

Compiling clisp-2.48 for 64bit OpenSolaris is a little bit tricky.


$ mkdir clisp; cd clisp
$ clisp_basedir=`pwd`
$ clisp_toolsdir=$clisp_basedir/clisp-tools
$ clisp_builddir=$clisp_basedir/clisp-build
$ mkdir $clisp_toolsdir

Now download libsigsegv and ffcal and unpack the archives and proceed:


$ cd libsigsegv-2.8   
$ CC="gcc-4.3.2 -m64" ./configure \
   --prefix=$clisp_toolsdir && make && \
   make check &&  make install
$ cd $clisp_basedir
$ cd ffcall-1.10
$ CC="gcc-4.3.2 -m64" ./configure \
  --build=x86_64-pc-solaris2.11 \
  --prefix=$clisp_toolsdir && \
  make && make check && \
  make install
$ cd $clisp_basedir
$ cd clisp-2.48
$ CC="gcc-4.3.2 -m64" \
  CFLAGS="-I/tmp/clisp/clisp-tools/include" \
  LDFLAGS="-L/tmp/clisp/clisp-tools/lib 
-R/tmp/clisp/clisp-tools/lib"  ./configure \ 
  --with-ffcall --with-libffcall-prefix=$clisp_toolsdir \
  --with-libsigsegv-prefix=$clisp_toolsdir --disable-mmap \
  --without-readline $clisp_builddir && \
cd $clisp_builddir && make

At some point it the compilation process stops because two symbols are undefined. However, these symbols are defined in libgnu.a. For some strange reason even if one adds the name of the library in the Makefile, the compiler fails to find the library. For this reason, I had to do the following dirty trick:


$ cd boot
$ mkdir foo
$ cp lisp.a lisp.a.orig
$ mv lisp.a foo/.
$ cp ../gllib/.libs/libgnu.a foo/.
$ cd foo
$ for lib in *.a; do ar x $lib; done
$ ar cr ../lisp.a *.o
$ ranlib ../lisp.a
$ cd ..
$ rm -rf foo
$ cd ..
$ make

Now the compilation of clisp completes succesfully! Note that the same trick should be applied even when compiling for 32bit OpenSolaris.

OpenSolaris: Backing up DVDs

Posted in OpenSolaris by asyropoulos on Απριλίου 18, 2010

I buy a lot of DVDs since I do prefer to watch moview and TV shows on my TV set. However, I prefer not to view the original DVDs in order to keep them with no scratches. To do this I need to back up them. For this I use vobcopy (you can get binaries from www.sunfreepacks.com) and DVDShrink (this is not the official site of the software!) with wine. Once you have installed vobcopy and wine, you can install DVDShrink by entering the command

$wine dvdshrink32setup.exe

Here is what you should do in order to “rip” a DVD:

  1. Insert your DVD to your DVD drive and enter the comand:
    $ vobcopy -i /media/NAME_OF_DVD -I

    to see  that vobcopy works.

  2. “Rip” the DVD with the command
    $vobcopy -i /media/NAME_OF_DVD -m -o /destination/folder
  3. Start up DVDShrink and choose the /destination/folder and create shrinked copy of the DVD.
  4. Burn the resulting image (I assume you have asked DVDShrink to create a DVD image file).

That’s all!

PS It is now possible to use backlite, a Qt only version of k9copy. The packages (i.e., backlite and Qt)  will be available from sunfreepacks really soon.

Follow

Get every new post delivered to your Inbox.