ca65-14.html
来自「cc65 的编译器文档」· HTML 代码 · 共 128 行
HTML
128 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD> <META NAME="GENERATOR" CONTENT="LinuxDoc-Tools 0.9.20"> <TITLE>ca65 Users Guide: Module constructors/destructors</TITLE> <LINK HREF="ca65-15.html" REL=next> <LINK HREF="ca65-13.html" REL=previous> <LINK HREF="ca65.html#toc14" REL=contents></HEAD><BODY><A HREF="ca65-15.html">Next</A><A HREF="ca65-13.html">Previous</A><A HREF="ca65.html#toc14">Contents</A><HR><H2><A NAME="condes"></A> <A NAME="s14">14.</A> <A HREF="ca65.html#toc14">Module constructors/destructors</A></H2><P><EM>Note:</EM> This section applies mostly to C programs, so the explanationbelow uses examples from the C libraries. However, the feature may also beuseful for assembler programs.</P><H2><A NAME="ss14.1">14.1</A> <A HREF="ca65.html#toc14.1">Module overview</A></H2><P>Using the <CODE><A HREF="ca65-10.html#.CONSTRUCTOR">.CONSTRUCTOR</A></CODE> and <CODE><A HREF="ca65-10.html#.DESTRUCTOR">.DESTRUCTOR</A></CODE> keywords it it possible to exportfunctions in a special way. The linker is able to generate tables with allfunctions of a specific type. Such a table will <EM>only</EM> include symbolsfrom object files that are linked into a specific executable. This may be usedto add initialization and cleanup code for library modules.</P><P>The C heap functions are an example where module initialization code is used.All heap functions (<CODE>malloc</CODE>, <CODE>free</CODE>, ...) work with a fewvariables that contain the start and the end of the heap, pointers to the freelist and so on. Since the end of the heap depends on the size and start of thestack, it must be initialized at runtime. However, initializing thesevariables for programs that do not use the heap are a waste of time andmemory.</P><P>So the central module defines a function that contains initialization code andexports this function using the <CODE>.CONSTRUCTOR</CODE> statement. If (and only if)this module is added to an executable by the linker, the initializationfunction will be placed into the table of constructors by the linker. The Cstartup code will call all constructors before <CODE>main</CODE> and all destructorsafter <CODE>main</CODE>, so without any further work, the heap initialization code iscalled once the module is linked in.</P><P>While it would be possible to add explicit calls to initialization functionsin the startup code, the new approach has several advantages:</P><P><OL><LI>If a module is not included, the initialization code is not linked in and notcalled. So you don't pay for things you don't need.</LI><LI>Adding another library that needs initialization does not mean that thestartup code has to be changed. Before we had module constructors anddestructors, the startup code for all systems had to be adjusted to call thenew initialization code.</LI><LI>The feature saves memory: Each additional initialization function needs justtwo bytes in the table (a pointer to the function).</LI></OL></P><H2><A NAME="ss14.2">14.2</A> <A HREF="ca65.html#toc14.2">Calling order</A></H2><P>Both, constructors and destructors are sorted in increasing priority order bythe linker when using one of the builtin linker configurations, so thefunctions with lower priorities come first and are followed by those withhigher priorities. The C library runtime subroutine that walks over theconstructor and destructor tables calls the functions starting from the top ofthe table - which means that functions with a high priority are called first.</P><P>So when using the C runtime, both constructors and destructors are called withhigh priority functions first, followed by low priority functions.</P><H2><A NAME="ss14.3">14.3</A> <A HREF="ca65.html#toc14.3">Pitfalls</A></H2><P>When creating and using module constructors and destructors, please take careof the following:</P><P><UL><LI>The linker will only generate function tables, it will not generate code tocall these functions. If you're using the feature in some other than theexisting C environments, you have to write code to call all functions in alinker generated table yourself. See the <CODE>condes</CODE> module in the Cruntime for an example on how to do this.</LI><LI>The linker will only add addresses of functions that are in modules linked tothe executable. This means that you have to be careful where to place thecondes functions. If initialization is needed for a group of functions, besure to place the initialization function into a module that is linked inregardless of which function is called by the user.</LI><LI>The linker will generate the tables only when requested to do so by the<CODE>FEATURE CONDES</CODE> statement in the linker config file. Each table has tobe requested separately.</LI><LI>Constructors and destructors may have priorities. These priorities determinethe order of the functions in the table. If your intialization or cleanup codedoes depend on other initialization or cleanup code, you have to choose thepriority for the functions accordingly.</LI><LI>Besides the <CODE><A HREF="ca65-10.html#.CONSTRUCTOR">.CONSTRUCTOR</A></CODE> and <CODE><A HREF="ca65-10.html#.DESTRUCTOR">.DESTRUCTOR</A></CODE> statements, there is also a moregeneric command: <CODE><A HREF="ca65-10.html#.CONDES">.CONDES</A></CODE>. This allows tospecify an additional type. Predefined types are 0 (constructor) and 1(destructor). The linker generates a separate table for each type on request.</LI></UL></P><HR><A HREF="ca65-15.html">Next</A><A HREF="ca65-13.html">Previous</A><A HREF="ca65.html#toc14">Contents</A></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?