Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
3.4 KiB

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