⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mem.inc

📁 还是一个词法分析程序
💻 INC
📖 第 1 页 / 共 2 页
字号:
; FILE: MEM.INC
; Copyright (c) 1991 By Borland International, Inc.
page


;
; shrink_memory - The main program should call this routine before
;                initializing any memory system objects. This shrinks
;                the DOS memory block used by the program to the
;                minimum possible size.
;
  global shrink_memory:proc


;****************************************************************
; Memory manager
; This memory manager supports simple allocation
; and deallocation of blocks from memory.
;
;

; This value is used in the NEXT pointer to indicate a FREE block
FREE_BLOCK = 0FFFFH


; Note that the VMT's for the objects in this file must be included in
; one of the .OBJ's of a project. This is accomplished by the MEM.ASM
; module defining the following equate before including this file:
;_MAKE_MEMVMT_ = 1

; Memory_block
;       A memory block is created for each unit on the heap. Memory
;   blocks for unused areas of the heap are larger, and contain more
;   information, than used blocks on the heap. Note that there are
;   derived classes that handle implementing used_blocks.
;
; \/ Not yet implemented
; initget- Handles getting an extra block of memory from DOS, and
;          allocating getting it set up. It does not need to be
;          passed the address of a memory block to init.
;          This is an example of a constructor that even does it's
;          own allocation, as well as setting up of VMT.
;     Arguments:(segments of previous and next blocks)
;     Returns:   AX segment of block that was allocated
;
;
; init   - Handles initializing a block to an unused state.
;          Includes setting the VMT pointer to type the block as a
;          memory_block and it also sets the next pointer and the
;          size. The previous block pointer is unused since the
;          memory system does not always find the previous block out
;          of efficiency considerations.
;     Arguments:(DS:SI instance ptr), (segments of previous and next blocks)
;
;
; deinit - Handles destruction of the memory block. Currently has no
;          function for memory_block or memory_usedblock. No protocol
;          for usage is setup.
;
; memstart - Returns fixed offset within memory blocks that marks the
;            start of information that is only optionally used if the
;            block is empty. This allows descendants to increase the
;            amount of bookkeeping information used in the memory_block
;            for all blocks, free and used.
;            Users of the memory system only need to store segment
;            values of alloced blocks, and they can use this function to
;            fill in the offset if they want.
;            Of course, this behaviour may not be compatible with future
;            memory blocks which might not neccessarily return a constant
;            value for the memstart.
;     Arguments: None
;     Returns:   BX  with fixed offset value
;
;
; Show - Shows whether or not the block is free.
;        If the block is free, additional information concerning the block
;        is printed, including it's next pointer and the size of the free
;        area.
;      Arguments: DS:SI - Address of the memory block
;
;
; MatchNext - Finds the address if the block whose next pointer points
;             to the given block
;      Arguments: DS:SI - Address of memory block
;                 AX    - Segment address of block to find next ptr to.
;      Returns:   AX    - Segment of memory block that points to the
;                            given block with next.
;                         AX=0 if a previous block was not found.
;                 DS:SI - Changed
;
; ScanFree   - If the current block is not free, this routine scans forward
;              until a free block is free
;      Arguments: DS:SI - Address of memory block
;      Returns:   DS:SI - Points to a free block.
;                         DS=0 if no more free blocks
;                 AX - Size of block in paragraphs. 0 if not free.
;                      Size is already adjusted for bookkeeping info.
;
;
; GetNext    - Returns the segment address of the next block.
;      Arguments: DS:SI - Address of memory block
;      Returns:   AX    - Segment of next memory block
;
;
; SetNext    - Sets the pointer to the next block
;      Arguments: DS:SI - Address of memory block
;                 AX    - Segment of next memory block
;      Returns:   None
;
;
ifdef _DO_SETPREV_
; SetPrev  - Sets the pointer to the previous memory block. If the
;            block is in use, this call does nothing.
;      Arguments: DS:SI - Address of memory block
;                 AX    - Segment of prev memroy block
;      Returns:   None
endif
;
; SetSize  - Sets the size field of the memory block. If the
;            block is in use, this call does nothing.
;      Arguments: DS:SI - Address of memory block
;                 AX    - Size of the memory block
;      Returns:   None
;
; IsFree
;      Arguments: DS:SI - Address of the memory block
;      Returns    Z flag set if the block is free.
;
;
; MarkUsed - Marks the block as used. This is done by copying the
;            NEXT2 pointer into NEXT. This will erase the FFFF
;            signature in the NEXT ptr, which will show the
;            block as used. This does nothing if the block is already
;            marked as used.
;
;      Arguments: DS:SI - Address of the memory block
;
; MarkFree - Marks the block as free. This is done by copying the
;            NEXT pointer into NEXT2, and putting 0FFFFH in NEXT.
;            Also the size of the block is calculated and filled in.
;            Also the previous block pointer is filled in.
;
;      Arguments: DS:SI - Address of the memory block
;                 AX    - Previous block
;
; RawBlockSize - Returns the total size in paragraphs of the block,
;                including bookkeeping information. Only works for
;                blocks that are free.
;      Arguments: DS:SI - Address of memory block
;      Returns:   AX
;
; BreakBlock - Breaks a free block in two.
;      Arguments: DS:SI - Address of the memory block
;                 AX    - New desired size of the block, not counting
;                         bookkeeping info.
;      Returns:   AX      Segment address of new free block, if block broken
;                         off, or returns same as original DS if whole block
;                         is used.
;
; Combine - Combines a block with it's previous block, if both are free.
;           The previous block is guaranteed to be free if this is called.
;           Alloc calls this during it's forward walk thru the stack.
;           Blocks that are not free should ignore this call.
;      Arguments: DS:SI - Address of the memory block
;                 AX    - Address of the previous block.
;      Returns:   DS:SI - Address of the previous block if the combine was
;                         done, otherwise they are unchanged.
;
;
; LockBlock - Currently not used. This call should be made before using the
;             contents of a block of memory allocated from the heap. This
;             routine may return a new address to use as a pointer to the
;             actual memory contents.
;             This is automatically called before the ALLOC call terminates.
;
; UnLockBlock - Currently not used. This call should be given the value
;             returned by the UnLockBlock routine.
;
; AllocFail - This method is called for all blocks if an allocation
;             request of the memory system is about to fail. If the
;             block does nothing with this request, it needs to return
;             the Z flag set. If it is able to process this request
;             and cause memory to be freed, or in some other way enable
;             the memory system to possibly succeed, then it can return
;             the zero flag unset, and the memory_system will retry the
;             allocation request.
;             Example: Diskswapping memory blocks could delay actual
;             swap out until they recieve this call. Memory blocks and
;             the controlling swap block should know the location of
;             each other, so they both can respond to the request.
;             Blocks that implement memory swapping might even try to
;             swap out several blocks before returning with the Z flag
;             unset.


