среда, 5 декабря 2007 г.

DirectShow & D3DERR_OUTOFVIDEOMEMORY solution

I've wrote recently about the error D3DERR_OUTOFVIDEOMEMORY which you can reproduce by creating/destroying VMR9 instance in the Windowless mode X times (X depends of yours video card memory amount).

With the help of the DirectX9 debug binaries I have found that the memory leak takes place in case of switching VMR9 in the Windowsless mode. I've found that the DirectX9 surface which is created on that step will never be destroyed.

Seem to be the solution is to use DirectX9 Nov2007. No more leaks and works fine.

пятница, 2 ноября 2007 г.

Cut, copy & union AVI files without recompression

Написал тут на днях программку для копирования нескольких AVI файлов в отдельный файл без пережатия. Т.е. можно объединить список файлов, отрезать края, а потом получить единый файл. Если интересно и не лень будет, то расскажу как это делается и сорсы выложу...

четверг, 25 октября 2007 г.

Beeline + ICQ решение проблем с подключением

Долго боролся с проблемой как подключиться к ICQ с сотового телефона. Jimm отказывался коннектиться последнее время. Уже, наверное, известно, что проблема состоит в ограниченном количестве IP у Билайна.

И наконец, ура, наткнулся на программку Talkonaout для сотового телефона. С её помощью можно легко общаться в любой IM сети и коннектится всегда!

понедельник, 22 октября 2007 г.

Dynamic array template

This is my article prepared for publishing on codeproject.com.

Abstract

Often it is useful to allocate continuous memory block for some data to hold picture or string in it. Sometimes such memory block has non-trivial structure. For instance, it can be some class with dynamic array of elements and the static header as for bitmap that we want to send over network. And the problem is we want to chose array size dynamically. And we want to do it in the simplest and fastest way. This article is written to solve that problem.


Introduction

To send some data block via network or shared memory it is required to be continuous. It can't contain pointers or classes based on dynamic memory allocation – std::vector, for instance. (To be honest it is possible to send non-continuous data, but it could require to write a lot of code on both client and server side. And that code could be hard to support). So what to do in this case?

The simples way to solve the problem is to use C++ zero-sized arrays. Let's look at the following sample:

struct DynamicSizeStruct
{
    int a, b;
    float g;
    long SomeData[0]; // Microsoft compiler reports warning here, but it is safe to use
}

Ok, we have structure declaration now. It's time to allocate some memory. The very first though is to use malloc or it can be function called operator new. Anyway, there is no difference at this point.

size_t data_count = 100;
DynamicSizeStruct* p = reinterpret_cast<DynamicSizeStruct*>(
    malloc( sizeof(DynamicSizeStruct) + sizeof(long)*data_count ) );

Quite a lot of code for simple memory allocation... isn't it? That's the first problem, but it is not the biggest. Let's look at the more complex sample.

class DataUnit
{
public:
    DataUnit();
    ~DataUnit();

    long Data;
};

class DynamicSizeStruct
{
public:
    DynamicSizeStruct();
    virtual ~DynamicSizeStruct();

    int a, b;
    float g;
    DataUnit SomeData[0];
};

Well, we can use malloc as earlier but constructor and destructor will not be invoked. We'll could even use global function operator new for DynamicSizeStruct, but what about DataUnit? That the second problem – how to invoke constructors/destructors for all of the array members?

Here we have two problems. Now the time for solving it.


Reduce code size

To solve the first problem we'll use C++ template:

template<typename BaseT, DataT> class DynamicArrayHolder : public BaseT
{
public:
    DynamicArrayHolder() {};
    virtual ~DynamicArrayHolder() {};

    void* operator new( std::size_t size, std::size_t count );
    void operator delete( void* p ) { ::operator delete ( p ); };
    const DataT& operator[]( std::size_t pos ) const { return N[pos]; }
    DataT& operator[]( std::size_t pos ) { return N[pos]; }

protected:
    DataT DAHData[0]; // dynamic array declaration
}

Template has two parameters, BaseT is the base class to where we want to add dynamic size array, DataT — type of data elements in array. I declared operator new here to allocate all necessary memory at once. Operator delete is quite simple, it's just release all allocated memory. Index operator allows to gain access to data array members.

As you can see constructor and destructor are trivial at this point. The operator new have to care about allocation memory required by DataT elements. Parameter size will be filled by C++ compiler at compile time (it will contain number of bytes required to hold all data members of class BaseT + 4 bytes for virtual table). And parameter count we will use to define number of elements at runtime.


