Custom Search

Tuesday, January 29, 2008

Alignment

Sometimes when we are coding on architectures with limited memory footprints , we might end up writing a memory allocator ourselves (more often than not ). Having said that - one of the important design considerations for a memory allocator is getting an address that is probably aligned with the underlying architecture.

Here is the simple code fragment to do the alignment for a given size.
Note that - the align function returns a new address only when the input address is not aligned by itself.


#include <stdlib.h>
#include <stdio.h>

/* align address to be a multiple of n. */
void* align(void* memory, int alignment)
{
unsigned char* ptr;
unsigned int offset;

ptr =(unsigned char*)memory;
offset =(unsigned int)ptr % alignment;

return offset ? (ptr + alignment - offset ): ptr;
}



int main()
{
void * a;

a = malloc(sizeof(char) * 10);

printf("\nAddress: 0x%p ", a );
printf("\nAligned Address:0x%p ", align(a, sizeof(int) ));
return EXIT_SUCCESS;
}




Reblog this post [with Zemanta]

Thursday, January 24, 2008

C++ STL list with logarithmic lookup ??

If we talk about standard C++ SQL (C++03, at least) looking for a data structure to simulate a list with logarithmic lookup, the question will appear to be contradictory to begin with.

If we are looking for a data structure whose access pattern is different from that of the underlying storage mechanism - Boost MultiIndex is an option worth spending at.

Wednesday, January 23, 2008

C++ - atexit implementation

More than one occasion - when a program exits - we may want to do some cleanup operations and notify other processes ( this becomes extremely important in the case of child-parent processes).

C/C++ does have a function called atexit() that does that.

A sample code fragment to use the same is as follows.


#include <cstdlib>
#include <iostream>

using namespace std;

void do1()
{
std::cout << "do1 implementation\n";
}

void do2()
{
std::cout << "do2 implementation\n";
}


int main()
{
atexit(do1);
atexit(do2);

exit(EXIT_SUCCESS);
}

Sample output:


$ g++ -Wall -Werror testexit.cpp
[noname@localhost test]$ ./a.out
do2 implementation
do1 implementation

Monday, January 21, 2008

Tutorial on Boost Threads

If you are looking for a tutorial on Boost Threads, start here .

Wednesday, January 9, 2008

Books on Design Patterns for concurrent / networked objects

One of the interesting books on concurrent / networked objects is -
Pattern-Oriented Software Architecture Volume 2: Patterns for Concurrent and Networked Objects

Boost ASIO library for high performance networking servers

If anybody is venturing into developing networking libraries in C++ , the ACE framework is the one that strikes your mind first.

Recently came across the library called boost asio - available here .

Boost ASIO definitely makes things much easy and portable. If the application we are developing needs to be truly portable - the fact that asio is part of the boost framework guarantees portability by definition ( across a large number of compilers across many platforms).

There is also an interesting article on the design patterns to be used by the boost library available here.

That makes interesting reading for sure.