home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR2 / CBUFF09.ZIP / SRC.ZIP / ALLOC.C next >
C/C++ Source or Header  |  1993-11-16  |  4KB  |  140 lines

  1. /*  $Id$
  2.  *  
  3.  *  File    alloc.c
  4.  *  Part of    ChessBase utilities file format (CBUFF)
  5.  *  Author    Anjo Anjewierden, anjo@swi.psy.uva.nl
  6.  *  Purpose    Memory allocation
  7.  *  Works with    GNU CC 2.4.5
  8.  *        Borland C++ 3.1
  9.  *  
  10.  *  Notice    Copyright (c) 1993  Anjo Anjewierden
  11.  *  
  12.  *  History    18/10/93  (Created)
  13.  *          03/11/93  (Last modified)
  14.  */ 
  15.  
  16.  
  17. #include "cbuff.h"
  18.  
  19.  
  20. /*----------------------------------------------
  21.  *  Introduction
  22.  *----------------------------------------------
  23.  *
  24.  *  Memory allocation routines.  This uses the standard C library
  25.  *  functions unless __BORLANDC__ is defined.  The functions in
  26.  *  this file are broken if size_t != unsigned long.
  27.  *
  28.  *  Note to developers:  Please forward the appropriate code for
  29.  *  other compilers that have size_t != unsigned long.
  30.  */
  31.  
  32.  
  33. /*----------------------------------------------
  34.  *  Definitions
  35.  *----------------------------------------------*/
  36.  
  37. #ifdef __BORLANDC__
  38. #include <alloc.h>
  39. void *    farmalloc(unsigned long);
  40. void    farfree(void *);
  41. #endif
  42.  
  43.  
  44. /*----------------------------------------------
  45.  *  Public Functions
  46.  *----------------------------------------------*/
  47. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  48. @node alloc
  49. @deftypefun {void *} alloc (unsigned long @var{n})
  50. Allocates @var{n} bytes of memory and returns a pointer to the first byte.
  51. @end deftypefun
  52. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  53.  
  54. #ifdef __BORLANDC__
  55. void *
  56. alloc(unsigned long n)
  57. { return farmalloc(n);
  58. }
  59. #else
  60. void *
  61. alloc(unsigned long n)
  62. { return malloc(n);
  63. }
  64. #endif
  65.  
  66. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  67. @node unalloc
  68. @deftypefun void unalloc (void *@var{p})
  69. Returns the memory previously allocated with @code{alloc}.
  70. @end deftypefun
  71. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  72.  
  73. #ifdef __BORLANDC__
  74. void
  75. unalloc(void *p)
  76. { farfree(p);
  77. }
  78. #else
  79. void
  80. unalloc(void *p)
  81. { free(p);
  82. }
  83. #endif
  84.  
  85.  
  86. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  87. @node allocCharp
  88. @deftypefun {char *} allocCharp (char *@var{s})
  89. Allocates memory for the @code{char *} contained in @var{s}, and then
  90. copies the contents of @var{s} into the allocated memory.  A pointer to
  91. the first byte of @var{s} is returned.  Note that the length of the
  92. resulting @var{s} should not be modified in order for
  93. @code{unallocCharp} to work.
  94. @end deftypefun
  95. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  96.  
  97. char *
  98. allocCharp(char *s)
  99. { char *t;
  100.  
  101.   t = alloc(strlen(s)+1);
  102.   strcpy(t, s);
  103.   return t;
  104. }
  105.  
  106.  
  107. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  108. @node unallocCharp
  109. @deftypefun void unallocCharp (char *@var{s})
  110. Reclaims the memory of @var{s}, which was previously allocated by
  111. @code{allocCharp}.
  112. @end deftypefun
  113. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  114.  
  115. void
  116. unallocCharp(char *s)
  117. { unalloc(s);
  118. }
  119.  
  120.  
  121. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  122. @node printMemoryAvailable
  123. @deftypefun void printMemoryAvailable (FILE *@var{fd})
  124. Prints a message on the given @var{fd} which tells the user how much
  125. (internal) memory is still available.  This is useful on machines with
  126. little memory (e.g. DOS).  If the environment does not support limited
  127. memory (e.g. Unix) nothing is printed.
  128. @end deftypefun
  129. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  130.  
  131. void
  132. printMemoryAvailable(FILE *fd)
  133. {
  134. #ifdef __BORLANDC__
  135.   unsigned long farcoreleft(void);
  136.  
  137.   fprintf(fd, "Memory still available %lu bytes\n", farcoreleft());
  138. #endif
  139. }
  140.