Abstract:
A searching method for determining a function associated with a memory block of a memory of a computer system and the computer system thereof are disclosed. A first function execution code executed by the computer system calls a second function execution code to require the computer system to allocate a first memory block to the first function execution code. A symbol mapping table (i.e., a linker map) stores a symbol address corresponding to the first function execution code. The searching method includes storing a return address of the second function execution code into a predetermined memory block of the memory, reading the return address from the predetermined memory block, and determining that the first memory block has been allocated to the first function execution code according to the return address and the symbol address stored in the symbol mapping table.

Description:
BACKGROUND OF THE INVENTION  
       [0001]     1. Field of the Invention  
         [0002]     The present invention relates to a searching method for determining a function associated with a memory block and a computer system thereof, and more specifically, to a searching method and a computer system for determining a function associated with a memory block according to a return address stored in a header of the memory block.  
         [0003]     2. Description of the Prior Art  
         [0004]     In a computer system, it is fundamental to provide a system service for dynamically allocating a memory block to a function. Each function can require the computer system to dynamically allocate a memory block, and later that same function returns the allocated memory block back to the computer system after it has been determined that the function no longer requires the allocated memory. In this way, the goal of sharing the memory resource can be achieved. However, if it is determined that the allocated memory block will no longer be utilized by the function but is not returned to the computer system, the allocated memory block that will not be utilized again by the function can not be re-allocated by the computer system for utilization by a different function. This results in a situation in which the available memory resources tend to decrease over time. This phenomenon is generally referred to as a memory leak. A memory leak results in the lack of memory resources that can be utilized by the computer system. If the memory leak is not serious, perhaps a slow memory leak, it causes the performance of the computer system to be reduced. If the situation is serious, perhaps a very fast memory leak, it may cause the computer system to crash. A memory leak is a considerably serious problem. When the computer system detects that there is insufficient available memory space, it is necessary and important for the computer system to further check to determine if the insufficiently availability of memory space resulted from a memory leak. Furthermore, it is critical to realize what has caused the memory leak. An embodiment is provided in the following paragraph for describing how to resolve the aforementioned question by relying on a prior art.  
         [0005]     Please refer to  FIG. 1 .  FIG. 1  is a functional block diagram of a computer system  10  according to a prior art. The computer system  10  comprises a microprocessor  12 , a flash memory  14 , a random access memory (RAM)  16 , and a buffer memory  18 . The operation of the computer system  10  comprises the microprocessor  12  accessing data stored in the flash memory  14 , the RAM  16  or the buffer memory  18 , and the process of executing necessary operations. The flash memory  14  is a non-volatile memory storing a source code FS 1  of a first function F 1 , a source code FS 2  of a second function F 2 , and two pre-process directives, “ —— FILE —— ” and “ —— LINE —— ” corresponding to the first function F 1 . Functions of the two pre-process directives will be described in the following paragraph. The RAM  16  is a volatile memory comprising a plurality of memory blocks  16   a,    16   b,  and  16   c.  The memory block  16   a  contains a header  16   ah,  the memory block  16   b  contains a header  16   bh,  and the memory block  16   c  contains a header  16   ch.  Additionally, in the computer system  10 , the buffer memory  18  is utilized for storing an execution code that has been generated by the microprocessor  12  for compiling a function.  
         [0006]     Please refer to  FIG. 1  and  FIG. 2 .  FIG. 2  is a diagram of the first function F 1  shown in  FIG. 1  calling (i.e., invoking) the second function F 2  according to the prior art. The microprocessor  12  compiles a program containing the first function F 1  and the second function F 2 . During the compilation of the program, the microprocessor  12  obtains the instructions that the content of the line number L 1  of the first function F 1  is to call the second function F 2 . At this time, according to the prior art, the micro-processor  12  records the function name (i.e., F 1 ) of the first function F 1  in the pre-process directive  —— FILE —— , and records the line number L 1  in the pre-process directive  —— LINE —— . After the program has been compiled, the micro-processor  12  generates a first function execution code FE 1  corresponding to the first function F 1 , and a second function execution code FE 2  corresponding to the second function F 2 . Please note that the first function execution code FE 1  and the second function execution code FE 2  are both stored in the buffer memory  18 .  
         [0007]     In the present embodiment, the first function F 1  calls the second function F 2  to instruct the computer system  10  to allocate a certain memory block to the first function F 1 . After the program has been compiled, it enters a program run time stage. When the computer system  10  executes the first function execution code FE 1 , specifically, FE 1 &#39;s certain part that is corresponding to the line number L 1 , the program counter branches to the address of the second function execution code FE 2 . The computer system  10  then starts to execute the second function execution code FE 2  from the beginning of the second function F 2 . Assume now the second function F 2  requires the computer system  10  to allocate the memory block  16   b  to the first function F 1 . At this time, the computer system  10  copies the memory allocation information stored in the header of the memory block  16   b,  wherein the memory allocation information contains the function name (i.e., F 1 ) of the first function F 1  and the line number L 1 , respectively stored in the pre-process directives  —— FILE ——  and  —— LINE —— . As is well known in the art, the data type of the data stored in the pre-process directive  —— FILE ——  is the character data type. Therefore, as the number of characters in the function name increases, the space occupied by the pre-process directive  —— FILE ——  increases. The data type of the line number stored in the pre-process directive  —— LINE ——  is the integer data type. This integer data type usually occupies 4 bytes. After the computer system  10  finishes executing the second function execution code FE 2 , the execution point (i.e., the program counter) branches back to the first function F 1 . The computer system  10  then executes the line L 2  (the line next to and following the line L 1 ) of the first function F 1 , which means that the computer system  10  starts to execute the first function execution code FE 1 , specifically, FE 1 &#39;s certain part that is corresponding to the line number L 2 .  
         [0008]     When an engineer or a programmer discover there may be a memory leak, the engineer or programmer can check the header of the memory block  16   b  to obtain the allocation related information of the memory block  16   b.  Note that the memory block  16   b  is allocated to the first function F 1  by the computer system  10 . That means, according to the prior art, when a memory leak occurs and the engineer needs to find an allocated memory block that should be returned to the computer system  10  but in fact it has not been returned, the engineer can obtain allocation related information of all memory blocks to find which the allocated memory block is and which function the allocated memory block is allocated to by the computer system  10 . In this way, the reason of the memory leak problem may be found. However, during the program compilation time, the prior art method needs to occupy some space of the non-volatile memory to store the memory allocation information—for the data of the pre-process directives  —— FILE ——  and the  —— LINE —— . Also, during the program execution time, the memory allocation information is copied to the random access memory (RAM). Hence, the expended time and the memory space cost are increased and the overhead of the computer system is raised accordingly.  
       SUMMARY OF THE INVENTION  
       [0009]     One of the objectives of the claimed invention is therefore to provide a searching method for determining a function associated with a memory block according to a return address stored in a header of the memory block, in order to resolve the above-mentioned problem.  
         [0010]     According to the claimed invention, a searching method is disclosed. The searching method is utilized for determining a function associated with a memory block of a memory of a computer system. The memory comprises a plurality of memory blocks and stores a first function execution code, a second function execution code, and a symbol mapping table (i.e., a linker map). The first function execution code executed by the computer system calls the second function execution code to require the computer system to allocate a first memory block to the first function execution code. The symbol mapping table stores a symbol address corresponding to the first function execution code. The searching method comprises: storing a return address of the second function execution code into a predetermined memory block of the memory, reading the return address from the predetermined memory block, and determining that the first memory block has been allocated to the first function execution code according to the return address and the symbol address stored in the symbol mapping table.  
         [0011]     In addition, the claimed invention provides a computer system. The computer system comprises: a memory comprising a plurality of memory blocks for storing a first function execution code, a second function execution code and a symbol mapping table (i.e., a linker map), wherein the symbol mapping table stores a symbol address corresponding to the first function execution code; and a computation unit, coupled to the memory, for executing the first function execution code and the second function execution code, wherein the first function execution code calls the second function execution code to require the computer system to allocate a first memory block to the first function execution code and stores a return address of the second function execution code into a predetermined memory block of the memory, and the computation unit reads the return address from the predetermined memory block and determines that the first memory block has been allocated to the first function execution code according to the return address and the symbol address stored in the symbol mapping table.  
         [0012]     One advantage according to the claimed invention is that there is definitely no need to occupy any additional non-volatile memory space. In contrast to the prior art memory allocation information, the claimed invention memory allocation information stored in a header of an allocated memory block is only a return address, and it only requires 4 bytes of space in the header to store the return address. In this way, it is easy to precisely and quickly find a function associated with the allocated memory block. Hence, the claimed invention searching method provides the computer system with a reduced system overhead, and reduces the expended time and required memory space to further increase the execution performance and the execution speed of the computer system.  
         [0013]     These and other objectives of the present invention will no doubt become obvious to those of ordinary skill in the art after reading the following detailed description of the preferred embodiment that is illustrated in the various figures and drawings. 
     
    
     BRIEF DESCRIPTION OF THE DRAWINGS  
       [0014]      FIG. 1  is a functional block diagram of a computer system according to a prior art.  
         [0015]      FIG. 2  is a diagram of the first function shown in  FIG. 1  calling the second function according to the prior art.  
         [0016]      FIG. 3  is a functional block diagram of a computer system according to the present invention.  
         [0017]      FIG. 4  is a flowchart of a first stage of a searching method that determines a function associated with a memory block.  
         [0018]      FIG. 5  is a flowchart of a second stage of a searching method that determines a function associated with a memory block. 
     
    
     DETAILED DESCRIPTION  
       [0019]     Please refer to  FIG. 3 .  FIG. 3  is a functional block diagram of a computer system  20  according to the present invention. The computer system  20  comprises a microprocessor  22 , a flash memory  24 , a random access memory (RAM)  26 , and a buffer memory  28 . In the present embodiment, a source code FS 1  of a first function F 1  and a source code FS 2  of a second function F 2  are stored in the flash memory  24 . The RAM  26  contains a plurality of memory blocks  26   a,    26   b,  and  26   c.  The memory blocks  26   a,    26   b,    26   c  have corresponding headers  26   ah,    26   bh,    26   ch,  respectively. It should be noted that the components of the computer system  20  and the components with the same names of the computer system  10  shown in  FIG. 1  have similar functions, so the functions and operations of these components are not repeatedly described. In the present embodiment, in addition to the first function execution code FE 1  generated by the microprocessor  22  compiling the first function F 1  and the second function execution code FE 2  generated by the microprocessor  22  compiling the second function F 2 , the buffer memory  28  further stores a symbol mapping table ST. The symbol mapping table ST stores the function name (i.e., F 1 ) of the first function F 1 , and a symbol address F 1 A corresponding to the first function execution code FE 1 . The first function execution code FE 1  is stored in the buffer memory  28  at the address F 1 A. Similarly, the symbol mapping table ST also stores the function name (i.e., F 2 ) of the second function F 2 , and a symbol address F 2 A corresponding to the second function execution code FE 2 . The second function execution code FE 2  is stored in the buffer memory  28  at the address F 2 A. The purpose of the symbol mapping table ST in the present embodiment will be described in the following paragraph. Please note that the symbol mapping table ST is a necessary component required during the compilation process according to the present invention. Please note that the detailed process of how the symbol mapping table ST is built is not a limitation of the present invention.  
         [0020]     Please refer to  FIG. 3 ,  FIG. 4  and  FIG. 5 .  FIG. 4  is a flowchart of a first stage of a searching method that determines a function associated with the memory block  16   b.    FIG. 5  is a flowchart of a second stage of the searching method that determines a function associated with the memory block  16   b.  The searching method according to the present invention comprises two stages that are referred to as the first stage and the second stage.  
         [0021]     The first stage comprises the following steps:  
         [0022]     Step  200 : Start the first stage.  
         [0023]     Step  202 : Store the return address RA into the header  26   bh  of the memory block  26   b.    
         [0024]     Step  204 : End the first stage.  
         [0025]     When an engineer (e.g., a programmer) discovers that there may be a memory leak, he controls the computer system  20  to start executing the second stage.  
         [0026]     The second stage comprises the following steps:  
         [0027]     Step  206 : Start the second stage.  
         [0028]     Step  208 : Read the return address RA from the header  26   bh  of the memory block  26   b.    
         [0029]     Step  210 : Determine that the memory block  16   b  has been allocated to the first function F 1  during the execution time of the first function execution code FE 1  according to the return address RA and the symbol address F 1 A stored in the symbol mapping table ST.  
         [0030]     Step  212 : End the second stage.  
         [0031]     The detailed description of the above-mentioned first stage, as shown in  FIG. 4 , is included in the following paragraph. For example, some space of the buffer memory  28  is utilized for storing the first function execution code FE 1  corresponding to the first function F 1 . The execution code of the line number L 1  is stored in the buffer memory  28  at an address A 1 , and the execution code of the line number L 2  is stored in the buffer memory  28  at an address A 2 . The second function execution code FE 2  corresponding to the second function F 2  is stored in the buffer memory  28  at an address B 1 .  
         [0032]     In the present embodiment, when a program is being executed, if the computer system  20  starts to execute the first function execution code FE 1 , specifically, FE 1 &#39;s certain part that is corresponding to the line number L 1 . In other words, the computer system  20  starts to execute the execution code data stored in the buffer memory  28  at the address A 1 . The first function F 1  must call the second function F 2 . At this time the computer system  20  performs two steps at the same time. The two steps comprises: (1) the execution point branching to the address B 1 , and (2) viewing the address A 2  as the return address RA of the second function F 2  (the second function execution code FE 2 ) and recording the address A 2  in a return address register (i.e., a LR Register). The computer system  20  then stores the return address RA (that is address A 2  currently) stored in the return address register into the header  26   bh  of the memory block  26   b  (Step  202 ). Please note that in other embodiments of the present invention, the return address RA can be recorded in a stack, not in a return address register. In addition, the stack can be stored in the buffer memory  28  or any other memory devices that can be accessed by the microprocessor  22 . Next, the computer system  20  starts to execute the data stored in the buffer memory  28  at the address B 1 ; that is, the computer system  20  starts to execute the second function execution code FE 2 . After the execution of the second function execution code FE 2  is finished, the execution point will branch back to the address A 2  recorded in the return address register, and the computer system  20  then executes the line number L 2  of the first function F 1  (the line next to and following the line L 1 ), which means the computer system  20  executes the first function execution code FE 1 &#39;s certain part corresponding to the line number L 2 . Please note that the return address RA is dynamically obtained during the program execution time. In this way, the present invention does not need to occupy any space of the buffer memory  28  to store the additional data according to the prior art, comprising the pre-process directives  —— FILE ——  and  —— LINE ——  shown in  FIG. 1 . The method according to the present invention can reduce the load of the computer system  20 .  
         [0033]     When an engineer (i.e., a programmer) discovers that there may be a memory leak, the engineer needs to look up all headers of all the memory blocks to find an abnormal operated allocated memory block. The abnormal operated allocated memory block is a memory block that should be returned to the computer system  20  but still has not been returned. In the present embodiment, assume the memory block  26   b  is the above-mentioned abnormally operated allocated memory block. The engineer can read the return address RA from the header  26   bh  of the memory block  26   b  (step  208 ). However, at this time, the engineer does not know that the memory block  26   b  is/has been allocated to which function by the computer system  20 . The engineer selects a greatest value (number) from all numbers that are stored in the symbol mapping table ST and smaller than the return address RA. The greatest value (number) corresponds to the symbol address F 1 A. In this way, it can be known that the memory block  26   b  storing the return address RA has been allocated by the computer system  20  to the first function F 1  corresponding to the symbol address F 1 A (step  210 ).  
         [0034]     In the above-mentioned embodiment, the present invention method is applied to the problems associated with memory leaks. However, the present invention method can also be utilized for recording a call stack or be applied on a system security design. Additionally, some other applications, like setting certain open functions to only allow certain modules to call, are covered by the present invention.  
         [0035]     In contrast to the prior art, the searching method according to the present invention is that there is definitely no need to occupy any additional non-volatile memory space. Also, in contrast to the prior art memory allocation information, the present invention memory allocation information stored in a header of an allocated memory block is only a return address, and it only requires 4 bytes of space in the header to store the return address. In this way, it is easy to precisely and quickly find a function associated with the allocated memory block. Hence, the present invention searching method generates less system overhead for the computer system. Additionally, the present invention reduces the expended time and memory space to further increase the execution performance and speed up the execution of the computer system.  
         [0036]     Those skilled in the art will readily observe that numerous modifications and alterations of the device and method may be made while retaining the teachings of the invention. Accordingly, the above disclosure should be construed as limited only by the metes and bounds of the appended claims.