Annotation of mstools/samples/rpc/data/xmit/xmitu.c, revision 1.1.1.1

1.1       root        1: /****************************************************************************
                      2:                          Microsoft RPC Version 1.0
                      3:                        Copyright Microsoft Corp. 1992
                      4:                               xmit Example
                      5: 
                      6:     FILE:      xmitu.c
                      7: 
                      8:     PURPOSE:   Utility functions used by both client and server
                      9:                sides of the RPC distributed application.
                     10:                This sample demonstrates the transmit_as example.
                     11:                A doubly-linked list is transmitted over the network
                     12:                as a sized array.
                     13: 
                     14:     RELATED:   xmits.c - server main
                     15:                xmitp.c - remote procedures
                     16:                xmitc.c - client main
                     17: 
                     18:     FUNCTIONS: DOUBLE_LINK_TYPE_to_xmit - convert list to array
                     19:                DOUBLE_LINK_TYPE_from_xmit - convert array to list
                     20:                DOUBLE_LINK_TYPE_free_inst - free linked list memory
                     21:                DOUBLE_LINK_TYPE_free_xmit - free array memory
                     22:                MIDL_user_allocate - user-supplied memory allocator
                     23:                MIDL_user_free - user-supplied routine to free memory
                     24: 
                     25:                ArrayWalkProc - utility to display the array
                     26:                ListWalkProc - utility to display the linked list
                     27:                InsertNewNode - utility to add a node to the list
                     28: 
                     29:     COMMENTS:  This sample program generates a linked list to
                     30:                demonstrate how a list with aliasing can be transmitted
                     31:                using the transmit_as attribute as a sized array.
                     32:                The pointers are rebuilt on the server side.
                     33: 
                     34: ****************************************************************************/
                     35: #include <stdio.h>
                     36: #include <string.h>
                     37: #include <stdlib.h>
                     38: #include <rpc.h>       // RPC API functions, types
                     39: #include "xmit.h"     // header file generated by MIDL compiler
                     40: 
                     41: void ArrayWalkProc(DOUBLE_XMIT_TYPE * pArray)
                     42: {
                     43:     int i;
                     44:     printf("Display contents of transmitted array:\n");
                     45: 
                     46:     for (i = 0; i < pArray->sSize; i++)
                     47:        printf("pArray->asNumber[%d] = %d\n", i, pArray->asNumber[i]);
                     48: }
                     49: 
                     50: void ListWalkProc(DOUBLE_LINK_TYPE * pList)
                     51: {
                     52:     printf("Display contents of doubly linked list:\n");
                     53:     while (pList != NULL) {
                     54:        printf("pList @0x%x = %d, Next = 0x%x\n", pList, pList->sNumber, pList->pNext);
                     55:        pList = pList->pNext;
                     56:     }
                     57: }
                     58: 
                     59: DOUBLE_LINK_TYPE * InsertNewNode(short sValue, DOUBLE_LINK_TYPE * pPrevious)
                     60: {
                     61: DOUBLE_LINK_TYPE * pNew;
                     62: 
                     63:     do
                     64:        pNew = (DOUBLE_LINK_TYPE *) MIDL_user_allocate(sizeof(DOUBLE_LINK_TYPE));
                     65:     while (pNew == pPrevious);
                     66: 
                     67:     pNew->pNext = NULL;             /* initialize */
                     68:     pNew->pPrevious = NULL;    /* initialize */
                     69:     pNew->sNumber = sValue;    /* insert b between a and c */
                     70: 
                     71:     pNew->pPrevious = pPrevious;            /* prev(b) = a */
                     72:     if (pPrevious != NULL) {
                     73:        pNew->pNext = pPrevious->pNext;      /* next(b) = c */
                     74:        pPrevious->pNext = pNew;             /* next(a) = b */
                     75:        if (pNew->pNext != NULL)
                     76:            (pNew->pNext)->pPrevious = pNew; /* prev(c) = b */
                     77:     }
                     78:     else
                     79:        pNew->pNext = NULL;
                     80:     return(pNew);
                     81: }
                     82: 
                     83: void * MIDL_user_allocate(size_t len)
                     84: {
                     85:     void * ptr;
                     86:     ptr = malloc(len);
                     87:     if (ptr == NULL)
                     88:        exit(2);  /* exit program */
                     89:     return(ptr);
                     90: }
                     91: 
                     92: void MIDL_user_free(void * ptr)
                     93: {
                     94:     if (ptr != NULL)
                     95:        free(ptr);
                     96: }
                     97: 
                     98: /* free the doubly linked list */
                     99: /* move forward through list, freeing the previous entry */
                    100: void DOUBLE_LINK_TYPE_free_inst (DOUBLE_LINK_TYPE * pList)
                    101: {
                    102:     if (pList == NULL)
                    103:        return;
                    104: 
                    105:     for (; pList->pNext != NULL; pList = pList->pNext)
                    106:        MIDL_user_free(pList->pPrevious);
                    107:     MIDL_user_free(pList);  /* free the last entry */
                    108: }
                    109: 
                    110: /* free the array structure */
                    111: void DOUBLE_LINK_TYPE_free_xmit (DOUBLE_XMIT_TYPE * pSizedArray)
                    112: {
                    113:     MIDL_user_free(pSizedArray);
                    114: }
                    115: 
                    116: /* convert from linked list to array */
                    117: void DOUBLE_LINK_TYPE_to_xmit   (DOUBLE_LINK_TYPE  *  pList,
                    118:                                  DOUBLE_XMIT_TYPE **  ppArray)
                    119: {
                    120:     short cCount, i;
                    121:     DOUBLE_LINK_TYPE * pHead = pList;  /* save pointer to start */
                    122:     DOUBLE_XMIT_TYPE * pArray;
                    123: 
                    124:     /* count the number of elements to allocate memory */
                    125:     for (cCount = 0; pList != NULL; pList = pList->pNext)
                    126:        cCount++;
                    127: 
                    128:     /* allocate the memory for the array */
                    129:     pArray = (DOUBLE_XMIT_TYPE *) MIDL_user_allocate(sizeof(DOUBLE_XMIT_TYPE) + (cCount * sizeof(short)));
                    130:     pArray->sSize = cCount;
                    131: 
                    132:     /* copy the linked list contents into the array */
                    133:     for (i = 0, pList = pHead; pList != NULL; pList = pList->pNext)
                    134:        pArray->asNumber[i++] = pList->sNumber;
                    135: 
                    136:     /* return the address of the pointer to the array */
                    137:     *ppArray = pArray;
                    138: }
                    139: 
                    140: /* convert from array to linked list */
                    141: /* in C, the value for pDblLinkedList cannot change during the call */
                    142: 
                    143: void DOUBLE_LINK_TYPE_from_xmit (DOUBLE_XMIT_TYPE * pArray,
                    144:                                 DOUBLE_LINK_TYPE * pDblLinkedList)
                    145: {
                    146:     DOUBLE_LINK_TYPE * pCurrent;
                    147:     int i;
                    148: 
                    149:     pCurrent = pDblLinkedList;  /* overwrite first element, delete others */
                    150:     pCurrent->sNumber = pArray->asNumber[0];  /* overwrite first element */
                    151:     DOUBLE_LINK_TYPE_free_inst(pCurrent->pNext); /* delete other elements */
                    152:     pCurrent->pNext = NULL;                  /* initialize */
                    153:     pCurrent->pPrevious = NULL;
                    154: 
                    155:     for (i = 1; i < pArray->sSize; i++) {      /* write new values */
                    156:        pCurrent = InsertNewNode(pArray->asNumber[i], pCurrent);
                    157:     }
                    158:     return;
                    159: }
                    160: 
                    161: /* end xmitu.c */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.