home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 330_02 / tskalloc.c < prev    next >
C/C++ Source or Header  |  1990-10-12  |  4KB  |  191 lines

  1. /*
  2.    --- Version 2.0 90-10-12 10:33 ---
  3.  
  4.    TSKALLOC.C - CTask - Dynamic memory allocation interface
  5.  
  6.    Public Domain Software written by
  7.       Thomas Wagner
  8.       Ferrari electronic Gmbh
  9.       Beusselstrasse 27
  10.       D-1000 Berlin 21
  11.       Germany
  12.  
  13.    This file is new with Version 1.1
  14.  
  15.    This module contains the memory allocation functions that are needed
  16.    if TSK_DYNAMIC is defined.
  17.  
  18.    Version 2.1 adds the tsk_calloc and tsk_realloc functions, and
  19.    provides an option to compile direct malloc replacements. This
  20.    option may be used if the C-runtime library functions have been
  21.    renamed, so that all external functions using the allocation
  22.    routines arrive here. If you have the runtime library source code,
  23.    it is relatively painless to change the names. Without the source,
  24.    you will have to resort to a binary editor to patch the entry names
  25.    directly.
  26. */
  27.  
  28. #include "tsk.h"
  29.  
  30. #if (TSK_TURBO)
  31. #include <alloc.h>
  32. #else
  33. #include <malloc.h>
  34. #endif
  35.  
  36. /*
  37.    Define MALLOC_REPLACED TRUE if you replaced the C run-time library
  38.    entries for malloc/free/realloc with UPPERCASE names, as suggested 
  39.    in the manual.
  40. */
  41.  
  42. #define MALLOC_REPLACED    FALSE
  43.  
  44. /*
  45.    You can replace the following definitions to use different
  46.    allocation routines if desired.
  47. */
  48.  
  49. #if (!MALLOC_REPLACED)
  50.  
  51. #define xalloc    malloc
  52. #define xcalloc   calloc
  53. #define xrealloc  realloc
  54. #define xfree     free
  55.  
  56. #else
  57.  
  58. extern void *MALLOC (size_t size);
  59. extern void *REALLOC (void *buffer, size_t size);
  60. extern void *FREE (void *buffer);
  61.  
  62. #define xalloc    MALLOC
  63. #define xcalloc   calloc
  64. #define xrealloc  REALLOC
  65. #define xfree     FREE
  66.  
  67. #endif
  68.  
  69. resource Neardata alloc_resource;
  70.  
  71.  
  72. #if (!MALLOC_REPLACED)
  73.  
  74.  
  75. farptr Globalfunc tsk_alloc (word size)
  76. {
  77.    farptr ptr;
  78.  
  79.    request_resource (&alloc_resource, 0L);
  80.    ptr = (farptr)xalloc (size);
  81.    release_resource (&alloc_resource);
  82.  
  83.    return ptr;
  84. }
  85.  
  86.  
  87. farptr Globalfunc tsk_calloc (word item, word size)
  88. {
  89.    farptr ptr;
  90.  
  91.    request_resource (&alloc_resource, 0L);
  92.    ptr = (farptr)xcalloc (item, size);
  93.    release_resource (&alloc_resource);
  94.  
  95.    return ptr;
  96. }
  97.  
  98.  
  99. farptr Globalfunc tsk_free (farptr item)
  100. {
  101.    request_resource (&alloc_resource, 0L);
  102.    xfree ((void *)item);   /* Ignore warning in small model */
  103.    release_resource (&alloc_resource);
  104.    return LNULL;
  105. }
  106.  
  107.  
  108. farptr Globalfunc tsk_realloc (farptr item, word size)
  109. {
  110.    farptr ptr;
  111.  
  112.    request_resource (&alloc_resource, 0L);
  113.    ptr = (farptr)xrealloc ((void *)item, size);   /* Ignore warning in small model */
  114.    release_resource (&alloc_resource);
  115.    return ptr;
  116. }
  117.  
  118.  
  119. #else
  120.  
  121. void *malloc (size_t size)
  122. {
  123.    void *area;
  124.  
  125.    if (ctask_active)
  126.       request_cresource (&alloc_resource, 0L);
  127.  
  128.    area = xmalloc (size);
  129.                                  
  130.    if (ctask_active)
  131.       release_resource (&alloc_resource);
  132.  
  133.    return area;
  134. }
  135.  
  136.  
  137. void *realloc (void *item, size_t size)
  138. {
  139.    void *area;
  140.  
  141.    if (ctask_active)
  142.       request_cresource (&alloc_resource, 0L);
  143.  
  144.    area = xrealloc (item,size);
  145.                                  
  146.    if (ctask_active)
  147.       release_resource (&alloc_resource);
  148.  
  149.    return area;
  150. }
  151.  
  152.  
  153. void free (void *area)
  154. {
  155.    if (ctask_active)
  156.       request_cresource (&alloc_resource, 0L);
  157.  
  158.    xfree (area);
  159.  
  160.    if (ctask_active)
  161.       release_resource (&alloc_resource);
  162. }
  163.  
  164.  
  165. farptr Globalfunc tsk_alloc (word size)
  166. {
  167.    return (farptr)malloc (size);
  168. }
  169.  
  170.  
  171. farptr Globalfunc tsk_calloc (farptr item, word size)
  172. {
  173.    return (farptr)calloc (item, size);
  174. }
  175.  
  176.  
  177. farptr Globalfunc tsk_free (farptr item)
  178. {
  179.    free (item);
  180.    return LNULL;
  181. }
  182.  
  183.  
  184. farptr Globalfunc tsk_realloc (farptr item, word size)
  185. {
  186.    return (farptr)realloc (item, size);
  187. }
  188.  
  189. #endif
  190.  
  191.