Delphi's atomic world

zhaozj2021-02-11  189

Reposted from: http://www.9cbs.net/expert/topic/108/108956.shtm;

http://www.9cbs.net/expert/topic/109/109010.shtm

Delphi Atom World Keywords: Delphi Control Miscellaneous During use Delphi development software, we are like a group of happy cattle and sheep, carefree, enjoy the sun and various VCL controls from the Object Pascal language. A rich waterfruit. Looking up, you are looking at the sky, you can taste the dense grass on the earth, who will want to think how big is the universe, what is more than the molecule and the atom? That is a philosopher. At this time, the philosopher is sitting on the top of the high mountain, looking up at the universe nebula transform, staring at the hilarious crawling, looked back, nodding the cattle and sheep grazing. Smile. If you pull up a grass, you gently contain it in your mouth, close your eyes, try it, don't know what this green grass is in the mouth of philosophers? Just, his face has been with satisfactory smile. Understand and understand Delphi's micro-world world, which allows us to completely understand the macro application structure of Delphi, and develop our software in a broader ideological space. This is like, Newton discovers the movement of the macro object, but because of why it is unclear why this is moving like this, Einstein experiences the happy life of relativism between basic particle regular and macro object! What is the first section of TOBJECT atom TOBJECT? It is the basic core of the Object Pascal language architecture and the origin of various VCL controls. We can think that TOBJECT is one of the atoms constituting the Delphi application, of course, they are constituted by a thinner particles such as basic Pascal syntax elements. Saying Tobject is the atom of the Delphi program because Tobject is supported by the Delphi compiler. All objects are derived from TOBJECT, even if you do not specify TOBJECT for ancestors. TOBJECT is defined at the System unit, which is part of the system. At the beginning of the System.Pas unit, there is such an comment text: {PREDEFINED Constants, Types, Procedures,} {and functions (Such as true, integer, or} {WriteLn) do not have it actual declarations.} {Instead they isy INTO The Compiler} {AND area Treated as if They were declared} {at the beginning, this unit contains predefined constants, types, procedures, and functions (such as: ture, integer or Writeln), they don't actually declare, but the compiler is built into, and the beginning of the compilation is considered to be defined. You can join other source program files such as Classes.Pas or Windows.PAS to compile and debug their source code, but you must not add the System.PAS source file to your project file to compile! Delphi will report the report repeatedly define the System's compilation error! Therefore, TOBJECT is the definition provided inside the compiler. For those we use the Delphi development program, Tobject is atomic things.

TObject defined in the System unit is such that: TObject = class constructor Create; procedure Free; class function InitInstance (Instance: Pointer): TObject; procedure CleanupInstance; function ClassType: TClass; class function ClassName: ShortString; class function ClassNameIs (const name: string): Boolean; class function ClassParent: TClass; class function ClassInfo: Pointer; class function instanceSize: Longint; class function InheritsFrom (AClass: TClass): Boolean; class function MethodAddress (const name: ShortString): Pointer; class function MethodName (Address: Pointer): ShortString; function FieldAddress (const Name: ShortString): Pointer; function GetInterface (const IID: TGUID; out Obj): Boolean; class function GetInterfaceEntry (const IID: TGUID): PInterfaceEntry; class function GetInterfaceTable: PinterFaceTable; Function SafeCallexception (ExceptObject: Tobject; Exceptaddr: Pointer): hResult; Virtual; ProcedureAfter Contrology: HRESULT; truction; virtual; procedure BeforeDestruction; virtual; procedure Dispatch (var Message); virtual; procedure DefaultHandler (var Message); virtual; class function NewInstance: TObject; virtual; procedure FreeInstance; virtual; destructor Destroy; virtual; end; Below, we Will gradually knock on the gate of the TOBJECT atom to see what is the structure. We know, TOBJECT is the basic class of all objects, then what is an object? Any object in Delphi is a pointer, which refers to a space occupied by the object in memory! Although the object is a pointer, we don't have to write like this code myObject ^ .GetName when we reference the object of the object, but can only be written as MyObject.getname, which is supported by the compiler. Friends using C Builder know the relationship between objects and pointers, because the objects of C Builder are defined as pointers.

The object pointer points to the object space for the object stores data, and we analyze the data structure of the memory space points to the object pointer. The head 4 bytes of the object space is the virtual method address table (VMT - Vritual Method Table). The next space is a space that stores the member data of the object itself, and is stored in the order of the data member of the object class from the data member of the object class, and the definition order of the data member in each class. The false method of the class (VMT) saves the process address of the original ancestor derived from the original ancestors of this class to all classes of the class. The virtual method of the class is to use the method of reserving the word Vritual declaration, the virtual method is the basic mechanism for achieving the object polymorphism. Although the dynamic method of reserving the word Dynamic declaration can also realize the polymorphism of the object, but such a method is not saved in the virtual method address table (VMT), it is only another kind of storage space provided by Object Pascal. Polymorphism implementation mechanism, but it is at the expense of call speed. Even if we do not define any class of false methods, the objects of such a class still exist pointers to the virtual method address table, just that the address item is zero. However, those false methods, such as DESTROY, FreeInstance, etc. in TOBJECT, and where is it stored? It turns out that their method address is stored in a space offset in the negative direction of the VMT pointer. In fact, the data space in the negative direction of the VMT table offsets 76 bytes is the system data structure of the object class, which is related to the compiler and may be changed in the future Delphi version. Therefore, you can think that VMT is a data structure starting from the negative offset address space, the negative offset data area is the system data area of ​​the VMT, and the positive offset data of the VMT is the user data area (custom imaginary address table. ). The functions and processes of class information or object runtime information defined in TOBJECT are generally related to system data of VMT. A VMT data represents a class, in fact, VMT is class! In Object Pascal, we use TOBJECT, TCOMPONENT, and the like, which are implemented as their respective VMT data internally in Delphi. The type of class defined by Class of reserves the word definition is actually pointing to the pointer to the associated VMT data. For our app, VMT data is static data. After the compiler compiles to complete our application, these data information has been identified and initialized. The program statement we wrote can access the VMT related information, obtain information such as the size of the object, class name, or run time, or call the name and address of the virtual method or reading method. When an object is generated, the system assigns a memory space for the object and linked the object with the related class, so that the head 4 bytes in the data space allocated for the object, becomes a point VMT data. Pointer. Let's take a look at how the object is born and destroyed. Looking at my three-year-old son is active on the grass, it is because of the birth process of my life, I can really realize the meaning and greatness of life. Only those who have experienced death will be more understanding and cherish life. So let us understand the process of the production and demise of the object! We all know that using the following statement can construct a simple object: anObject: = TOBJECT.CREATE; the compiler implements its compilation as: based on the VMT corresponding to TOBJECT, call the TOBJECT CREATE constructor.

In the CREATE constructor, the system's classcreate process is called, and the system's classcreate process is called the NewInstance virtual method in the class VMT. The purpose of calling the NewInstance method is to establish an instance space of the object, because we do not overload the method, so it is the newInstance of the Tobject class. The NewInstance method of the TOBJEC class will call the getMem process to assign memory for the object based on the object instance size (InstanceSize) initialized in the VMT table, and then call the INITINSTANCE method to initialize the assigned space. The InitInstance method first initializes the heads of the object space to point to the pointer to the object class corresponding to the VMT, and then clear the remaining space. After establishing an object instance, a virtual methodAfterconstruction is called. Finally, save the address pointer of the object instance data into the anobject variable, so that anobject object is born. Similarly, use the following statement to eliminate an object: anObject.destroy; TOBJECT's destructor DESTROY is declared as a false method, and it is also one of the intrinsic methods of the system. The Destory method first calls the BeforeDestruction virtual method, then call the system's ClassDestroy process. The ClassDestory process calls the FreeInstance virtual method by class VMT, and the FreeInstance method is called the FreeMem process to release the memory space of the object. In this way, an object disappears in the system. The sect of the object is simpler than the structure of the object, as if the birth of life is a long birth process, and death is relatively short, which seems to be an inevitable law. During the structure and destructuring of the object, NEWINSTANCE and FreeInstance are invoked to create and release the memory space of the object instance. The reason why these two functions declare as virtual functions, in order to allow users to prepare a special object class that requires users to manage memory (such as in some special industrial control programs), there is extended space. The Afterconstruction and BeForedStruction declared as virtual functions, but also to the future class after the object, have the opportunity to breathe the newly born object breathe the first fresh air, and the object can allow the object to complete the good event before the object is done, this is Reasonable things. In fact, the TFORM object and the TDATAMODULE object's oncreate events and ONDESTROY events are triggered by these two virtual functions overloaded in TForm and TDATAMODULE. In addition, TOBJEC also provides a Free method, it is not a virtual method, which is specially provided for those who can freely release the object if it is empty (NIL). In fact, I can't figure out if the object is empty, it is a problem that the program logic is unclear. However, anyone is not perfect, it may make mistakes, using free to avoid accidental mistakes. However, writing the correct program cannot relisten to such a solution, or should be the first goal of the program's logical correctness! Interested friends can read the original code of the SYSTEM unit, where a large number of code is written in assembly language. Careful friends can find that the Tobject's constructor crete and destructor Destory did not write any code. In fact, in the debug state through the debug's CPU window, it clearly reflects the assembly code of Create and Destory.

Because, Master Gate creating Delphi does not want to provide excessive complex things to users, they want users to write applications in simple concepts, hide complex work to the internal inside of the system by them. So, take the code of these two functions when publishing System.PAS units, allowing users to think that TOBJECT is the source of everything, the user derived class is completely from virtual, which is not wrong. Although these most essential code reading Delphi requires a small amount of assembly language knowledge, but read this code, let us know the basic laws of the origins and development of the Delphi world. Even if you don't understand, you can at least understand some basic things, and write a Delphi program for us. The second section of the TCLASS atom is defined in the System.Pas unit: Tclass = Class of Tobject; It means that tclass is the class of TOBJECT. Because Tobject itself is a class, TCLASS is the class of the so-called class. Conceptually, TCLASS is the type of class, namely, class. However, we know a class of Delphi representing a VMT data. Therefore, the class can be considered to be the type defined for the VMT data item, in fact, it is a pointer type pointing to VMT data! In previous conventional C languages, it is not possible to define types of classes. Once the object is compiled, the structure information of the class has been converted to an absolute machine code, and there will be no complete class information in the memory. Some higher-level object-oriented languages ​​support dynamic access and calls for class information, but often require a complex internal interpretation mechanism and more system resources. The Delphi's Object Pascal language absorbs some of the excellent features of the high-level object-oriented language, and retains the traditional advantages that can be directly compiled into machine code, which relatively solves the problem of advanced features and program efficiency. It is because Delphi retains complete class information in the application, in order to provide advanced object-oriented functions such as AS and IS or the runtime conversion and discriminating classes, and the VMT data of the class plays a key core role. Interested friends can read the ASCLASS and ISClass two assembly processes of the System cell, they are the implementation code of the AS and IS operators to deepen the understanding of the class and VMT data.

Delphi Atom World (2) Keywords: Delphi Control Miscellaneous TCLASS Atom In System.Pas Unit, TCLASS is defined: tclass = Class of Tobject; It means that tclass is the class of Tobject. Because Tobject itself is a class, TCLASS is the class of the so-called class. Conceptually, TCLASS is the type of class, namely, class. However, we know a class of Delphi representing a VMT data. Therefore, the class can be considered to be the type defined for the VMT data item, in fact, it is a pointer type pointing to VMT data! In previous conventional C languages, it is not possible to define types of classes. Once the object is compiled, the structure information of the class has been converted to an absolute machine code, and there will be no complete class information in the memory. Some higher-level object-oriented languages ​​support dynamic access and calls for class information, but often require a complex internal interpretation mechanism and more system resources. The Delphi's Object Pascal language absorbs some of the excellent features of the high-level object-oriented language, and retains the traditional advantages that can be directly compiled into machine code, which relatively solves the problem of advanced features and program efficiency. It is because Delphi retains complete class information in the application, in order to provide advanced object-oriented functions such as AS and IS or the runtime conversion and discriminating classes, and the VMT data of the class plays a key core role. Interested friends can read the ASCLASS and ISClass two assembly processes of the System cell, they are the implementation code of the AS and IS operators to deepen the understanding of the class and VMT data. With the type of `class, you can use the class as a variable. You can understand the variables of the class as a special object, you can access the class variables as access objects. For example: Let's look at the following program fragment: type TSampleClass = class of TSampleObject; TSampleObject = class (TObject) public constructor Create; destructor Destroy; override; class function GetSampleObjectCount: Integer; procedure GetObjectIndex: Integer; end; var aSampleClass: TSampleClass ACLASS: TCLASS; In this code, we define a class TsampleObject and its related class type TsampleClass, as well as two type variables asampleclass and aclass. In addition, we also define constructor, destructory functions, a class method GetSampleObjectCount, and an object method getObjectIndex. First, let's understand the meaning of class variables asampleclass and aclass. Obviously, you can use TSAMPLEOBJECT and TOBJECT as a constant value and assign them to the ACLASS variable, as if the 123 constant value is assigned to the integer variable I. Therefore, the relationship between class types, classes, and class variables is the relationship between type, constant, and variables, but is just about this level of the class rather than the object level. Of course, directly assigning TOBJECT to AsampleClass, because asampleclass is the class variable of Tobject derived class TsampleObject, and TOBJECT does not include all definitions compatible with TsampleClass type.

Instead, the TSAMPLEOBJECT assigns to the ACLASS variable is legal, because TsampleObject is the TOBJECT derived class, is compatible with the Tclass type. This is completely similar to the assignment and type matching of object variables. Then, let's take a look at what is a class method. The so-called method is to refer to the method called at the level of the class, as the getSampleObjectCount method as defined above, is a method of declaring the word Class Class. The class method is different from the object method that is called on the object level, and the object method has been familiar with us, and the class method is always used in the hierarchical use of all kinds of objects of all kinds of objects and centralized management objects. In the TOBJECT definition, we can find a large number of ways, such as ClassName, ClassInfo, and NewInstance, and more. Where newInstance is also defined as Virtual, ie empty class methods. This means that you can rewrite the NewInstance implementation method in derived subclasses to construct the object instance of this class in a special manner. You can also use the Self this identifier in the class method, but the meaning of its representative is different from the SELF in the object method. SELF in the class method indicates its own class, that is, pointing to the pointer of the VMT, and the SELF in the object method represents the object itself, that is, a pointer to the object data space. Although, the class method can only be used at the class level, but you can still call the class method through an object. For example, the class method className Class TOBJECT can be called by statement aobject.classname, because the heads of the object data space points to the object pointer are pointers to the class VMT. Instead, you can't call the object method at the class level, and the statement of TOBJECT.FREE must be illegal. It is worth noting that the constructor is a class method, and the destructor is an object method! what? The constructor is a class method, the destructor is an object method! Have you made a mistake? Look, when you create an object, use the statement similar to the following statement: AObject: = TOBJECT.CREATE; it is clearly a CREATE method called class TOBJECT. The following statement is used when deleting objects: AObject.destroy; even if the object is used to release the object, it is also a DESTROY method of the object. The reason is very simple, before constructing the object, the object does not exist, only class, creating objects can only be used. Instead, the delete object must be deleted an existing object, which is an object being released, not a class being released. Finally, the problem of the virtual construct function is discussed by the way. In the traditional C language, the false preframe function can be implemented, but the fictional creation function is a problem. Because, in a traditional C language, there is no type of type. The instance of the global object is in the global data space, and the local object of the function is also compiled, and the instance of the mapping in the stack space is compiled. Even the object created, but use the New operator to press the fixed class structure. The instance allocated in the heap space, and the constructor is just a method of initializing the generated object instance. Traditional C language has no real class method, even if it can define a so-called static-based method, it is ultimately implemented as a special global function, not to mention the virtual class method, the virtual method can only target the specific object instance effective. Therefore, conventional C language believes that before the specific object instance is generated, it is impossible to construct the object itself according to the upcoming object itself.

It is indeed impossible because it will logically produce contradictory paradox! However, it is precisely because there is a type information of a dynamic class in Delphi, there is a virtual virtual class method, as well as constructor is a key concept based on class implementation, and virtual constructor can be realized. The object is generated by the class, the object is like a baby in growth, and the class is its mother. The baby doesn't know what kind of person in the future, but the mothers have cultivated different from their respective education methods. People, the truth is connected. It is in the definition of the Tcomponent class, and the constructor CREATE is defined as virtual, in order to make different types of controls to achieve their respective construction methods. This is the greatness of the concepts of TCLASS creation, and it is also the greatness of Delphi. .................................... Chapter 3 Win32 time and space looks at my old father The little grandson who played toys, and then said to me: "The child is like yours, like to disassemble things, and see it." Think about me, I often take the toy car, a small alarm clock, music box, and so on, and I often have been reprimanded by my mother. My first understanding of the basic principle of the computer is related to the music box I have ever. That is a comic book at high school, a white beard old man explaining the theory of the smart machine, a uncle who stays eight characters is talking about computer and music boxes. They said that the central processor of the computer is the row of music reeds used to pronounce in the music box. The computer program is the bump of the small cylinder in the music box, and the rotation of the small cylinder is equivalent to the central processor. The natural movement of the instruction pointer, and the tap-controlled bump control music reed vibration pronunciation is equivalent to the instruction of the central processor execution program. The music box makes a wonderful melody, which is the music spectrum performance of the craftsman. The computer has complex processing, which is implemented according to the programmer pre-prepared program. After going to college, I knew that white beard old man is the scientific master Tuling. His limited self motivated theory promotes the development of the entire information revolution, and the uncle's husband's uncle is the father of the computer. Nosman, Feng The computer architecture is still the main system of the computer. The music box is not white, the mother can be wrapping. There is deep and simple understanding to create a deep and simple creation. This chapter we will discuss the basic concepts related to our programming in Windows 32-bit operating systems, establish the correct time and space in Win32. I hope that after reading this chapter, we can understand the procedures, processes, and threads, understand the principles of executive files, dynamic connection libraries, and running packages, see the truth of global data, local data, and parameters in memory. The first section understands the cause of history, Windows is from DOS. In the Dos era, we have been only the concept of programs, but there is no process. At that time, there were only the regular army of the operating system, such as UNIX and VMS, etc., there were only the concept of the process, and the multi-process means that small machines, terminals, and multi-users also means money. Most of my time can only use relatively low-cost microcomputers and DOS systems, just start contacting processes and small machines when learning the operating system. After Windows 3.x, Microsoft is only in the graphical interface operating system, and I am also the concept of formal facing multitasks and processes at this time. Previously under DOS, only one program can be executed at the same time, and multiple programs can be implemented at the same time at Windows, which is multitasking. While running a program in DOS, you cannot perform the same program, and under Windows, the same program can have more than two copies at the same time, and each running program copy is a process.

More specifically, any program is run to generate a task, and each task is a process. When the program and the process are placed together, you can think that the program is static, a typical program is a static code and data consisting of several DLL files by an Exe file or an EXE file. The process is an operation of the program, which is a dynamically running code and dynamically changed data in memory. When the static program requires running, the operating system will provide a certain memory space for this run, transfer the static program code and data into these memory spaces, and relocate the program's code and data, it is in this space. Internal execution program, there is a dynamic process. Two copies running simultaneously with the same program means that there are two process spaces in the system memory, but their program function is the same, but in different dynamic changes. From the time of the process run, the processes are executed simultaneously, and the professional terminology is called parallel execution or concurrent execution. However, this is mainly the operating system to give us the surface feeling. In fact, the processes are performed in time, that is, the procedures for performing the process of the process by turning the CPU. For a CPU, only one process in the same time is executed. The operating system is the behind-the-scene manipulator of the schedule process, which continuously saves and switches the current state executed in the CPU, so that every scheduling process is considered to be complete and continuous. Since the process is scheduled very fast, it is the feeling of the process to run at the same time. In fact, it is true that there is only a hardware environment in multi-CPU. Sowing of the thread section later, we will find that the truly process is running, and the process is more important to provide the process space. From the space occupied by the process, each process space is relatively independent, and each process runs in its own stand-alone space. A program includes both code spaces and data space, code, and data to occupy process space. Windows assigns actual memory for the data space required for each process, and generally uses sharing means for code space, map a program of one code to multiple processes of the program. This means that if a program has 100K code and requires 100K data space, it is a total of 200K process space, and the first time the operation system will allocate 200K process space, and the second operation of the running program When the process, the operating system only assigns 100K data space, and the code space shares the space of the previous process. What is mentioned above is the basic time and space of the process in the Windows operating system. In fact, the 16-bit and 32-bit operating systems of Windows have great differences in the time and space of the process. From time, the 16-bit Windows operating system, such as Windows 3.x, etc., process management is very simple, it is actually just a multi-tasking management operating system. Moreover, the operating system is passive to the scheduling of the task. If a task does not give up the processing of the message, the operating system must wait. Since the 16-bit Windows system is defective in management process, the CPU resources are fully occupied by a process. In that era, for 16 Windows can have the opportunity to schedule other tasks, Microsoft strongly praises developers who develop Windows applications are broad programmers, which makes them willing to write a few rows of grace gifts. Instead, Win32 operating system, such as Windows 95 and NT, etc., is the ability to have real multi-process and multitasking operating systems. The process in Win32 is completely scheduled by operating system. Once the process runs, the operating system will actively switch to the next process regardless of whether the process is still processing data.

Strictly speaking, the 16-bit Windows operating system is not a complete operating system, while the 32-bit Win32 operating system is a true operating system. Of course, Microsoft will not say that Win32 makes up for 16 Windows shortcomings, but claims that Win32 has achieved a advanced technology called "preemptive multitasking", which is a commercial means. From the space, the process space in the 16-bit Windows operating system is relatively independent, but the process can be easily accessible to each other's data space. Because these processes are actually different data segments in the same physical space, and improper address operations can easily cause errors to read and write, and make the operating system crash. However, in the Win32 operating system, each process space is completely independent. Win32 provides a 4G virtual for each process and is a continuous address space. The so-called continuous address space means that each process has the address space from $ 000000 to $ fffffff, instead of the segments of 16-bit Windows. In WIN32, you don't have to worry about your read and write operations unintentionally affect the data in other process space, nor do you have to worry about nothing to harass your work. At the same time, Win32 provides a continuous 4G virtual space provided by your process. It is the operating system to map the physical memory to you with the support of hardware. Although you have such a broad virtual space, the system will never waste a byte. Physical memory. The second process space rarely cares about the internal world of the process when we write Win32 applications with Delphi. Because Win32 provides 4G continuous virtual process space for our process, the largest application in the world may only use part of the space. It seems that the process space is infinite, but the 4G process space is virtual, and the actual memory of your machine may be far from this. Although the process has such a vast space, some complex algorithms are still unable to operate because stack overflows, especially programs that contain a large number of recursive algorithms. Therefore, in depth, understand and understand the structure of these 4G process space, and its relationship with physical memory, etc., will help us understand the Win32's time and space, which can be used in actual development work. The worldview and methodology solve a variety of problems. Below, we will understand the internal world of Win32's process space through simple experiments. This may require some knowledge of CUP registers and assembly languages, but I try to illustrate it with a simple language. When starting Delphi, a project1 will be automatically generated, and we will take it with it. Set a breakpoint in any location of the Project1.dpr's original program, for example, set a breakpoint at BeGin. Then run the program, which is automatically stopped when the program is running to the breakpoint. At this time, we can open the CPU window in the debug tool to observe the internal structure of the process space. The current instruction pointer register EIP is stopped in $ 0043E4B8, the maximum number of two 16-based number of addresses where the address where the program instruction is located, can be seen that the current program is in the address location of the 4G process space, which occupies $ 00000000 to $ Fffffff is a quite little address space. In the instruction box in the CPU window, you can view the contents of the process space. When you look at the space content less than $ 00400000, you will find a string of question marks "????", because the address space is not mapped to the actual physical space. If you are at this time, you will find that it is also $ 00400000. Although Hinstance reflects the handle of the process instance, it is the start address value that the program is loaded into the memory, as is the same in the 16-bit Windows.

转载请注明原文地址:https://www.9cbs.com/read-5131.html

New Post(0)