03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

You can control the stack size in a .exe. This can apply to console programs, servers, or<br />

programs with no GUI – but not to UIQ programs, since they are launched with apprun.exe.<br />

You can also control the stack size when you launch a thread explicitly from within your<br />

program. If you have an application with an algorithm that requires a large stack such as a<br />

heavily recursive game-tree search, you may have to encapsulate the algorithm in a .exe of<br />

its own, or a separate thread.<br />

Since each user heap eats into a scarce system resource – the free page list – and since<br />

applications and servers run for months or years without being restarted, it's vital that<br />

programs detect heap failure due to a lack of memory. It's also vital that programs release<br />

unneeded memory as soon as possible. This is the domain of the Symbian OS cleanup<br />

framework, which is covered in <strong>Chapter</strong> 6.<br />

Threads have independent default heaps in the sense that each thread always allocates<br />

from its own heap. But since all heaps are in the same process' address space, each thread<br />

in a process can access objects on other heaps in that process – provided suitable<br />

synchronization methods are used.<br />

In addition to the default heap, threads can have other heaps. But these introduce new<br />

complications, so you should use them only if you have to. For any nondefault heap, you<br />

must provide a specific C++ operatornew() to allocate objects onto it. For local shared<br />

heaps–shared with other threads in the same process – you have to introduce<br />

synchronization using mutexes or the like. For global shared heaps – shared with threads in<br />

other processes – the heap is mapped to a different address in each process, so you have to<br />

introduce a smart reference system rather than straightforward pointers. All these things are<br />

possible if necessary – but they're rarely necessary. Usually, it's better to use a server to<br />

manage shared resources, rather than a shared heap. There's an overview of servers below,<br />

and they are covered more thoroughly, including performance optimization, in <strong>Chapter</strong>s 18,<br />

19 and 20.<br />

A thread's nonshared heaps are allocated into a 256 MB region of a process' virtual address<br />

space. By limiting the maximum size to 2 MB, there is an implied maximum of 128 threads<br />

per process.<br />

2.9.2 No Writable Static Data in DLLs<br />

DLLs only support read-only data, and program code. Writable static data is supported only<br />

by .exe is. This imposes some design disciplines on native Symbian OS code. It also<br />

makes life more difficult when porting code, which often assumes the availability of writable<br />

static. A feasible workaround is to use a .exe to contain the ported code. The .exe is<br />

packaged as a Symbian OS server, which allows it to be shared between multiple programs.<br />

By using a separate process, we also gain the benefit of isolation.<br />

Porting code would be much simpler if writable static data were supported, but then each<br />

DLL that uses this feature would contain fixed references to the address of its data. Every<br />

process using the DLL would therefore have to place the data at this specified address. In an<br />

open programming environment such as Symbian OS, you would need some form of<br />

system-wide DLL memory allocator to ensure that there are no clashes between the address<br />

requirements of different DLLs, and that an appropriate address will be available in every<br />

process that might use any given DLL. Symbian considered implementing such a system,<br />

but decided that it would impose unacceptable penalties on code size, execution times and<br />

RAM usage.<br />

Conventional operating systems circumvent the issue by detecting when the required<br />

address is not free in any particular process, making a copy of the DLL and modifying the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!