Delphi Atom World (2)
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. 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.