;       SCCSID = @(#)sswmemp.asm	6.1 90/11/18

;      SCCSID = @(#)sswmemp.asm	1.5 90/03/19
; ****************************************************************************
; *									     *
; *			  IBM/Microsoft Confidential			     *
; *									     *
; *		    Copyright (c) IBM Corporation  1987, 1990		     *
; *		    Copyright (c) Microsoft Corp.  1987, 1990		     *
; *			      All Rights Reserved			     *
; *									     *
; ****************************************************************************

			 page 60,132

;*********************** START OF SPECIFICATIONS ***********************/
;								       */
;  SUBROUTINE NAME:  SSWMEMP					       */
;								       */
;  DESCRIPTIVE NAME:  Screen Switcher memory table lookup	       */
;								       */
;  FUNCTION:  Scans memory table for possible locations for sgcbs. If  */
;	      no more room, then a call is issued to reallocate        */
;	      memory. This function updates the table and	       */
;	      sgpointeroff.					       */
;								       */
;  NOTES:  Executes on Level 3					       */
;								       */
;  ENTRY POINT:  SSMemMap					       */
;								       */
;  INPUT:  DS register must point to the common data segment.	       */
;	   BX register contains the sg# 			       */
;								       */
;  EXIT-NORMAL: AX = no_error					       */
;								       */
;  EXIT-ERROR:	AX = error_scs_no_memory_available		       */
;								       */
;  EFFECTS:  If memory is not available then a reallocation call is    */
;	     issued to the system. If memory is available or becomes   */
;	     available, a slot is found for the sgcb# passed in via    */
;	     the ax register. The table is updated to reflect the      */
;	     assignment of the memory block to the sg.		       */
;								       */
;	     Finally, sgpointeroff and DI are set to point to the      */
;	     sgcb which is being created.			       */
;								       */
;  INTERNAL REFERENCES: 					       */
;    ROUTINES:	SSMemTableSu					       */
;								       */
;  EXTERNAL REFERENCES: 					       */
;    ROUTINES:	DosReallocSeg					       */
;								       */
;	Change History: 					       */
;								       */
;	@TL6 - 03/01/90 - DCR 704 - Merge SMG/SSW instance data into   */
;			  OS2CHAR				       */
;								       */
;								       */
;************************ END OF SPECIFICATIONS ************************/

EXTRN DOSREALLOCSEG:FAR
EXTRN SSMEMTABLESU:NEAR
EXTRN memory_pool:byte, mempoolsize:word, segsize:word
EXTRN allocation_cnt:word, memtableptr:word, sgpointeroff:word

.xlist
INCLUDE SSWEQUAT.INC
INCLUDE SSWEXTR1.INC
INCLUDE STRUC.INC
INCLUDE ERROR.INC
INCLUDE ERROR2.INC
.list

.386p

CODE	SEGMENT WORD PUBLIC 'CODE' USE16                                ;@TL6

 ASSUME  CS:CODE, DS:NOTHING,SS:NOTHING,ES:NOTHING			;@TL6

 PUBLIC  SSMEMMAP

SSMEMMAP       PROC    NEAR

 push	 dx
 push	 cx
 push	 bx

 mov	 di,offset memory_pool			    ; We will look through
 mov	 bx,1					    ; the memory table to
 mov	 dx,eot 				    ; find available space
 mov	 cx,null				    ; for our sgcb
 sub	 ax,ax								;@TL6

 .WHILE <[di+2] ne bl> OR
 .WHILE <[di] e dx>
  .IF <[di] e dx>				    ; Have we reached the
   .IF <[di+2] ne cx>				    ; end-of-table?
    mov     di,[di+2]				    ; If so, and if the
   .ELSE					    ; ptr_nxt_table is
    mov     ax,null				    ; valid, follow the
    .LEAVE SHORT				    ; chain - or leave with
   .ENDIF					    ; insufficient memory
  .ELSE 					    ; If not eot
   add	   di,18				    ; Go to next block
  .ENDIF
 .ENDWHILE

 .IF <ax e null>				    ; If no more room for
  mov	  ax,mempoolsize			    ; sgcbs we need to
  add	  segsize,ax				    ; allocate memory
  push	  segsize
  push	  ds
  call	  DosReallocseg
  .IF <zero ax> 							;@TL6
   inc	   allocation_cnt
   mov	   bx,mempoolsize			    ; Setup pointer to new
   add	   memtableptr,bx			    ; memory table

   call    ssmemtablesu 			    ; Set up a new memory
   mov	   bx,memtableptr			    ; table and store the
   mov	   [di+2],bx				    ; ptr in the old table
   mov	   di,bx				    ; move pointer to di

  .ENDIF
 .ENDIF
 pop	 bx					    ; Get sg#
 .IF <zero ax>								;@TL6
  mov	  ax,occupied
  mov	  cx,null
  mov	  [di+2],al				    ; No longer available
  mov	  [di+3],bx				    ; Store sg# in table
  mov	  [di+5],cx				    ; Set #pids to null
  mov	  [di+11],al				    ; and the next semiblock
  mov	  [di+12],bx				    ; is taken too.
  mov	  [di+14],cx				    ; The #pids is set to
						    ; null to allow us to
						    ; identify an sgcb
  mov	  di,[di+7]				    ; Set DI to sgcb
  mov	  sgpointeroff,di			    ; Set pointer to sgcb
 .ENDIF

 pop	 cx
 pop	 dx

 ret
SSMEMMAP       ENDP
CODE	ENDS								;@TL6
 END
