Fix crash under SI95 with multiple receive threads
[ric-plt/lib/rmr.git] / src / rmr / si / src / si95 / sinew.c
1 // vim: noet sw=4 ts=4:
2 /*
3 ==================================================================================
4     Copyright (c) 2020 Nokia
5     Copyright (c) 2020 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20
21 /*
22 *******************************************************************************
23 *
24 *  Mnemonic: SInew
25 *  Abstract: This routine is responsible for alocating a new block based on
26 *            the block type and initializing it.
27 *  Parms:    type - Block id to create
28 *  Returns:  Pointer to the new block or NULL if not successful
29 *  Date:     26 March 1995
30 *  Author:   E. Scott Daniels
31 *  Mod:         22 Feb 2002 - To ensure new field in tp block is initialised
32 *
33 ******************************************************************************
34 */
35 #include "sisetup.h"
36
37 extern void *SInew( int type ) {
38         void *retptr;                  //  generic pointer for return
39         struct tp_blk *tpptr;          //  pointer at a new tp block
40         struct ginfo_blk *gptr;        //  pointer at gen info blk
41         struct ioq_blk *qptr;          //  pointer to an I/O queue block
42
43  switch( type ) {
44                 case IOQ_BLK:              //  make an I/O queue block
45                         if( (qptr = (struct ioq_blk *) malloc( sizeof( struct ioq_blk) )) != NULL ) {
46                                 qptr->addr = NULL;
47                                 qptr->next = NULL;
48                                 qptr->data = NULL;
49                                 qptr->dlen = 0;
50                         }
51                         retptr = (void *) qptr;    //  set pointer for return
52                         break;
53
54                 case TP_BLK:
55                         if( (tpptr = (struct tp_blk *) malloc( sizeof( struct tp_blk ) )) != NULL ) {
56                                 memset( tpptr, 0, sizeof( *tpptr ) );
57                                 tpptr->fd = -1;
58                                 tpptr->type = -1;
59                                 tpptr->flags = TPF_UNBIND;   //  default to unbind on termination
60                         }
61                         retptr = (void *) tpptr;   //  setup for later return
62                         break;
63
64                 case GI_BLK:                //  create global info block
65                         if( (gptr = (struct ginfo_blk *) malloc( sizeof( struct ginfo_blk ) )) != NULL ) {
66                                 memset( gptr, 0, sizeof( *gptr ) );
67
68                                 gptr->magicnum = MAGICNUM;   //  inidicates valid block
69                                 gptr->flags = 0;
70                                 gptr->tplist = NULL;
71                                 FD_ZERO( &gptr->readfds);      //  clear the fdsets
72                                 FD_ZERO( &gptr->writefds) ;
73                                 FD_ZERO( &gptr->execpfds );
74                                 gptr->rbuf = NULL;             //  no read buffer
75                                 gptr->cbtab = NULL;
76                                 gptr->rbuflen = 0;
77                         }
78
79                 retptr = (void *) gptr;    //  set up for return at end
80                 break;
81
82                 default:
83                         retptr = NULL;           //  bad type - just return null
84                 break;
85         }                          //  end switch
86
87         return( retptr );           //  send back the new pointer
88 }