; Claim memory using Unix brk() call ; ================================== ; mdfs.net/Info/Comp/PDP11/ProgTips/ClaimMem - J.G.Harston ; ; Tries to claim as much memory as possible, starting at the top of memory ; and stepping down in steps of 256 bytes until the memory claim is ; successful. ; On entry, nothing ; On exit, r1=top of claimable memory ; r0=corrupted .IO_ClaimMem clr r1 ; r1=top of memory, start at &10000-256 mov #&8911,TRAP_BUF ; SYS brk .IO_InitMemLp sub #256,r1 ; Step down 256 bytes mov r1,TRAP_BUF+2 ; Store as TRAP argument TRAP 0 ; SYS sbrk,addr EQUW TRAP_BUF bcs IO_InitMemLp ; Memory not claimable, try a bit less ; ; To work on BSD2.9 needs to be told to move the stack ; This code assumes that the return address is the sole item on the stack ; To work transparently on both Bell Unix and BSD2.9, SIGSYS must be caught ; and set to return CS. mov (sp)+,r0 ; Get return address mov r1,sp ; Move stack to memory we've just claimed mov -(sp),r1 ; Restack the return address on the new stack TRAP 58 ; SYS local,nostk EQUW TRAP_NOSTK ; bcs IO_ClaimMemBell ; CS, SYS local doesn't exist, we are running on Bell Unix bcc IO_ClaimMemBSD ; CC, SYS local exists, we are running on BSD2.9 ; .IO_ClaimMemBSD .IO_ClaimMemBell rts pc .TRAP_NOSTK EQUW 4 ; SYS nostk .TRAP_BUF EQUW 0 EQUW 0