Eubonicode++: Hackin C++ Gangsta Style

On December 3, 2011, in Code Monkey, by Tom

A little part of me died doing this to Bjarne, but I couldn't resist.

“Foshizzle” is a modified version of the Clang compiler which supports Eubonicode++ (an alternate C++ syntax). Foshizzle supports all the existing features of Clang, but with extensions to more fully express your gansta style. All the regular C++ syntax still works, but you can now substitute Eubonicode++ keywords and operators (see below).

Here’s a sample method that calculates factorials (Note: the semicolons can be replaced with a certain expletive):

int factorialz(int n)
{
    int rezult be 1;
    slongas (n bepimpin 0)
    {
        rezult be rezult dimes n;
        dissin n;
    }
    putou rezult;
}

 

I can’t take all the credit. I was inspired by the Iowa State students that first created Eubonicode (webpage no longer available).

Here are my Clang modifications: ebpp-clang30rc4.patch

Build instructions are near the end of this post.

Continue reading »

Tagged with:  

Adding stdint.h to Visual Studio

On November 13, 2011, in Code Monkey, by Tom

Yes, stdint.h and inttypes.h are missing from Microsoft Visual Studio. As a result, anyone who’s written cross-platform C\C++ code for Windows has probably seen this error:

error C2146: syntax error : missing ';' before identifier 'foo'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

The fact is that Visual Studio doesn’t implement C99… it implements C89 (for compatibility reasons). So, you don’t have access to familiar types like:

  • int8_t, int16_t, int32_t, int64_t
  • uint8_t, uint16_t, uint32_t, uint64_t
  • INT8_MIN, INT8_MAX, etc
  • The fprintf macros PRId32, PRIu32, etc
  • strtoimax(), etc

Fortunately, Alexander Chemeris has written msinttypes: an implementation of stdint.h and inttypes.h for Microsoft Visual Studio. It is licensed under the BSD license, so the headers are actually usable commercially (no viral GPL). This following quote probably goes without saying (emphasis mine):

Note though, that just adding these headers does not make Visual Studio compiler fully C99 compliant.

This project has saved me a bunch of unwanted coding. Thanks Alex!

Tagged with:  

Here’s a video done by Ed Catmull (founder of Pixar) in 1972 while at the University of Utah. It’s purported to be the first digitally rendered film. It’s just amazing how far we’ve come since these early pioneering days.

The math that we take for granted for rendering 3D was being invented, real time, to create this video. (Ed’s credited for having working out that math to handle things like texture mapping, 3D anti-aliasing and z-buffering.)

The story behind the video and how it was found recently is pretty cool too. Props to Robby Ingebretsen for sharing this!


40 Year Old 3D Computer Graphics (Pixar, 1972) from Robby Ingebretsen on Vimeo.

Tagged with:  

Here’s a great interview with Microsoft Technical Fellow and author of the CLR garbage collector, Patrick Dussud.

How does GC, work, generally? Why is it important? The GC inside of the CLR is of a specfic type – ephemeral, concurrent (the server version has always been concuurent and now with Background GC on the client in CLR 4, GC is concurrent on the client as well, but there are differences…)

Tagged with:  

Thomas Becker has a great article explaining C++11′s rvalue references and how they allow move semantics and perfect forwarding.

