home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / msj / msjv3_3 / dllcode / chatmem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-02  |  4.9 KB  |  176 lines

  1. /********************************************************************
  2. *    CHATMEM.C  - Memory allocation routines for shared DLL memory
  3. *
  4. *    (C) 1988, By Ross M. Greenberg for Microsoft Systems Journal
  5. *
  6. *    This module conatins three functions. Allocation of memory,
  7. *    de-allocation of memory and the getseg call.
  8. *
  9. *    The current ANSI calloc/alloc/malloc sequence does not allow for
  10. *    an additional parameter to specify if memory requested through
  11. *    these functions is to be sharable or private. Therefore the MSC
  12. *    library calls all allocate private memory.
  13. *
  14. *    These routines allocate a 64K chunk of memory, requested from OS/2
  15. *    as a sharable chunk, then dole it out using the DosSub allocation
  16. *    calls.
  17. *
  18. *    Only one 64K chunk is allocated: if more memory is desired an
  19. *    additional call to dosallocseg would have to be made and all 
  20. *    sessions already logged in given access to the chunk.  Out of 
  21. *    laziness, that was not done for these demonsttration routines.
  22. *
  23. *
  24. *    Compile with:
  25. *
  26. *    cl /AL /Au /Gs /c chatmem.c
  27. *
  28. *    Note -    Though broken here, the following two lines are entered
  29. *            as one line:
  30. *
  31. *    link startup+chatlib+chatmem,chatlib.dll,,llibcdll+doscalls,
  32. *    chatlib/NOE
  33. *
  34. *    Remember to move the DLL itself into your DLL library directory
  35. *
  36. ********************************************************************/
  37.  
  38. #include    <stdio.h>
  39. #include    <stdlib.h>
  40. #include    <malloc.h>
  41. #include    <dos.h>
  42.  
  43. #define    TRUE    1
  44. #define    FALSE    0
  45.  
  46. /* The following OS/2 system calls are made in this module: */
  47.  
  48. extern far pascal dosallocseg();
  49. extern far pascal dosfreeseg();
  50. extern far pascal dossuballoc();
  51. extern far pascal dossubset();
  52. extern far pascal dossubfree();
  53. extern far pascal dosgetseg();
  54. extern far pascal dossemrequest();
  55. extern far pascal dossemclear();
  56.  
  57.  
  58. #define    NULLP    (char *)NULL
  59.  
  60.  
  61. /*    This semaphore is so that we don't hurt ourselves as we allocate
  62.  *    and deallocate memory
  63.  */
  64.  
  65. long    memory_semaphore = NULL;
  66.  
  67. /*    This is the actual selector which the 64K dosallocseg() 
  68.  * call returns
  69.  */
  70.  
  71. unsigned major_selector = NULL;
  72.  
  73. /********************************************************************
  74. *    my_calloc(number_of_items,size_of_item)
  75. *
  76. *    Emulates the more typical calloc call, but returns memory which
  77. *    can later be allocated as sharable.
  78. *
  79. *    After the first call (which causes a 64K chunk to be allocated),
  80. *    all subsequent calls cause a dossuballoc call to be made, and
  81. *    a long pointer to the returned memory to be created and returned
  82. *
  83. *    The 64K chunk must be initialized by the dossubset call before it
  84. *    can be used by other dossub functions.
  85. *
  86. *    Because the dossubfree call requires a size, the size requested
  87. *    plus the sizeof an int is actually allocated and the size of the
  88. *    total request is then stored in the first two bytes of the 
  89. *    returned character array.  The ptr returned, however, is this 
  90. *    memory location plus the initial sizeof and int -- therefore the 
  91. *    bookkeeping is transparent to the application task.
  92. ********************************************************************/
  93.  
  94. char * 
  95. my_calloc(size1, size2)
  96. int    size1;
  97. int    size2;
  98. {
  99. unsigned    long    selector;
  100. int    stat;
  101. char    *ptr;
  102. int    sizeit = (size1 * size2) + sizeof(int);
  103.  
  104.     dossemrequest(&memory_semaphore, -1L);
  105.     if    (!major_selector)
  106.     {
  107.         if    (stat = dosallocseg(0, &major_selector, 3))
  108.         {
  109.             printf("dosalloc error:%d\n", stat);
  110.             dossemclear(&memory_semaphore);
  111.             return(NULLP);
  112.         }
  113.  
  114.         if    (stat = dossubset(major_selector, 1, 0))
  115.         {
  116.             printf("Error in dossubset:%d\n", stat);
  117.             dossemclear(&memory_semaphore);
  118.             return(NULLP);
  119.         }
  120.     }
  121.  
  122.     selector = 0;
  123.     if    (stat = dossuballoc(major_selector, &selector, sizeit))
  124.     {
  125.         printf("dossuballoc error:%d\n", stat);
  126.         dossemclear(&memory_semaphore);
  127.         return(NULLP);
  128.     }
  129.  
  130.     dossemclear(&memory_semaphore);
  131.     ptr = (char *)(((long)major_selector << 16) + (long)selector);
  132.     memset(ptr, (char)NULL, sizeit);
  133.     *(int *)ptr = sizeit;
  134.     return(ptr + sizeof(int));
  135. }
  136.  
  137. /********************************************************************
  138. *    my_free(pointer_to_a_character_array_previously_my_calloc'ed)
  139. *
  140. *    Subtract sizeof an int from the pointer, dereference as an
  141. *    int, then free that number of bytes.
  142. *
  143. ********************************************************************/
  144.  
  145. my_free(ptr)
  146. char    *ptr;
  147. {
  148. int    stat;
  149.  
  150.     ptr -= sizeof(int);
  151.  
  152.     dossemrequest(&memory_semaphore, -1L);
  153.     if (stat = dossubfree(major_selector, FP_OFF(ptr), *(int *)ptr))
  154.     {
  155.         printf("Error freeing: %lx\n", ptr);
  156.         exit(1);
  157.     }
  158.     dossemclear(&memory_semaphore);
  159. }
  160.  
  161. /********************************************************************
  162. *    my_getseg()
  163. *
  164. *    Causes the memory affilaited with the major_selector to become
  165. *    accessable to this process.
  166. ********************************************************************/
  167.  
  168. my_getseg()
  169. {
  170. int    stat;
  171.  
  172.     if    (stat=dosgetseg(major_selector))
  173.         printf("Error on getseg:%d\n", stat);
  174.     return(stat);
  175. }
  176.