; Note the size of PREV & NEXT. Because MASM mode makes all symbols
;  be global in scope, if other structures define their own PREV or
;  NEXT pointers, they must be of the same size, and at the same
;  location within the structure. Since other routines may want
;  fullsize NEXT and PREV pointers, this structure defines them to be
;  the same size.... However, a more advanced memory block could
;  make use of the extra word to implement some sort of disk cacheing
;  or other use, especially if lock/unlock system is used for all
;  blocks.
; Note that FindPrev_ is defined to avoid conflict with memory system
;  FindPrev routine.
; Note also show

     global  memory_block_initget:proc
     global  memory_block_init:proc
     global  memory_block_deinit:proc
     global  mb_show:proc
     global  memory_block_reserved_size:proc
     global  mb_findprev:proc
     global  mb_scan:proc
     global  mb_getnext:proc
     global  mb_isfree:proc
     global  mb_setprev:proc
     global  mb_setnext:proc
     global  mb_setsize:proc
     global  mb_rawblocksize:proc
     global  mb_breakblock:proc
     global  memory_block_markused:proc
     global  memory_block_combine:proc
     global  memory_block_markfree:proc
     global  memory_block_lock:proc
     global  memory_block_unlock:proc
     global  memory_block_allocfail:proc

memory_block  STRUC METHOD {
 initget:mptr=memory_block_initget   ;  SI returns seg of allocated block.
 init:mptr=memory_block_init         ;  DS:SI is block.
 virtual deinit:mptr=memory_block_deinit
 virtual show:mptr=mb_show
 virtual memstart:mptr=memory_block_reserved_size
 virtual MatchNext:mptr=mb_findprev
 virtual ScanFree:mptr=mb_scan
 virtual GetNext:mptr=mb_getnext
 virtual IsFree:mptr=mb_isfree

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -