;       SCCSID = @(#)smrfreid.asm	6.3 92/02/21
;       SCCSID @(#)smrfreid.asm	6.3 92/02/21
PAGE 60,132                             ;
; ****************************************************************************
; *                                                                          *
; *                       IBM/Microsoft Confidential                         *
; *                                                                          *
; *                 Copyright (c) IBM Corporation  1987, 1990                *
; *                 Copyright (c) Microsoft Corp.  1987, 1990                *
; *                           All Rights Reserved                            *
; *                                                                          *
; ****************************************************************************

;***************************************************************************
;
; Source File Name:     SMGFSGID.ASM
;
; Subroutine Name:      DOSSMFREESGID
;
; Descriptive Name:     Free the specified screen group number.
;
; Function:     Converts the specified screen group number to a
;               bit mask and turns off the bit associated with
;               the screen group in the array of words used to
;               track free screen groups.
;
; Notes:
;       Dependencies:   The queue must be protected by a RAM
;                       semaphore from the calling subroutine.
;
;       Restrictions:   None
;
; Change History:
;   @CJ6  03/23/90  PTR B712413 Shift bit offset for session to free.
;   @MHW  09/13/90  PTR B715818 In-line performance (MMIO) hooks
;   CJ52  01/27/92  PTR B731604 Don't reuse last SG# freed for next SG#.
;
; Entry Point:
;       Linkage:  EXTRN DOSSMFREESGID:NEAR
;                       CALL    DOSSMFREESGID
;
; Inputs:
;       DX      SGID       Screen group ID
;       DS      RDATA
;
; Exit Normal:
;       AX      ReturnCode Value = 0, screen group number freed
;
; Exit Errors:
;       None
;
; Effects:      All other registers preserved.
;
; Internal References:
;          Data:
;                          maxscrngrprs
;                          freegroups
;                          usedscrngrps
;
;                       The following data is in a shared data segment(SDATA)
;
;          Routines:
;
; External References:
;          Data:
;          Routines:
;
;***************************************************************************
ifdef I1
;
;  Correct DWord = SG# MOD 32
;  Mark screen group number available for use
;  Decrement number of used groups
;  RC = No Error
;
;***************************************************************************
else

.386C
    EXTRN UsedScrnGrps      : WORD
    EXTRN FreeGroups        : DWORD
    EXTRN LastSGFreed       : WORD      ; Last SG that went away        ;CJ52
.xlist
    INCLUDE SMRSGCB.INC
    INCLUDE INFOSEG.INC                 ; needed by smmmpha.inc         ;MHW
    INCLUDE SMMMPHA.INC                 ; Performance hook macros       ;MHW
.xcref
    INCLUDE ERROR2.INC
    INCLUDE SMRRDATA.INC
    INCLUDE STRUC.INC
.cref
.list

CODE SEGMENT WORD PUBLIC 'CODE' USE16
    ASSUME CS:CODE,DS:RDATA,SS:NOTHING,ES:NOTHING

    PUBLIC DOSSMFREESGID
DOSSMFREESGID PROC NEAR

;  ENTER 0,0                                                            ;CJ10
   PUSH SI                              ; Save registers
;  PUSH EDX                                                             ;CJ10

   MMPHPreDOSSMFreeSGId                 ; Performance Hook (8104h)      ;MHW

;  **********************************************
;  *  Use SI to point to the correct flag word  *
;  **********************************************
   MOV  AX, LastSGFreed                 ; Get last SG# that died        ;CJ52
   XOR  AH, AH                          ; Get rid of session type byte  ;CJ10,VR6
   MOV  SI, AX                          ; Locate correct DWord          ;CJ10
   SHR  SI, 5                           ; SG#/32
   SHL  SI, 2                           ; *4 for double word offset     ;@CJ6


;  ************************************************
;  *  Mark screen group number available for use  *
;  ************************************************
   AND  EAX, 1FH                        ; DX = SGID MOD 32              ;CJ10
   BTR  [SI].FreeGroups, EAX            ; Reset (0) bit in free groups  ;CJ10

   DEC  UsedScrnGrps                    ; Decrement number of used groups
   SUB  AX, AX                          ; Successful return code        ;CJ10

   MOV  LastSGFreed, AX                 ; Reset last SG# freed          ;CJ52

;  POP  EDX                                                             ;CJ10
   POP  SI

   MMPHPostDOSSMFreeSGId                ; Performance Hook (8184h)      ;MHW

;  LEAVE                                                                ;CJ10
   RET                                  ; Return to caller

DOSSMFREESGID ENDP
CODE ENDS

endif
    END
