home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / program / lynxlib / tricks.c < prev    next >
C/C++ Source or Header  |  1993-10-23  |  2KB  |  72 lines

  1. /* This source file is part of the LynxLib miscellaneous library by
  2. Robert Fischer, and is Copyright 1990 by Robert Fischer.  It costs no
  3. money, and you may not make money off of it, but you may redistribute
  4. it.  It comes with ABSOLUTELY NO WARRANTY.  See the file LYNXLIB.DOC
  5. for more details.
  6. To contact the author:
  7.     Robert Fischer \\80 Killdeer Rd \\Hamden, CT   06517   USA
  8.     (203) 288-9599     fischer-robert@cs.yale.edu                 */
  9.  
  10. /* Tricks in dealing with the OS */
  11. #include <e_osbind.h>
  12.  
  13. /* ------------------------------------------------- */
  14. char *malloc_highend(size)
  15. /* Malloc's at the high end of memory */
  16. LONG size;
  17. {
  18. char *mem;        /* Temporary memory block */
  19. LONG memsize;    /* Full size of available memory */
  20. char *out;        /* Output thing */
  21.     /* Allocate at the high end of memory */
  22.     memsize = (LONG)Malloc(-1);
  23.     if (memsize <= size) return NULL;
  24.     mem = Malloc(memsize - size);
  25.     if (mem == NULL) return NULL;
  26.     out = Malloc(size);
  27.     Mfree(mem);
  28.     return out;
  29. }
  30. /* ------------------------------------------------- */
  31. /* Allocates a "permanent" memory block in high memory */
  32. char *PMalloc(size)
  33. LONG size;
  34. {
  35. BASEPAGE *b, *old_b;    /* For scanning the basepage linked list */
  36. long usp;
  37. char *block;
  38.  
  39.     usp = Super(0L);
  40.  
  41.     /* Find oldest basepage */
  42.     for (b = BP; b != NULL; b = (BASEPAGE *)(b->p_parent))
  43.         old_b = b;
  44.  
  45.     /* Make that process temporarily the current process */
  46. #ifdef DEBUG
  47.     if (CURPROC != BP) {
  48.         printf("Mismatch: CURPROC=%lx, BP=%lx\n", CURPROC, (long) BP);
  49.         Super(usp);
  50.         exit(1);
  51.     } 
  52. #endif
  53.     CURPROC = old_b;
  54.  
  55.     /* Allocate memory */
  56.     block = malloc_highend(size);
  57.  
  58.     /* Restore current process */
  59.     CURPROC = BP;
  60.  
  61.     Super(usp);
  62.     return block;
  63. }
  64. /* ------------------------------------------------- */
  65. term_res(out)
  66. /* Terminates and stays resident, saving the text, data, bss and basepage */
  67. int out;        /* Output code */
  68. {
  69.     Ptermres(BP->p_tlen + BP->p_dlen + BP->p_blen + 256, out);
  70. }
  71. /* ------------------------------------------------- */
  72.