I particularly liked his summary of the 3 things you need to remember about rvalue references:

  1. By overloading a function like this:
    
    void foo(X& x); // lvalue reference overload
    void foo(X&& x); // rvalue reference overload
    


    you can branch at compile time on the condition “is foo being called on an lvalue or an rvalue?” The primary (and for all practical purposes, the only) application of that is to overload the copy constructor and copy assignment operator of a class for the sake of implementing move semantics. If and when you do that, make sure to pay attention to exception handling, and use the new noexcept keyword as much as you can.

  2. std::move turns its argument into an rvalue.
  3. std::forward allows you to achieve perfect forwarding if you use it exactly as shown:
    
    template<typename T, typename Arg>
    shared_ptr<T> factory(Arg&& arg)
    {
        return shared_ptr<T>(new T(std::forward<Arg>(arg));
    }

I also like his definition of lvalues and rvalues in the introduction:

The original definition of lvalues and rvalues from the earliest days of C is as follows: An lvalue is an expression e that may appear on the left or on the right hand side of an assignment, whereas anrvalue is an expression that can only appear on the right hand side of an assignment.

In C++, this is still useful as a first, intuitive approach to lvalues and rvalues. However, C++ with its user-defined types has introduced some subtleties regarding modifiability and assignability that cause this definition to be incorrect. There is no need for us to go further into this. Here is an alternate definition which, although it can still be argued with, will put you in a position to tackle rvalue references: An lvalue is an expression that refers to a memory location and allows us to take the address of that memory location via the & operator. An rvalue is an expression that is not an lvalue. 

Tagged with:  

Connecting Linux to a Cisco VPN server using a PCF file is easy (even from within a VirtualBox virtual machine). First, I wouldn’t bother with Cisco’s Linux client… especially if you are running 64bit. You have to patch a source file and mod the Makefile. Using VPNC is so much easier.

NOTE: I only had the PCF file provided by work, and the group password was encrypted. If you know your group password, then you can just run VPNC directly or write a conf file yourself.

My setup:

  • Fedora 12 x86_64
  • Running in VirtualBox 4.1.6 with bridged networking (I didn’t try it with NAT)
  • Connecting to a Cisco VPN server at work

Connecting:

  1. Install VPNC (`sudo yum install vpnc` in Fedora)
  2. Download the pcf2vpnc Perl script (cached)
  3. Convert your Cisco PCF file to VPNC conf format: `perl pcf2vpnc company.pcf vpnc.conf`
  4. Connect to the VPN server: `sudo vpnc ./vpnc.conf` (you will be prompted for you username and password)
  5. (optional) Run `ifconfig` to see the tunnel interface that was created
eth0     Link encap:Ethernet  HWaddr 08:00:DE:AD:BE:EF
         inet addr:192.168.1.10  Bcast:192.168.1.255  Mask:255.255.255.0
         ...

lo       Link encap:Local Loopback
         inet addr:127.0.0.1  Mask:255.0.0.0
         ...

tun0     Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
         inet addr:10.200.100.1  P-t-P:10.220.116.219  Mask:255.255.255.255
         ...

Disconnecting:

  1. Run `sudo vpnc-disconnect` (don’t forget the `sudo`)

That’s it. Cheers!

 

CRC32: A Simple C++ Class

On June 22, 2011, in Code Monkey, by Tom

Cyclic Redundancy Checks are a quick and convenient way to verify if a data set has changed. They are not meant to be “secure” like a MD5 or SHA-1, but they offer a very practical solution to most problems. CRC’s are also useful for verifying if 2 data sets are equal. Here’s a simple class I use that’s efficient. Honestly, I don’t remember what flavor of CRC32 it implements (sorry), but it has always worked for what I’ve needed. Cheers!

static const uint32_t kCrc32Table[256] = {
    0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
    0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
    0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
    0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
    0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
    0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
    0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
    0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
    0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
    0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
    0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
    0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
    0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
    0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
    0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
    0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
    0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 

Continue reading »

Tagged with:  

Netem is a great tool for simulating a WAN connection, with all the expected latency, jitter, packet loss, duplication, and bandwidth limitations. These instructions walk you through setting up a machine that sits between your server and client that emulates the behavior of a WAN connection. I use Fedora 14, so your distro may be a little different, but hopefully this post gets you pointed in the right direction.

The picture below shows the 2 most common configurations for a Netem box:

Netem Box Setup

I’ve always used the first configuration, but it doesn’t really matter.

1. Find a Suitable System

  • Any “reasonable” machine that can run Fedora 14 (I use an old Pentium 4 box since I don’t need to simulate a high-speed link)
  • It must have 2 network interfaces
  • It’s nice to use a smaller box if you want it to be portable

2. Install bridge-utils

Make sure you are root, and run:

brctl

to see if bridge-utils is installed. If it isn’t, run:

yum install bridge-utils.

Continue reading »

Tagged with:  

Live555: Compiling with VS2008

On May 14, 2011, in Code Monkey, by Tom

Compiling Live555 Streaming Media with Visual Studio 2008 isn’t obvious. Using Cygwin or MinGW is just a pain and frankly unnecessary. There is no built-in support for VS solution files, because Windows support is a low priority for the Live555 community. This is evidenced by this forum response from Ross Finlayson:

>4. Is it possible to include a Visual Studio solution in the
>distrubution to make it more convenient for Windows developers to
>use live555?
>5. Is it possible for live555 to generate adequate makefiles for
>Windows systems with development environments newer than Visual
>Studio 2003?

I have no current plans to change this. (These days, fewer and fewer people seem to be using Windows for development of system software.)

Regardless, Live555 works fine on Windows and is actually quite easy to build. Simply do the following:

  1. Open the ‘win32config’ file and change the TOOLS32=... variable to your VS2008 install directory. For me, it’s TOOLS32=C:\Program Files\Microsoft Visual Studio 9.0\VC
  2. In ‘win32config’, modify the LINK_OPTS_0=... line from msvcirt.lib to msvcrt.lib. This fixes the link error:
    LINK : fatal error LNK1181: cannot open input file 'msvcirt.lib'
  3. Open the Visual Studio command prompt.
  4. From the ‘live’ source directory, run genWindowsMakefiles
  5. Now you’re ready to build. Simply run the following commands:
    cd liveMedia
    nmake /B -f liveMedia.mak
    cd ..\groupsock
    nmake /B -f groupsock.mak
    cd ..\UsageEnvironment
    nmake /B -f UsageEnvironment.mak
    cd ..\BasicUsageEnvironment
    nmake /B -f BasicUsageEnvironment.mak
    cd ..\testProgs
    nmake /B -f testProgs.mak
    cd ..\mediaServer
    nmake /B -f mediaServer.mak

That’s it. You should be good to go.

Tagged with:  

Lua: Get CPU Load on Linux

On May 2, 2011, in Code Monkey, by Tom

Here’s a Lua function that returns the percentage of CPU used on Linux. The load includes time in user space and system space. In case you’re curious, I used `top` to get the load for a couple reasons:

  1. It’s installed by default on most distros (as opposed to the sysstat tools).
  2. It gives the idle time in percentage normalized to 100% (as opposed to /proc/stat which requires knowledge of the jiffy’s interval, which can vary).
function cpu_load(interval)
   interval = interval or 1
   local f = io.popen('top -bp$$ -d ' .. interval .. ' -n 2')
   local s = f:read('*all')

   -- Find the idle times in the stdout output
   local tokens = s:gmatch(',%s*([%d%.]+)%%%s*id')

   -- We're interested in the 2nd 'top' idle time
   local ii = 1
   for idle in tokens do
      if ii == 2 then
         return 100.0 - tonumber(idle)
      end
      ii = ii + 1
   end
end

You can call it from the Lua shell as:

> load = cpu_load()
> print(load)
43.7

Tagged with: