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.
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.
相关文章推荐
- 在Windows环境下使用MinGW编译Qt 4.8.6
- 堆和栈的区别
- TypeError: window.clipboardData is undefined
- poj3660
- 搜索命令之locate,whereis,which
- SQL循环语句
- ireport各个版本的下载地址
- 解决sqlite删除数据后,文件大小不变问题(VACUUM)
- Qt 工程 pro文件
- linux-scp命令
- face++ c++接口 以及 curl 库的使用,json库在另一个博客介绍
- 2015’12杭电校赛1002 Polygon (计算几何)
- 兼容ie8 rgba()用法
- 【Objective-C学习记录】第三十六天
- Swift入门———逻辑分支
- MySQL和SQL Server比较
- 最新php环境搭建
- 21. Merge Two Sorted Lists
- A. Pasha and Stick
- 排序