您的位置:首页 > 其它

pragma code_seg

2016-01-06 21:47 281 查看
I have a couple of question about this subject:-

1) From MSDN, it says this is used to force routines into certain code segments. But I am not clear as to what this
means? How did our fucntion routines get into the memory usually if without using this #pragma declaration?

2) How much memory is allocated for one segment?

I don't understand how this can help to lower the memory usage of a program/routine? If so, why can't we just have
every rountine in our program under code_seg so that the program we create use minimal memory?

3) What does it mean when we say "code that can be paged out of kernel memory"? what is the benefits of paging
out from memory? under what condition we do this?

Answer

Paging
is the way how Windows memory manager works.
I strongly encourage

you to read this article first:

http://msdn.microsoft.com/library/en...msdn_ntvmm.asp

In the Portable Executable format, code and data are packaged into several

sections. By default,
all program code goes into a single section named

[/u]".text". #pragma code_seg directive allows to create change this default

and
create additional code sections.
Thus, answering your first question,

if #pragma code_seg is not used, the code is allocated in the .text section.

Each section in memory is rouned up to the page size, which is 4K on Intel

x86 processors. That means if you have a section which size is 2 bytes, it

still takes one 4-Kbyte page of memory.

Using #pragma code_seg you can group functions in such a way that fuctions

that are used together are placed together into one section. Let's assume

you have several functions in the program and these functions take two

pages together. Let's also assume that in the most common scenario only

a half of all functions is used. Without any optimization, the functions

which are called in this scenario will be scattered between both pages,

so you will have both pages in the memory.
If you put these most commonly

used functions into a dedicated section, chances are that they will fit

into a single page.
That means that only one page must be resident in the

memory, while the rest of the code can be paged out.

In a large program it is nearly impossible to do such an optimization

manually, because it is extremely difficult to observe all possible

execution paths. The good news are that the Platform SDK contains an

utility named
Working Set Tuner (WST),
that does this optimization. Please,

refer to MSDN documentation for more details.

In the kernel mode, any code that executes at the DISPATCH IRQL level

or higher cannot be paged out.
Any code that executes at the PASSIVE level

can be paged.
Paging is advantageous because it helps to conserve physical

memory. As a rule of thumb, if you have in a driver more than 4K pageable

code, you should put it into a pageable section, otherwise you worth nothing.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: