home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / PAX20.ZIP / MEM.C < prev    next >
C/C++ Source or Header  |  1990-11-12  |  3KB  |  141 lines

  1. /* $Source: /u/mark/src/pax/RCS/mem.c,v $
  2.  *
  3.  * $Revision: 2.0.0.3 $
  4.  *
  5.  * mem.c - memory allocation and manipulation functions
  6.  *
  7.  * DESCRIPTION
  8.  *
  9.  *    These routines are provided for higher level handling of the UNIX
  10.  *    memory allocation functions.
  11.  *
  12.  * AUTHOR
  13.  *
  14.  *     Mark H. Colburn, Open Systems Architects, Inc. (mark@minnetech.mn.org)
  15.  *
  16.  *
  17.  * COPYRIGHT
  18.  *
  19.  *    Copyright (c) 1989 Mark H. Colburn.  All rights reserved.
  20.  *
  21.  *    Redistribution and use in source and binary forms are permitted
  22.  *    provided that the above copyright notice and this paragraph are
  23.  *    duplicated in all such forms and that any documentation,
  24.  *    advertising materials, and other materials related to such
  25.  *    distribution and use acknowledge that the software was developed
  26.  *    by Mark H. Colburn.
  27.  *
  28.  *    THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  29.  *    IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  30.  *    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  31.  *
  32.  * $Log:    mem.c,v $
  33.  * Revision 2.0.0.3  89/10/13  02:35:17  mark
  34.  * Beta Test Freeze
  35.  *
  36.  */
  37.  
  38. #ifndef lint
  39. static char        *ident = "$Id: mem.c,v 2.0.0.3 89/10/13 02:35:17 mark Exp Locker: mark $";
  40. static char        *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
  41. #endif /* ! lint */
  42.  
  43.  
  44. /* Headers */
  45.  
  46. #include "pax.h"
  47.  
  48.  
  49. /* mem_get - allocate memory
  50.  *
  51.  * DESCRIPTION
  52.  *
  53.  *    Mem_get attempts to allocate a block of memory using the malloc
  54.  *    function call.  In the event that the memory is not available,
  55.  *    mem_get will display an "Out of memory" message for the user
  56.  *    the first time it encounters the an out of memory situation.
  57.  *    Subsequent calls to mem_get may fail, but no message will be
  58.  *    printed.
  59.  *
  60.  * PARAMETERS
  61.  *
  62.  *    uint len    - The amount of memory to allocate
  63.  *
  64.  * RETURNS
  65.  *
  66.  *    Normally returns the pointer to the newly allocated memory.  If
  67.  *    an error occurs, NULL is returned, and an error message is
  68.  *    printed.
  69.  *
  70.  * ERRORS
  71.  *
  72.  *    ENOMEM    No memory is available
  73.  */
  74.  
  75. #ifdef __STDC__
  76.  
  77. char *
  78. mem_get(uint len)
  79.  
  80. #else
  81.  
  82. char *
  83. mem_get(len)
  84.     uint                len;    /* amount of memory to get */
  85.  
  86. #endif
  87. {
  88.     char               *mem;
  89.     static short        outofmem = 0;
  90.  
  91.     DBUG_ENTER("mem_get");
  92.     if ((mem = (char *) malloc(len)) == (char *) NULL && !outofmem) {
  93.     outofmem++;
  94.     warn("mem_get()", "Out of memory");
  95.     }
  96.     DBUG_RETURN(mem);
  97. }
  98.  
  99.  
  100. /* mem_str - duplicate a string into dynamic memory
  101.  *
  102.  * DESCRIPTION
  103.  *
  104.  *    Mem_str attempts to make a copy of string.  It allocates space for
  105.  *    the string, and if the allocation was successfull, copies the old
  106.  *    string into the newly allocated space.
  107.  *
  108.  * PARAMETERS
  109.  *
  110.  *    char *str     - string to make a copy of
  111.  *
  112.  * RETURNS
  113.  *
  114.  *    Normally returns a pointer to a new string at least as large
  115.  *    as strlen(str) + 1, which contains a copy of the the data
  116.  *    passed in str, plus a null terminator.  Returns (char *)NULL
  117.  *    if enough memory to make a copy of str is not available.
  118.  */
  119.  
  120. #ifdef __STDC__
  121.  
  122. char *
  123. mem_str(char *str)
  124.  
  125. #else
  126.  
  127. char *
  128. mem_str(str)
  129.     char               *str;    /* string to make a copy of */
  130.  
  131. #endif
  132. {
  133.     char               *mem;    /* temporary pointer */
  134.  
  135.     DBUG_ENTER("mem_str");
  136.     if (mem = mem_get((uint) strlen(str) + 1)) {
  137.     strcpy(mem, str);
  138.     }
  139.     DBUG_RETURN(mem);
  140. }
  141.