home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / os / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  21.0 KB  |  896 lines

  1. /***********************************************************
  2. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /* $XConsortium: utils.c,v 1.109 92/02/24 19:03:14 keith Exp $ */
  25. #include "Xos.h"
  26. #include <stdio.h>
  27. #include "misc.h"
  28. #include "X.h"
  29. #include "input.h"
  30. #include "opaque.h"
  31. #include <signal.h>
  32. #ifndef SYSV
  33. #include <sys/resource.h>
  34. #endif
  35.  
  36. #ifdef SIGNALRETURNSINT
  37. #define SIGVAL int
  38. #else
  39. #define SIGVAL void
  40. #endif
  41.  
  42. extern char *display;
  43.  
  44. extern long defaultScreenSaverTime;    /* for parsing command line */
  45. extern long defaultScreenSaverInterval;
  46. extern int defaultScreenSaverBlanking;
  47. extern int defaultBackingStore;
  48. extern Bool disableBackingStore;
  49. extern Bool disableSaveUnders;
  50. extern Bool PartialNetwork;
  51. #ifndef NOLOGOHACK
  52. extern int logoScreenSaver;
  53. #endif
  54. #ifdef RLIMIT_DATA
  55. extern int limitDataSpace;
  56. #endif
  57. #ifdef RLIMIT_STACK
  58. extern int limitStackSpace;
  59. #endif
  60. #ifdef RLIMIT_NOFILE
  61. extern int limitNoFile;
  62. #endif
  63. extern int defaultColorVisualClass;
  64. extern long ScreenSaverTime;        /* for forcing reset */
  65. extern Bool permitOldBugs;
  66. extern int monitorResolution;
  67. extern Bool defeatAccessControl;
  68.  
  69. Bool CoreDump;
  70.  
  71. void ddxUseMsg();
  72.  
  73. #ifndef SVR4
  74. extern char *sbrk();
  75. #endif
  76.  
  77. #ifdef AIXV3
  78. #define AIXFILE "/tmp/aixfile"
  79. FILE *aixfd;
  80. int SyncOn  = 0;
  81. extern int SelectWaitTime;
  82. #endif
  83.  
  84. #ifdef DEBUG
  85. #ifndef SPECIAL_MALLOC
  86. #define MEMBUG
  87. #endif
  88. #endif
  89.  
  90. #ifdef MEMBUG
  91. #define MEM_FAIL_SCALE 100000
  92. long Memory_fail = 0;
  93. static pointer minfree = NULL;
  94. static void CheckNode();
  95. #endif
  96.  
  97. Bool Must_have_memory = FALSE;
  98.  
  99. char *dev_tty_from_init = NULL;        /* since we need to parse it anyway */
  100.  
  101. /* Force connections to close on SIGHUP from init */
  102.  
  103. SIGVAL
  104. AutoResetServer ()
  105. {
  106.     dispatchException |= DE_RESET;
  107.     isItTimeToYield = TRUE;
  108. #ifdef GPROF
  109.     chdir ("/tmp");
  110.     exit (0);
  111. #endif
  112. #ifdef SYSV
  113.     signal (SIGHUP, AutoResetServer);
  114. #endif
  115. }
  116.  
  117. /* Force connections to close and then exit on SIGTERM, SIGINT */
  118.  
  119. SIGVAL
  120. GiveUp()
  121. {
  122.     dispatchException |= DE_TERMINATE;
  123.     isItTimeToYield = TRUE;
  124. }
  125.  
  126.  
  127. static void
  128. AbortServer()
  129. {
  130.     extern void AbortDDX();
  131.  
  132.     AbortDDX();
  133.     fflush(stderr);
  134.     if (CoreDump)
  135.     abort();
  136.     exit (1);
  137. }
  138.  
  139. void
  140. Error(str)
  141.     char *str;
  142. {
  143.     perror(str);
  144. }
  145.  
  146. #if defined (UTEK) || defined (UTEKV) || defined(sgi)
  147. /*
  148.  * Tektronix has a shared-memory time value which doesn't
  149.  * match gettimeofday at all, but it is only accessible
  150.  * inside the driver.  SGI has their own GetTimeInMillis.
  151.  */
  152. #else
  153. long
  154. GetTimeInMillis()
  155. {
  156.     struct timeval  tp;
  157.  
  158.     gettimeofday(&tp, 0);
  159.     return(tp.tv_sec * 1000) + (tp.tv_usec / 1000);
  160. }
  161. #endif
  162.  
  163. AdjustWaitForDelay (waitTime, newdelay)
  164.     pointer        waitTime;
  165.     unsigned long   newdelay;
  166. {
  167.     static struct timeval   delay_val;
  168.     struct timeval        **wt = (struct timeval **) waitTime;
  169.     unsigned long        olddelay;
  170.  
  171.     if (*wt == NULL)
  172.     {
  173.     delay_val.tv_sec = newdelay / 1000;
  174.     delay_val.tv_usec = 1000 * (newdelay % 1000);
  175.     *wt = &delay_val;
  176.     }
  177.     else
  178.     {
  179.     olddelay = (*wt)->tv_sec * 1000 + (*wt)->tv_usec / 1000;
  180.     if (newdelay < olddelay)
  181.     {
  182.         (*wt)->tv_sec = newdelay / 1000;
  183.         (*wt)->tv_usec = 1000 * (newdelay % 1000);
  184.     }
  185.     }
  186. }
  187.  
  188. void UseMsg()
  189. {
  190. #if !defined(AIXrt) && !defined(AIX386)
  191.     ErrorF("use: X [:<display>] [option]\n");
  192.     ErrorF("-a #                   mouse acceleration (pixels)\n");
  193.     ErrorF("-ac                    disable access control restrictions\n");
  194. #ifdef MEMBUG
  195.     ErrorF("-alloc int             chance alloc should fail\n");
  196. #endif
  197.     ErrorF("-auth string           select authorization file\n");    
  198.     ErrorF("bc                     enable bug compatibility\n");
  199.     ErrorF("-bs                    disable any backing store support\n");
  200.     ErrorF("-c                     turns off key-click\n");
  201.     ErrorF("c #                    key-click volume (0-100)\n");
  202.     ErrorF("-cc int                default color visual class\n");
  203.     ErrorF("-co string             color database file\n");
  204.     ErrorF("-dpi int               screen resolution in dots per inch\n");
  205.     ErrorF("-f #                   bell base (0-100)\n");
  206.     ErrorF("-fc string             cursor font\n");
  207.     ErrorF("-fn string             default font name\n");
  208.     ErrorF("-fp string             default font path\n");
  209.     ErrorF("-help                  prints message with these options\n");
  210.     ErrorF("-I                     ignore all remaining arguments\n");
  211. #ifdef RLIMIT_DATA
  212.     ErrorF("-ld int                limit data space to N Kb\n");
  213. #endif
  214. #ifdef RLIMIT_NOFILE
  215.     ErrorF("-lf int                limit number of open files to N\n");
  216. #endif
  217. #ifdef RLIMIT_STACK
  218.     ErrorF("-ls int                limit stack space to N Kb\n");
  219. #endif
  220. #ifndef NOLOGOHACK
  221.     ErrorF("-logo                  enable logo in screen saver\n");
  222.     ErrorF("nologo                 disable logo in screen saver\n");
  223. #endif
  224.     ErrorF("-p #                   screen-saver pattern duration (minutes)\n");
  225.     ErrorF("-r                     turns off auto-repeat\n");
  226.     ErrorF("r                      turns on auto-repeat \n");
  227.     ErrorF("-s #                   screen-saver timeout (minutes)\n");
  228.     ErrorF("-su                    disable any save under support\n");
  229.     ErrorF("-t #                   mouse threshold (pixels)\n");
  230.     ErrorF("-to #                  connection time out\n");
  231.     ErrorF("ttyxx                  server started from init on /dev/ttyxx\n");
  232.     ErrorF("v                      video blanking for screen-saver\n");
  233.     ErrorF("-v                     screen-saver without video blanking\n");
  234.     ErrorF("-wm                    WhenMapped default backing-store\n");
  235.     ErrorF("-x string              loads named extension at init time \n");
  236. #ifdef XDMCP
  237.     XdmcpUseMsg();
  238. #endif
  239. #endif /* !AIXrt && ! AIX386 */
  240.     ddxUseMsg();
  241. }
  242.  
  243. /*
  244.  * This function parses the command line. Handles device-independent fields
  245.  * and allows ddx to handle additional fields.  It is not allowed to modify
  246.  * argc or any of the strings pointed to by argv.
  247.  */
  248. void
  249. ProcessCommandLine ( argc, argv )
  250. int    argc;
  251. char    *argv[];
  252.  
  253. {
  254.     int i, skip;
  255.  
  256. #ifdef MEMBUG
  257. #ifndef AIXV3
  258.     if (!minfree)
  259.     minfree = (pointer)sbrk(0);
  260. #else
  261.     /* segment 2 is user data space */
  262.     minfree = (pointer) 0x20000000;
  263. #endif
  264. #endif
  265.     defaultKeyboardControl.autoRepeat = TRUE;
  266.  
  267. #ifdef AIXV3
  268.     OpenDebug();
  269. #endif
  270.     for ( i = 1; i < argc; i++ )
  271.     {
  272.     /* call ddx first, so it can peek/override if it wants */
  273.         if(skip = ddxProcessArgument(argc, argv, i))
  274.     {
  275.         i += (skip - 1);
  276.     }
  277.     else if(argv[i][0] ==  ':')  
  278.     {
  279.         /* initialize display */
  280.         display = argv[i];
  281.         display++;
  282.     }
  283.     else if ( strcmp( argv[i], "-a") == 0)
  284.     {
  285.         if(++i < argc)
  286.             defaultPointerControl.num = atoi(argv[i]);
  287.         else
  288.         UseMsg();
  289.     }
  290.     else if ( strcmp( argv[i], "-ac") == 0)
  291.     {
  292.         defeatAccessControl = TRUE;
  293.     }
  294. #ifdef MEMBUG
  295.     else if ( strcmp( argv[i], "-alloc") == 0)
  296.     {
  297.         if(++i < argc)
  298.             Memory_fail = atoi(argv[i]);
  299.         else
  300.         UseMsg();
  301.     }
  302. #endif
  303.     else if ( strcmp( argv[i], "-auth") == 0)
  304.     {
  305.         if(++i < argc)
  306.             InitAuthorization (argv[i]);
  307.         else
  308.         UseMsg();
  309.     }
  310.     else if ( strcmp( argv[i], "bc") == 0)
  311.         permitOldBugs = TRUE;
  312.     else if ( strcmp( argv[i], "-bs") == 0)
  313.         disableBackingStore = TRUE;
  314.     else if ( strcmp( argv[i], "c") == 0)
  315.     {
  316.         if(++i < argc)
  317.             defaultKeyboardControl.click = atoi(argv[i]);
  318.         else
  319.         UseMsg();
  320.     }
  321.     else if ( strcmp( argv[i], "-c") == 0)
  322.     {
  323.         defaultKeyboardControl.click = 0;
  324.     }
  325.     else if ( strcmp( argv[i], "-cc") == 0)
  326.     {
  327.         if(++i < argc)
  328.             defaultColorVisualClass = atoi(argv[i]);
  329.         else
  330.         UseMsg();
  331.     }
  332.     else if ( strcmp( argv[i], "-co") == 0)
  333.     {
  334.         if(++i < argc)
  335.             rgbPath = argv[i];
  336.         else
  337.         UseMsg();
  338.     }
  339.     else if ( strcmp( argv[i], "-core") == 0)
  340.         CoreDump = TRUE;
  341.     else if ( strcmp( argv[i], "-dpi") == 0)
  342.     {
  343.         if(++i < argc)
  344.             monitorResolution = atoi(argv[i]);
  345.         else
  346.         UseMsg();
  347.     }
  348.     else if ( strcmp( argv[i], "-f") == 0)
  349.     {
  350.         if(++i < argc)
  351.             defaultKeyboardControl.bell = atoi(argv[i]);
  352.         else
  353.         UseMsg();
  354.     }
  355.     else if ( strcmp( argv[i], "-fc") == 0)
  356.     {
  357.         if(++i < argc)
  358.             defaultCursorFont = argv[i];
  359.         else
  360.         UseMsg();
  361.     }
  362.     else if ( strcmp( argv[i], "-fn") == 0)
  363.     {
  364.         if(++i < argc)
  365.             defaultTextFont = argv[i];
  366.         else
  367.         UseMsg();
  368.     }
  369.     else if ( strcmp( argv[i], "-fp") == 0)
  370.     {
  371.         if(++i < argc)
  372.             defaultFontPath = argv[i];
  373.         else
  374.         UseMsg();
  375.     }
  376.     else if ( strcmp( argv[i], "-help") == 0)
  377.     {
  378.         UseMsg();
  379.         exit(0);
  380.     }
  381. #ifdef RLIMIT_DATA
  382.     else if ( strcmp( argv[i], "-ld") == 0)
  383.     {
  384.         if(++i < argc)
  385.         {
  386.             limitDataSpace = atoi(argv[i]);
  387.         if (limitDataSpace > 0)
  388.             limitDataSpace *= 1024;
  389.         }
  390.         else
  391.         UseMsg();
  392.     }
  393. #endif
  394. #ifdef RLIMIT_NOFILE
  395.     else if ( strcmp( argv[i], "-lf") == 0)
  396.     {
  397.         if(++i < argc)
  398.             limitNoFile = atoi(argv[i]);
  399.         else
  400.         UseMsg();
  401.     }
  402. #endif
  403. #ifdef RLIMIT_STACK
  404.     else if ( strcmp( argv[i], "-ls") == 0)
  405.     {
  406.         if(++i < argc)
  407.         {
  408.             limitStackSpace = atoi(argv[i]);
  409.         if (limitStackSpace > 0)
  410.             limitStackSpace *= 1024;
  411.         }
  412.         else
  413.         UseMsg();
  414.     }
  415. #endif
  416. #ifndef NOLOGOHACK
  417.     else if ( strcmp( argv[i], "-logo") == 0)
  418.     {
  419.         logoScreenSaver = 1;
  420.     }
  421.     else if ( strcmp( argv[i], "nologo") == 0)
  422.     {
  423.         logoScreenSaver = 0;
  424.     }
  425. #endif
  426.     else if ( strcmp( argv[i], "-p") == 0)
  427.     {
  428.         if(++i < argc)
  429.             defaultScreenSaverInterval = ((long)atoi(argv[i])) *
  430.                          MILLI_PER_MIN;
  431.         else
  432.         UseMsg();
  433.     }
  434.     else if ( strcmp( argv[i], "-pn") == 0)
  435.         PartialNetwork = TRUE;
  436.     else if ( strcmp( argv[i], "r") == 0)
  437.         defaultKeyboardControl.autoRepeat = TRUE;
  438.     else if ( strcmp( argv[i], "-r") == 0)
  439.         defaultKeyboardControl.autoRepeat = FALSE;
  440.     else if ( strcmp( argv[i], "-s") == 0)
  441.     {
  442.         if(++i < argc)
  443.             defaultScreenSaverTime = ((long)atoi(argv[i])) * MILLI_PER_MIN;
  444.         else
  445.         UseMsg();
  446.     }
  447.     else if ( strcmp( argv[i], "-su") == 0)
  448.         disableSaveUnders = TRUE;
  449.     else if ( strcmp( argv[i], "-t") == 0)
  450.     {
  451.         if(++i < argc)
  452.             defaultPointerControl.threshold = atoi(argv[i]);
  453.         else
  454.         UseMsg();
  455.     }
  456.     else if ( strcmp( argv[i], "-to") == 0)
  457.     {
  458.         if(++i < argc)
  459.         TimeOutValue = ((long)atoi(argv[i])) * MILLI_PER_SECOND;
  460.         else
  461.         UseMsg();
  462.     }
  463.     else if ( strcmp( argv[i], "-terminate") == 0)
  464.     {
  465.         extern Bool terminateAtReset;
  466.         
  467.         terminateAtReset = TRUE;
  468.     }
  469.     else if ( strcmp( argv[i], "v") == 0)
  470.         defaultScreenSaverBlanking = PreferBlanking;
  471.     else if ( strcmp( argv[i], "-v") == 0)
  472.         defaultScreenSaverBlanking = DontPreferBlanking;
  473. #ifdef MEMBUG
  474.     else if ( strcmp ( argv[i], "validateMemory") == 0)
  475.     {
  476.         extern unsigned long MemoryValidate;
  477.         MemoryValidate = 1;
  478.     }
  479.     else if ( strcmp ( argv[i], "neverFreeMemory") == 0)
  480.     {
  481.         extern unsigned long MemoryNeverFree;
  482.         MemoryNeverFree = 1;
  483.     }
  484. #endif
  485.     else if ( strcmp( argv[i], "-wm") == 0)
  486.         defaultBackingStore = WhenMapped;
  487.     else if ( strcmp( argv[i], "-x") == 0)
  488.     {
  489.         if(++i >= argc)
  490.         UseMsg();
  491.         /* For U**x, which doesn't support dynamic loading, there's nothing
  492.          * to do when we see a -x.  Either the extension is linked in or
  493.          * it isn't */
  494.     }
  495.     else if ( strcmp( argv[i], "-I") == 0)
  496.     {
  497.         /* ignore all remaining arguments */
  498.         break;
  499.     }
  500.     else if (strncmp (argv[i], "tty", 3) == 0)
  501.     {
  502.         /* just in case any body is interested */
  503.         dev_tty_from_init = argv[i];
  504.     }
  505. #ifdef XDMCP
  506.     else if ((skip = XdmcpOptions(argc, argv, i)) != i)
  507.     {
  508.         i = skip - 1;
  509.     }
  510. #endif
  511. #ifdef AIXV3
  512.         else if ( strcmp( argv[i], "-timeout") == 0)
  513.         {
  514.             if(++i < argc)
  515.                 SelectWaitTime = atoi(argv[i]);
  516.             else
  517.                 UseMsg();
  518.         }
  519.         else if ( strcmp( argv[i], "-sync") == 0)
  520.         {
  521.             SyncOn++;
  522.         }
  523. #endif
  524.      else
  525.      {
  526.         UseMsg();
  527.         exit (1);
  528.         }
  529.     }
  530. }
  531.  
  532. #ifndef SPECIAL_MALLOC
  533.  
  534. #ifdef MEMBUG
  535. #define FIRSTMAGIC 0x11aaaa11
  536. #define SECONDMAGIC 0x22aaaa22
  537. #define FREEDMAGIC  0x33aaaa33
  538. #define BLANKMAGIC  0x44aaaa44
  539. #define ALLOCMAGIC  0x55aaaa55
  540.  
  541. typedef struct _MallocHeader    *MallocHeaderPtr;
  542.  
  543. typedef struct _MallocHeader {
  544.     unsigned long    amount;
  545.     unsigned long    time;
  546.     MallocHeaderPtr    prev;
  547.     MallocHeaderPtr    next;
  548.     unsigned long    magic;
  549. } MallocHeaderRec;
  550.  
  551. typedef struct _MallocTrailer {
  552.     unsigned long    magic;
  553. } MallocTrailerRec, *MallocTrailerPtr;
  554.  
  555. unsigned long    MemoryAllocTime;
  556. unsigned long    MemoryAllocBreakpoint = ~0;
  557. unsigned long    MemoryFreeBreakpoint = ~0;
  558. unsigned long    MemoryActive = 0;
  559. unsigned long    MemoryValidate;
  560. unsigned long    MemoryNeverFree;
  561.  
  562. MallocHeaderPtr    MemoryInUse;
  563. MallocHeaderPtr    MemoryFreed;
  564.  
  565. #define request(amount)    ((amount) + sizeof (MallocHeaderRec) + sizeof (MallocTrailerRec))
  566. #define Header(ptr)    ((MallocHeaderPtr) (((char *) ptr) - sizeof (MallocHeaderRec)))
  567. #define Trailer(ptr)    ((MallocTrailerPtr) (((char *) ptr) + Header(ptr)->amount))
  568.  
  569. static unsigned long *
  570. SetupBlock(ptr, amount)
  571.     unsigned long   *ptr;
  572. {
  573.     MallocHeaderPtr    head = (MallocHeaderPtr) ptr;
  574.     MallocTrailerPtr    tail = (MallocTrailerPtr) (((char *) ptr) + amount + sizeof (MallocHeaderRec));
  575.  
  576.     MemoryActive += amount;
  577.     head->magic = FIRSTMAGIC;
  578.     head->amount = amount;
  579.     if (MemoryAllocTime == MemoryAllocBreakpoint)
  580.     head->amount = amount;
  581.     head->time = MemoryAllocTime++;
  582.     head->next = MemoryInUse;
  583.     head->prev = 0;
  584.     if (MemoryInUse)
  585.     MemoryInUse->prev = head;
  586.     MemoryInUse = head;
  587.  
  588.     tail->magic = SECONDMAGIC;
  589.     
  590.     return (unsigned long *)(((char *) ptr) + sizeof (MallocHeaderRec));
  591. }
  592.  
  593. ValidateAllActiveMemory ()
  594. {
  595.     MallocHeaderPtr    head;
  596.     MallocTrailerPtr    tail;
  597.  
  598.     for (head = MemoryInUse; head; head = head->next)
  599.     {
  600.     tail = (MallocTrailerPtr) (((char *) (head + 1)) + head->amount);
  601.         if (head->magic == FREEDMAGIC)
  602.         FatalError("Free data on active list");
  603.         if(head->magic != FIRSTMAGIC || tail->magic != SECONDMAGIC)
  604.         FatalError("Garbage object on active list");
  605.     }
  606.     for (head = MemoryFreed; head; head = head->next)
  607.     {
  608.     tail = (MallocTrailerPtr) (((char *) (head + 1)) + head->amount);
  609.     if (head->magic != FREEDMAGIC || tail->magic != FREEDMAGIC)
  610.         FatalError("Non free data on free list");
  611.     if (!CheckMemoryContents (head, BLANKMAGIC))
  612.         FatalError("Freed data reused");
  613.     }
  614. }
  615.  
  616. FillMemoryContents (head, value)
  617.     MallocHeaderPtr head;
  618.     long        value;
  619. {
  620.     int            count;
  621.     long        *store;
  622.  
  623.     count = head->amount / sizeof (long);
  624.     store = (long *) (head + 1);
  625.     while (count--)
  626.     *store++ = value;
  627. }
  628.  
  629. CheckMemoryContents (head, value)
  630.     MallocHeaderPtr head;
  631.     long        value;
  632. {
  633.     int            count;
  634.     long        *check;
  635.  
  636.     count = head->amount / sizeof (long);
  637.     check = (long *) (head + 1);
  638.     while (count--)
  639.     if (*check++ != value)
  640.         return FALSE;
  641.     return TRUE;
  642. }
  643.  
  644. #endif
  645.  
  646. /* XALLOC -- X's internal memory allocator.  Why does it return unsigned
  647.  * int * instead of the more common char *?  Well, if you read K&R you'll
  648.  * see they say that alloc must return a pointer "suitable for conversion"
  649.  * to whatever type you really want.  In a full-blown generic allocator
  650.  * there's no way to solve the alignment problems without potentially
  651.  * wasting lots of space.  But we have a more limited problem. We know
  652.  * we're only ever returning pointers to structures which will have to
  653.  * be long word aligned.  So we are making a stronger guarantee.  It might
  654.  * have made sense to make Xalloc return char * to conform with people's
  655.  * expectations of malloc, but this makes lint happier.
  656.  */
  657.  
  658. unsigned long * 
  659. Xalloc (amount)
  660.     unsigned long amount;
  661. {
  662.     char        *malloc();
  663.     register pointer  ptr;
  664.     
  665.     if ((long)amount <= 0)
  666.     return (unsigned long *)NULL;
  667.     /* aligned extra on long word boundary */
  668.     amount = (amount + 3) & ~3;
  669. #ifdef MEMBUG
  670.     if (MemoryValidate)
  671.     ValidateAllActiveMemory ();
  672.     if (!Must_have_memory && Memory_fail &&
  673.     ((random() % MEM_FAIL_SCALE) < Memory_fail))
  674.     return (unsigned long *)NULL;
  675.     if (ptr = (pointer)malloc(request(amount)))
  676.     {
  677.     unsigned long    *ret;
  678.     ret = SetupBlock (ptr, amount);
  679.     FillMemoryContents ((MallocHeaderPtr) ptr, ALLOCMAGIC);
  680.     return ret;
  681.     }
  682. #else
  683.     if (ptr = (pointer)malloc(amount))
  684.     return (unsigned long *)ptr;
  685. #endif
  686.     if (Must_have_memory)
  687.     FatalError("Out of memory");
  688.     return (unsigned long *)NULL;
  689. }
  690.  
  691. /*****************
  692.  * Xcalloc
  693.  *****************/
  694.  
  695. unsigned long *
  696. Xcalloc (amount)
  697.     unsigned long   amount;
  698. {
  699.     unsigned long   *ret;
  700.  
  701.     ret = Xalloc (amount);
  702.     if (ret)
  703.     bzero ((char *) ret, (int) amount);
  704.     return ret;
  705. }
  706.  
  707. /*****************
  708.  * Xrealloc
  709.  *****************/
  710.  
  711. unsigned long *
  712. Xrealloc (ptr, amount)
  713.     register pointer ptr;
  714.     unsigned long amount;
  715. {
  716.     char *malloc();
  717.     char *realloc();
  718.  
  719. #ifdef MEMBUG
  720.     if (ptr)
  721.     {
  722.         if (MemoryValidate)
  723.         ValidateAllActiveMemory ();
  724.         if ((long)amount <= 0)
  725.         {
  726.         if (!amount)
  727.             Xfree(ptr);
  728.         return (unsigned long *)NULL;
  729.         }
  730.         if (!Must_have_memory && Memory_fail &&
  731.         ((random() % MEM_FAIL_SCALE) < Memory_fail))
  732.         return (unsigned long *)NULL;
  733.         amount = (amount + 3) & ~3;
  734.     CheckNode(ptr);
  735.     ptr = (pointer)realloc((char *) Header (ptr), request(amount));
  736.     if (ptr)
  737.         return SetupBlock (ptr, amount);
  738.     }
  739.     else
  740.     {
  741.     return Xalloc (amount);
  742.     }
  743. #else
  744.     if ((long)amount <= 0)
  745.     {
  746.     if (ptr && !amount)
  747.         free(ptr);
  748.     return (unsigned long *)NULL;
  749.     }
  750.     amount = (amount + 3) & ~3;
  751.     if (ptr)
  752.         ptr = (pointer)realloc((char *)ptr, amount);
  753.     else
  754.     ptr = (pointer)malloc(amount);
  755.     if (ptr)
  756.         return (unsigned long *)ptr;
  757. #endif
  758.     if (Must_have_memory)
  759.     FatalError("Out of memory");
  760.     return (unsigned long *)NULL;
  761. }
  762.                     
  763. /*****************
  764.  *  Xfree
  765.  *    calls free 
  766.  *****************/    
  767.  
  768. void
  769. Xfree(ptr)
  770.     register pointer ptr;
  771. {
  772. #ifdef MEMBUG
  773.     if (MemoryValidate)
  774.     ValidateAllActiveMemory ();
  775.     if (ptr)
  776.     {
  777.     MallocHeaderPtr        head;
  778.     MallocTrailerPtr    trail;
  779.  
  780.     CheckNode(ptr);
  781.     head = Header(ptr);
  782.     trail = Trailer(ptr);
  783.     if (head->time == MemoryFreeBreakpoint)
  784.         head->magic = FIRSTMAGIC;
  785.     head->magic = FREEDMAGIC;
  786.     trail->magic = FREEDMAGIC;
  787.     FillMemoryContents (head, BLANKMAGIC);
  788.     if (MemoryNeverFree)
  789.     {
  790.         head->prev = 0;
  791.         head->next = MemoryFreed;
  792.         MemoryFreed = head;
  793.     }
  794.     else
  795.         free ((char *) head);
  796.     }
  797. #else
  798.     if (ptr)
  799.     free((char *)ptr); 
  800. #endif
  801. }
  802.  
  803. #ifdef MEMBUG
  804. static void
  805. CheckNode(ptr)
  806.     pointer ptr;
  807. {
  808.     MallocHeaderPtr    head;
  809.     MallocHeaderPtr    f, prev;
  810.  
  811.     if (ptr < minfree)
  812.     FatalError("Trying to free static storage");
  813.     head = Header(ptr);
  814.     if (((pointer) head) < minfree)
  815.     FatalError("Trying to free static storage");
  816.     if (head->magic == FREEDMAGIC)
  817.     FatalError("Freeing something already freed");
  818.     if(head->magic != FIRSTMAGIC || Trailer(ptr)->magic != SECONDMAGIC)
  819.     FatalError("Freeing a garbage object");
  820.     if(head->prev)
  821.     head->prev->next = head->next;
  822.     else
  823.     MemoryInUse = head->next;
  824.     if (head->next)
  825.     head->next->prev = head->prev;
  826.     MemoryActive -= head->amount;
  827. }
  828.  
  829. DumpMemoryInUse (time)
  830.     unsigned long   time;
  831. {
  832.     MallocHeaderPtr    head;
  833.  
  834.     for (head = MemoryInUse; head; head = head->next)
  835.     if (head->time >= time)
  836.         fprintf (stderr, "0x%08x %5d %6d\n", head,
  837.                     head->amount,
  838.                     head->time);
  839. }
  840.  
  841. static unsigned long    MarkedTime;
  842.  
  843. MarkMemoryTime ()
  844. {
  845.     MarkedTime = MemoryAllocTime;
  846. }
  847.  
  848. DumpMemorySince ()
  849. {
  850.     DumpMemoryInUse (MarkedTime);
  851. }
  852. #endif
  853. #endif /* SPECIAL_MALLOC */
  854.  
  855. /*VARARGS1*/
  856. void
  857. FatalError(f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9) /* limit of ten args */
  858.     char *f;
  859.     char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;
  860. {
  861.     ErrorF("\nFatal server error:\n");
  862.     ErrorF(f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);
  863.     ErrorF("\n");
  864.     AbortServer();
  865.     /*NOTREACHED*/
  866. }
  867.  
  868. /*VARARGS1*/
  869. void
  870. ErrorF( f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9) /* limit of ten args */
  871.     char *f;
  872.     char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;
  873. {
  874. #ifdef AIXV3
  875.     fprintf(aixfd, f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);
  876.     fflush (aixfd);
  877.  
  878.     if (SyncOn)
  879.         sync();
  880. #else
  881.     fprintf( stderr, f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);
  882. #endif
  883. }
  884.  
  885. #ifdef AIXV3
  886. OpenDebug()
  887. {
  888.         if((aixfd = fopen(AIXFILE,"w")) == NULL )
  889.         {
  890.                 fprintf(stderr,"open aixfile failed\n");
  891.                 exit(-1);
  892.         }
  893.         chmod(AIXFILE,00777);
  894. }
  895. #endif
  896.