File: mem.asm - Tab length: 1 2 4 8 - Lines: on off - No wrap: on off

001: ;===============================================================================
002: ;
003: ; FreeObjectHandles
004: ;
005: ; Free all handles used while assembling a source
006: ;
007: ; in  fp  Stack frame
008: ;
009: ; out nothing
010: ;
011: ; destroy d0-d2/a0-a1
012: ;
013: ;===============================================================================
014: 
015: FreeObjectHandles:
016:   move.w  BIN_HD(fp),d0
017:   bsr FreeHandle
018:   clr.w BIN_HD(fp)
019: 
020:   move.w  LABELS_HD(fp),d0
021:   bsr.s FreeHandle
022:   clr.w LABELS_HD(fp)
023: 
024:   rts
025: 
026: 
027: ;===============================================================================
028: ;
029: ; FreeHandle
030: ;
031: ; Free an handle, deleting the file if it belongs to one
032: ; Return without error if Handle is H_NULL
033: ;
034: ; in  d0.w  Handle
035: ;   fp  Stack frame
036: ;
037: ; out nothing
038: ;
039: ; destroy d0-d2/a0-a1
040: ;
041: ;===============================================================================
042: 
043: FreeHandle:
044:   move.w  d0,-(sp)
045:   beq.s  \End
046:   RAMT  RAM_kernel::Hd2Sym
047:   move.l  a0,d0
048:   bne.s \FreeFile
049:     ROMT  HeapFree  ; No symbol associated to this handle, free it
050:     bra.s \End
051: \FreeFile:        ; A file is associated to the handle, its SYM_ENTRY is at a0
052:   pea (a0)
053:   jsr UNLINK(fp)
054:   addq.l  #4,sp
055: \End: addq.l  #2,sp
056:   rts
057: 
058: 
059: ;===============================================================================
060: ;
061: ; AllocHandle
062: ;
063: ; Allocate an handle on the heap
064: ; Will call NeedRAM if first attempt fails. NeedRAM checks is swapping is allowed
065: ; Will call MainExit if the handle could not be allocated
066: ;
067: ; in  d0.l  size
068: ;   fp  Stack frame
069: ;
070: ; out d0.w  handle
071: ;   a0  ptr to the handle
072: ;
073: ; destroy d0-d2/a0-a1
074: ;
075: ;===============================================================================
076: 
077: AllocHandle:
078:   move.l  d0,-(sp)
079:   move.l  d0,-(sp)
080:   ROMT  HeapAlloc
081:   addq.l  #4,sp
082:   tst.w d0
083:   bne.s \End
084:     moveq.l #0,d0   ; We can archive all
085:     bsr NeedRAM
086:     ROMT  HeapAlloc
087:     tst.w d0
088:     bne.s \End
089:       move.w  #ERROR_MEMORY,ERROR(fp)
090:       pea StrErrorMemory(pc)
091:       bsr PrintToStderr
092:       bra MainExit
093: \End: movea.w d0,a0
094:   trap  #3
095:   addq.l  #4,sp
096:   rts
097: 
098: 
099: ;===============================================================================
100: ;
101: ; ReallocHandle
102: ;
103: ; Reallocate an handle on the heap
104: ;
105: ; SwapOut is automatically called for this handle
106: ; Call NeedRAM if first attempt fails. NeedRAM checks is swapping is allowed
107: ; Call MainExit if the handle could not be allocated
108: ;
109: ; in  d0.l  size
110: ;   d1.w  handle
111: ;   fp  Stack frame
112: ;
113: ; out d0.w  handle
114: ;   a0  ptr to the handle
115: ;
116: ; destroy d0-d2/a0-a1
117: ;===============================================================================
118: 
119: ReallocHandle:
120:   move.l  d0,-(sp)
121:   move.w  d1,-(sp)
122:   move.l  d0,-(sp)
123:   move.w  d1,-(sp)
124: 
125:   move.w  d1,d0
126:   bsr SwapOut
127: 
128:   jsr REALLOC(fp)   ; Use Pedrom's realloc to allow files larger than 64 kb
129:   addq.l  #6,sp
130:   tst.w d0
131:   bne.s \End
132:     move.w  (sp),d0   ; This handle musn't be swapped !
133:     bsr NeedRAM
134:     jsr REALLOC(fp)
135:     tst.w d0
136:     bne.s \End
137:       move.w  #ERROR_MEMORY,ERROR(fp)
138:       pea StrErrorMemory(pc)
139:       bsr PrintToStderr
140:       bra MainExit
141: \End: movea.w d0,a0
142:   trap  #3
143:   addq.l  #6,sp
144:   rts
145: 
146: 
147: ;===============================================================================
148: ;
149: ; NeedRAM
150: ;
151: ; Swap all possible data in flash, if allowed by the flag
152: ; This function is called only by AllocHandle and ReallocHandle
153: ; It won't throw an error if nothing can be swapped, calling functions will do that
154: ;
155: ; in  d0.w  Handle which musn't be swapped in case of reallocation
156: ;     Set to 0 by swapout to prevent a random file to be not swapped in
157: ;   fp  Stack frame
158: ;
159: ; out nothing
160: ;
161: ; destroy nothing
162: ;
163: ;===============================================================================
164: 
165: NeedRAM:
166:   movem.l d0-d5/a0-a1,-(sp)
167:   move.w  d0,d3
168: 
169:   ;----------------------------------------------------------------------
170:   ; Check if swapping is allowed
171:   ;----------------------------------------------------------------------
172: 
173:   movea.l CURRENT_FLAGS_PTR(fp),a0
174:   move.l  (a0),d0
175:   btst.l  #BIT_SWAP,d0
176:   beq.s \End
177: 
178:   ;----------------------------------------------------------------------
179:   ; Try to swap all availables handles and files
180:   ; It doesn't matter if all files are not really put in flash, the
181:   ; next (re)allocation will fail, that's all.
182:   ; So don't check if something is really performed
183:   ;----------------------------------------------------------------------
184: 
185:   ;----------------------------------------------------------------------
186:   ; Swap sources files
187:   ;----------------------------------------------------------------------
188: 
189:   move.w  FILES_LIST_HD(fp),d4
190:   moveq.l #0,d5
191: 
192: \Loop:  movea.w d4,a0
193:   trap  #3
194:   cmp.w (a0)+,d5
195:   beq.s \EndLoop
196:     move.w  d5,d0
197:     mulu.w  #FILE.sizeof,d0
198:     move.w  FILE.Handle(a0,d0.l),d0 ; SwapIn () needs the Z-Flag to be set if the handle exists, so don't change that !
199:     bsr.s \SwapIn
200:     addq.w  #1,d5
201:     bra.s \Loop
202: \EndLoop:
203: 
204:   ;----------------------------------------------------------------------
205:   ; Swap objects handles
206:   ;----------------------------------------------------------------------
207: 
208:   move.w  FILES_LIST_HD(fp),d0
209:   bsr.s \SwapIn       ; SwapIn () needs the Z-Flag to be set if the handle exists, so don't change that !
210: 
211:   move.w  BIN_HD(fp),d0
212:   bsr.s \SwapIn
213: 
214:   move.w  LABELS_HD(fp),d0
215:   bsr.s \SwapIn
216: 
217: \End: movem.l (sp)+,d0-d5/a0-a1
218:   rts
219: 
220: 
221: ;-------------------------------------------------------------------------------
222: ; Swap a file :
223: ; - chekc that the handle exists
224: ; - check that the handle mustn't be kept in RAM
225: ; -   get a tmp filename if the handle doesn't belong to a file
226: ; -   create a file and give it the handle
227: ; - archive the file
228: ;
229: ; This function is called only by NeedRAM
230: ;-------------------------------------------------------------------------------
231: 
232: \SwapIn:
233:   beq.s \NoSwapIn     ; Handle not created yep (Z-flag already set)
234:   cmp.w d0,d3       ; Handle currently reallocated : don't swap it
235:   beq.s \NoSwapIn
236:     lea -18(sp),sp    ; Frame buffer for pdtlib::Hd2FullName
237:     movea.l sp,a0
238:     move.w  d0,d2     ; Save handle
239:     jsr HD_2_FULL_NAME(fp)
240:     tst.w d0
241:     bne.s \ArchiveFile    ; This handle already belongs to a file
242:       move.w  d2,-(sp)  ; Save Hd
243:       suba.l  a0,a0   ; Use PedroM internal buffer
244:       jsr TMPNAM(fp)  ; We don't care about the filename itself and don't have to save it
245:       move.w  (sp)+,d0  ; Hd
246:       pea (a0)    ; Save filename
247:       jsr CREATE_FILE(fp)
248:       movea.l (sp)+,a0  ; Filename
249:       tst.w d0
250:       beq.s \Fail
251: 
252: \ArchiveFile: jsr ARCHIVE_FILE(fp)
253: \Fail:    lea 18(sp),sp
254: \NoSwapIn:
255:   rts
256: 
257: 
258: ;===============================================================================
259: ;
260: ; SwapOut
261: ;
262: ; Swap an handle in RAM. If it fails, call NeedRAM, then retry.
263: ; If it fails again, throw an error and call Main Exit
264: ;
265: ; in  d0.w  Handle which must be swapped in RAM
266: ;   fp  Stack frame
267: ;
268: ; out nothing
269: ;
270: ; destroy nothing
271: ;
272: ;===============================================================================
273: 
274: SwapOut:
275:   movem.l d0-d3/a0-a2,-(sp)
276:   move.w  d0,d3
277: 
278:   ;----------------------------------------------------------------------
279:   ; Check if the file is already in RAM
280:   ;----------------------------------------------------------------------
281: 
282:   movea.w d0,a0
283:   trap  #3
284:   cmpa.l  ROM_BASE(fp),a0
285:   bcs.s \End      ; Already in RAM
286: 
287:   ;----------------------------------------------------------------------
288:   ; Try twice to pop in RAM. Call NeedRAM if first attempt fails
289:   ;----------------------------------------------------------------------
290: 
291:   bsr.s \Unarchive
292:   bne.s \End
293:   bsr NeedRAM     ; d0 already set to 0
294:   bsr.s \Unarchive
295:   bne.s \End
296: 
297:   ;----------------------------------------------------------------------
298:   ; Failed. Throw an error and quit
299:   ;----------------------------------------------------------------------
300: 
301:   move.w  #ERROR_MEMORY,ERROR(fp)
302:   pea StrErrorMemory(pc)
303:   bsr PrintToStderr
304:   bra MainExit
305: 
306:   ;----------------------------------------------------------------------
307:   ; Success
308:   ;----------------------------------------------------------------------
309: 
310: \End: movem.l (sp)+,d0-d3/a0-a2
311:   rts
312: 
313:   ;----------------------------------------------------------------------
314:   ; Unarchive a file. Z-flag set according to success
315:   ;----------------------------------------------------------------------
316: 
317: \Unarchive:
318:   lea -18(sp),sp    ; Buffer for pdtlib::Hd2FullName
319:   move.w  d3,d0     ; Handle
320:   movea.l sp,a0
321:   jsr HD_2_FULL_NAME(fp)
322:   jsr UNARCHIVE_FILE(fp)
323:   tst.w d0
324:   lea 18(sp),sp
325:   rts