home *** CD-ROM | disk | FTP | other *** search
/ Computer Select (Limited Edition) / Computer Select.iso / dobbs / v17n05 / 386bsd.592 next >
Encoding:
Text File  |  1992-03-30  |  9.6 KB  |  228 lines

  1. _PORTING UNIX TO THE 386: MISSING PIECES, PART I_
  2. by William Frederick Jolitz and Lynne Greer Jolitz
  3.  
  4.  
  5. [LISTING ONE]
  6.  
  7. /* Copyright (c) 1992 William Jolitz. All rights reserved.
  8.  * Written by William Jolitz 1/92
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions are met:
  11.  * 1. Redistributions of source code must retain the above copyright notice,
  12.  *    this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright notice,
  14.  *    this list of conditions and the following disclaimer in the documentation
  15.  *    and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement: This software is a component
  18.  *    of "386BSD" developed by William F. Jolitz, TeleMuse.
  19.  * 4. Neither the name of the developer nor the name "386BSD" may be used to
  20.  *    endorse or promote products derived from this software without specific
  21.  *    prior written permission.
  22.  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ AND
  23.  * IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS SOFTWARE SHOULD
  24.  * NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT. THE DEVELOPER URGES THAT USERS
  25.  * WHO REQUIRE A COMMERCIAL PRODUCT NOT MAKE USE OF THIS WORK.
  26.  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER "AS IS" AND ANY EXPRESS OR 
  27.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  28.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  29.  * EVENT SHALL THE DEVELOPER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  32.  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  33.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  34.  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  35.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.  * Resource lists. Usage:
  37.  *      rlist_free(&swapmap, 100, 200); add space to swapmap
  38.  *      rlist_alloc(&swapmap, 100, &loc); obtain 100 sectors from swap
  39.  */
  40.  
  41. /* A resource list element. */
  42. struct rlist {
  43.     unsigned    rl_start;   /* boundaries of extent - inclusive */
  44.     unsigned    rl_end;     /* boundaries of extent - inclusive */
  45.     struct rlist    *rl_next;   /* next list entry, if present */
  46. };
  47.  
  48. /* Functions to manipulate resource lists.  */
  49. extern rlist_free __P((struct rlist **, unsigned, unsigned));
  50. int rlist_alloc __P((struct rlist **, unsigned, unsigned *));
  51. extern rlist_destroy __P((struct rlist **));
  52.  
  53. /* heads of lists */
  54. struct rlist *swapmap;
  55.  
  56.  
  57.  
  58.  
  59. [LISTING TWO]
  60.  
  61. /* Copyright (c) 1992 William Jolitz. All rights reserved.
  62.  * Written by William Jolitz 1/92
  63.  * Redistribution and use in source and binary forms, with or without
  64.  * modification, are permitted provided that the following conditions are met:
  65.  * 1. Redistributions of source code must retain the above copyright notice,
  66.  *    this list of conditions and the following disclaimer.
  67.  * 2. Redistributions in binary form must reproduce the above copyright notice,
  68.  *    this list of conditions and the following disclaimer in the documentation
  69.  *    and/or other materials provided with the distribution.
  70.  * 3. All advertising materials mentioning features or use of this software
  71.  *    must display the following acknowledgement: This software is a component
  72.  *    of "386BSD" developed by William F. Jolitz, TeleMuse.
  73.  * 4. Neither the name of the developer nor the name "386BSD" may be used to
  74.  *    endorse or promote products derived from this software without specific
  75.  *    prior written permission.
  76.  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ AND
  77.  * IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS SOFTWARE SHOULD
  78.  * NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT. THE DEVELOPER URGES THAT USERS
  79.  * WHO REQUIRE A COMMERCIAL PRODUCT NOT MAKE USE OF THIS WORK.
  80.  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER "AS IS" AND ANY EXPRESS OR 
  81.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  82.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  83.  * EVENT SHALL THE DEVELOPER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  84.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  85.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  86.  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  87.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  88.  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  89.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  90.  
  91. #include "sys/param.h"
  92. #include "sys/cdefs.h"
  93. #include "sys/malloc.h"
  94. #include "rlist.h"
  95.  
  96. /* Resource lists. */
  97. /* Add space to a resource list. Used to either
  98.  * initialize a list or return free space to it. */
  99. rlist_free (rlp, start, end)
  100. register struct rlist **rlp; unsigned start, end; {
  101.     struct rlist *head;
  102.     head = *rlp;
  103. loop:
  104.     /* if nothing here, insert (tail of list) */
  105.     if (*rlp == 0) {
  106.         *rlp = (struct rlist *)malloc(sizeof(**rlp), M_TEMP, M_NOWAIT);
  107.         (*rlp)->rl_start = start;
  108.         (*rlp)->rl_end = end;
  109.         (*rlp)->rl_next = 0;
  110.         return;
  111.     }
  112.     /* if new region overlaps something currently present, panic */
  113.     if (start >= (*rlp)->rl_start && start <= (*rlp)->rl_end)
  114.         panic("overlapping rlist_free: freed twice?");
  115.     if (end >= (*rlp)->rl_start && end <= (*rlp)->rl_end)
  116.         panic("overlapping rlist_free: freed twice?");
  117.     /* are we adjacent to this element? (in front) */
  118.     if (end+1 == (*rlp)->rl_start) {
  119.         /* coalesce */
  120.         (*rlp)->rl_start = start;
  121.         goto scan;
  122.     }
  123.     /* are we before this element? */
  124.     if (end < (*rlp)->rl_start) {
  125.         register struct rlist *nlp;
  126.         nlp = (struct rlist *)malloc(sizeof(*nlp), M_TEMP, M_NOWAIT);
  127.         nlp->rl_start = start;
  128.         nlp->rl_end = end;
  129.         nlp->rl_next = *rlp;
  130.         *rlp = nlp;
  131.         return;
  132.     }
  133.     /* are we adjacent to this element? (at tail) */
  134.     if ((*rlp)->rl_end + 1 == start) {
  135.         /* coalesce */
  136.         (*rlp)->rl_end = end;
  137.         goto scan;
  138.     }
  139.     /* are we after this element */
  140.     if (start  > (*rlp)->rl_end) {
  141.         rlp = &((*rlp)->rl_next);
  142.         goto loop;
  143.     } else
  144.         panic("rlist_free: can't happen");
  145. scan:
  146.     /* can we coalesce list now that we've filled a void? */
  147.     {
  148.         register struct rlist *lp, *lpn;
  149.         for (lp = head; lp->rl_next ;) { 
  150.             lpn = lp->rl_next;
  151.             /* coalesce ? */
  152.             if (lp->rl_end + 1 == lpn->rl_start) {
  153.                 lp->rl_end = lpn->rl_end;
  154.                 lp->rl_next = lpn->rl_next;
  155.                 free(lpn, M_TEMP);
  156.             } else
  157.                 lp = lp->rl_next;
  158.         }
  159.     }
  160. }
  161. /* Obtain a region of desired size from a resource list. If nothing available 
  162.  * of that size, return 0. Otherwise, return a value of 1 and set resource 
  163.  * start location with *loc. (Note: loc can be zero if we don't wish value) */
  164. int rlist_alloc (rlp, size, loc)
  165. struct rlist **rlp; unsigned size, *loc; {
  166.     register struct rlist *lp = *rlp, *olp = 0;
  167.     /* walk list, allocating first thing that's big enough (first fit) */
  168.     for (; *rlp; rlp = &((*rlp)->rl_next))
  169.         if(size <= (*rlp)->rl_end - (*rlp)->rl_start + 1) {
  170.             /* hand it to the caller */
  171.             if (loc) *loc = (*rlp)->rl_start;
  172.             (*rlp)->rl_start += size;
  173.             /* did we eat this element entirely? */
  174.             if ((*rlp)->rl_start > (*rlp)->rl_end) {
  175.                 lp = (*rlp)->rl_next;
  176.                 free (*rlp, M_TEMP);
  177.                 *rlp = lp;
  178.             }
  179.             return (1);
  180.         }
  181.     /* nothing in list that's big enough */
  182.     return (0);
  183. }
  184.  
  185. /* Finished with this resource list, reclaim all space and 
  186.  * mark it as being empty.  */
  187. rlist_destroy (rlp)
  188. struct rlist **rlp; {
  189.     struct rlist *lp, *nlp;
  190.  
  191.     lp = *rlp;
  192.     *rlp = 0;
  193.     for (; lp; lp = nlp) {
  194.         nlp = lp->rl_next;
  195.         free (lp, M_TEMP);
  196.     }
  197. }
  198.  
  199.  
  200.  
  201. [LISTING THREE]
  202.  
  203. /* Excerpted with permission from 4.3BSD include file 
  204.  * "/usr/include/sys/exec.h"
  205.  * Redistribution and use in source and binary forms are freely permitted
  206.  * provided that the above copyright notice and attribution and date of work
  207.  * and this paragraph are duplicated in all such forms.
  208.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  209.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  210.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  211.  * Header prepended to each a.out file.
  212.  */
  213. struct exec {
  214.         long    a_magic;        /* magic number */
  215. unsigned long   a_text;         /* size of text segment */
  216. unsigned long   a_data;         /* size of initialized data */
  217. unsigned long   a_bss;          /* size of uninitialized data */
  218. unsigned long   a_syms;         /* size of symbol table */
  219. unsigned long   a_entry;        /* entry point */
  220. unsigned long   a_trsize;       /* size of text relocation */
  221. unsigned long   a_drsize;       /* size of data relocation */
  222. };
  223.  
  224. #define OMAGIC  0407            /* old impure format */
  225. #define NMAGIC  0410            /* read-only text */
  226. #define ZMAGIC  0413            /* demand load format */
  227.  
  228.