Now we can compare the old way to allocate data:

void* operator new( std::size_t size, std::size_t count )
{
    assert( count > 0 );
    if ( count == 0 ) throw std::bad_alloc();

    void* p = ::operator new ( size + sizeof( DataT )*count ); // allocate all required memory at once
    reinterpret_cast<DynamicArrayHolder*>(p)->Ncount = count;
    return p;
}

with the new one:

struct DynamicSizeStruct
{
    int a, b;
    float g;

    long SomeData[0]; // Microsoft compiler reports warning here, but it is safe to use
};

void someFunc()
{
    size_t data_count = 100;
    DynamicSizeStruct* p = reinterpret_cast<DynamicSizeStruct*>(
        malloc( sizeof( DynamicSizeStruct ) + sizeof(long)*data_count ) );
}

Less code and more readable as we planned! In the final implementation that you can download I've added three more functions GetDataPtr, GetByteSize and GetArraySize. GetDataPtr returns pointer to actual data of the BaseT class and you can use that pointer in memcpy functions with result of GetByteSize function. And finally, GetArraySize returns count of elements in dynamic array. I have to notice that there is one known restriction – we cannot construct BaseStruct with non-empty parameters list constructor.



Time to invoke DataT's constructors

We've already seen global function ::operator new in our operator new. But there we used "most common" form of that function. There is another one. And we will use it to invoke constructors for array members. New constructor implementation will look like that:

DynamicArrayHolder()
{
    for ( size_t n = 0; n < Ncount; ++n ) new ( &N[n] ) DataT;
};

It's just construct DataT elements one by one on already allocated memory block. In destructor we have to call destructor for all DataT elements:

virtual ~DynamicArrayHolder()
{
    for ( size_t n = 0; n < Ncount; ++n ) N[n].~DataT();
};

It is available full version of DynamicArrayHolder template in file DynamicArrayHolder.hpp. It contains additionaly copy constructor and some primitive error processing in the code. Also I've written sample code that have been placed in main.cpp file.

Download test.zip - 1.4 KB

пятница, 19 октября 2007 г.

Экология Dell

Сегодня приценивался к ноутбукам на сайте Dell. Пособирал разные конфигурации и наткнулся на одну забавную опцию, которая описывалась так: "Want to help the environment? Through Dell, you can plant a tree to help offset the carbon dioxide emissions generated to power your new system. You can also recycle your PC or donate it to someone in need." Т.е. Dell предлагает посадить дерево, чтобы скомпенсировать выделения углекислого газа спровоцированное опосредованно потреблением электричества вашим ноутом. Что ж тут скажешь — похвально.
Нам бы так — купил авто, и, как опцию, можно заказать в салоне посадку десятка деревьев. Я бы заплатил пожалуй даже 1000 руб...

четверг, 18 октября 2007 г.

DirectShow & D3DERR_OUTOFVIDEOMEMORY

Интересные детали выяснились с DirectShow... Имеет место быть неприятная ошибка, если в существующем графе очень быстро менять source filter'ы. С какого-то момента начинает выдаваться ошибка D3DERR_OUTOFVIDEOMEMORY при попытки соединить фильтры. А именно, на этапе соединения source filter с VMR9 не может выделится достаточное количество памяти. Поскольку в source filter управление памятью скрыто в BaseClasses, то пока проблема остается неразрешенной...

четверг, 11 октября 2007 г.

Fastest memory copy function

Сегодня исследовал наилучший способ копирования памяти... Сначала начал писать сам тестовую программу, но потом обнаружил нужный код у одного китайца... Вот результаты:

performance test
copy size: 405504 [bytes] w/t 16 byte-alignment * 1000 cycles

memcpy: 5.1 [ms]
rep movsd: 10.6 [ms]
FPU 8bytes: 11.4 [ms]
MMX movntq pre 16bytes: 10.2 [ms]
MMX movntq 16bytes: 10.3 [ms]
MMX movntq 8bytes: 10.4 [ms]
SSE movntps pre 32bytes: 10.5 [ms]
SSE movntps 16bytes: 10.7 [ms]
SSE 16bytes: 9.4 [ms]
MMX 16bytes: 6.5 [ms]
MMX 8bytes: 11.6 [ms]
asm 8bytes: 10.2 [ms]
asm 4bytes: 11.4 [ms]
C++ 4bytes: 41.9 [ms]


Оказывается нет ничего лучше, чем memcpy в редакции msvc+ 2005...