• mina86.com

  • Categories
  • Code
  • Contact
  • Lazy Proxy in Python

    Posted by Michał ‘mina86’ Nazarewicz on 8th of July 2012

    Paths of destiny lead mysterious ways. Not so long ago, I was a hard-core C hacker and now, I spend a lot of the time coding in Python.

    In somehow related news, I have discovered that my search-foo is not good enough, when I was unable to find a decent implementations of several design patterns in Python.

    What I needed was a generic proxy that would defer initialisation of an object to the moment it is first used. Here is what I came up with:

    class LazyProxy(object):
        __slots__ = '__get'
    
        def __init__(self, cls, *args, **kw):
            object.__setattr__(self, '_LazyProxy__get',
                               lambda: self.__set(cls(*args, **kw)))
    
        def __set(self, obj):
            object.__setattr__(self, '_LazyProxy__get', lambda: obj)
            return obj
    
        def __getattr__(self, name):
            return getattr(self.__get(), name)
    
        def __setattr__(self, name, value):
            return setattr(self.__get(), name, value)
    
        def __delattr__(self, name):
            return delattr(self.__get(), name)

    Deep Dive into Contiguous Memory Allocator

    Posted by Michał ‘mina86’ Nazarewicz on 10th of June 2012

    This is an extended version of an LWN article on CMA. It contains more detail on how to use CMA and a lot of boring code samples.

    Contiguous Memory Allocator (or CMA) has been developed to allow large physically contiguous memory allocations. By initialising early at boot time and with some fairly intrusive changes to Linux memory management, it is able to allocate large memory chunks without a need to grab memory for exclusive use.

    Simple in principle, it grew to be a quite complicated system which requires cooperation between boot-time allocator, buddy system, DMA subsystem, and some architecture-specific code. Still, all that complexity is usually hidden away and normal users won’t be exposed to it. Depending on perspective, CMA appears slightly different and there are different things to be done and look for.

    Null: The never-ending story

    Posted by Michał ‘mina86’ Nazarewicz on 27th of March 2011

    I have already mentioned some problems with the null pointer but my recent discovery knocked my socks off.

    By now, it should come with no surprise to anyone that 0 in pointer context acts as a null pointer (no matter of its actual representation). Moreover, it takes only a tiny bit of experimenting to figure out that expressions like (int)0 do as well. The latter is in itself a bit of a pita but it is conforming to the C++ standard which says:

    Prime numbers less than 100

    Posted by Michał ‘mina86’ Nazarewicz on 12th of December 2010

    Anyone working in a major company must have been hit by some ‘funny’ mail from a coworker that helps everyone gets through the day. No different at my office — at one point all engineers have been challenged to write the shortest code in C that prints all prime numbers (and only prime numbers) less than a hundred each on separate line.

    This is an interesting brain-teaser so posting it here so others may choose to think about it while their code’s compiling.

    Of course, a ‘C program’ needs not to be taken too seriously — depending on not too far fetched undefined behaviours of given implementation is all right (but please do not use system or exec family of calls; not that I can see how that would help).

    By the way, if you’re interested in how this challenge looks solved in Rust, I’ve described that as well.

    0 is ambiguous

    Posted by Michał ‘mina86’ Nazarewicz on 24th of October 2010

    It has been a long time since my last entry, so inspired by Adriaan de Groot’s entry, I decided to write something about 0, NULL and upcoming nullptr.

    I will try to be informative and explain what the whole buzz is about and then give my opinion about nullptr. Let us first inspect how a null pointer can be donated in C and C++.

    Synchronizacja w jądrze Linux

    Posted by Michał ‘mina86’ Nazarewicz on 3rd of April 2010

    Prezentacja z seminarium dyplomowego na temat mechanizmów synchronizacji w jądrze Linux. Pokrótce przedstawia bariery pamięci, zmienne atomowe, semafory, spinlocki itp.

    Dni Wolnego Oprogramowania

    Posted by Michał ‘mina86’ Nazarewicz on 25th of February 2010

    UPDATE: Dni ciągle jeszcze trwają, acz moja prelekcja już się odbyła, w związku z czym umieszczam nowszą, a tym samym poprawioną wersję prezentacji wraz z pełnymi kodami źródłowymi.

    Niektóry wiedzą, inni właśnie się dowiedzą, iż w przyszły piątek (5 marca) ruszają III Dni Wolnego Oprogramowania w Bielsku-Białej. Będę miał przyjemność nie tylko bycia na tej imprezie, ale również aktywnego uczestnictwa. Dla wszelkich osób zainteresowanych udostępniam wersję alfa prezentacji, którą będę przedstawiał. Po zakończeniu wystawię wersję finalną wraz ze wszystkimi kodami źródłowymi.

    NTP over HTTP

    Posted by Michał ‘mina86’ Nazarewicz on 16th of January 2010

    Sitting in a dark office, after swearing for hours at ATI video cards I noticed time on my computer was incorrect. ‘No problem,’ I thought as I started typing ntpdate. That’s where it struck me that our beloved IT department had blocked most of the Internet. Checking the time on a watch or a mobile phone was not an option — I had neither — nor was looking at GKrellM on another PC — that’s lame.

    ‘I wish there was a NTP-over-HTTP protocol’ I sighed. And then I realised there was…

    tabbed, urxvt extension

    Posted by Michał ‘mina86’ Nazarewicz on 16th of May 2009

    Some time ago, I’ve modified the tabbed rxvt-unicode extension adding a few useful features. Somehow upstream did not want it while people started modifying the extension even more and so tabbedex fork came to be. The code can be browsed on GitHub or cloned from git://github.com/mina86/urxvt-tabbedex.git

    tabbedex carries a lot of changes and it would be futile to try and list them all here especially as the list would likely become out of date soon. Because of that, I recommend taking a look at the source code, which has, at the very beginning, a comment with a list of changes made.

    Treasure Hunt w Prologu

    Posted by Michał ‘mina86’ Nazarewicz on 17th of January 2009

    W poprzednim wpisie pisałem o Google Treasure Hunt, a w szczególności o zadaniu czwartym. Teraz, w ramach przygotowań do kolokwióm z języka Prolog postanowiłem napisać w nim rozwiązanie postawionego problemu: