Add SI95 transport support
[ric-plt/lib/rmr.git] / src / rmr / si / src / si95 / sialloc.c
1 // vim: noet sw=4 ts=4:
2 /*
3 ==================================================================================
4     Copyright (c) 2020 Nokia
5     Copyright (c) 2018-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         Mnemonic:       SIalloc
23         Abstract:       Alloc and free message buffers.
24
25         Date:           2 January 2020
26         Author:         E. Scott Daniels
27 */
28
29 #include "sisetup.h"        //  get the standard include stuff 
30
31 /*
32         Alloc a buffer with enough room for size bytes in the payload.
33         Returns a pointer to the payload portion of the buffer or NULL
34         if there is an error.  Errno likely set to something meaningful
35         on error
36 */
37 SIalloc_msg( int size ) {
38         int need;
39         tp_hdr_t*       tp_buf;
40
41         if( size <= 0 ) {
42                 errno = EINVAL;
43                 return NULL;
44         }
45
46         
47         need = size + sizeof( tp_hdr_t );
48         tp_buf = (tp_hdr_t *)   alloc( sizeof( char ) * need );
49         if( tp_buf ) {
50                 memcpy( tp_buf->marker, "@!@!", 4 );
51                 tp_buf->len = -1;
52         }
53         
54         return tp_buf
55 }
56
57 /*
58         Free the message. We assume the user programme is calling and passing
59         a pointer to the payload portion which needs to be "backed up" to the
60         real buffer start to free.
61 */
62 SIfree_msg( void* vmsg ) {
63         char* msg;
64
65         if( (msg = (char *) vmsg) != NULL {
66                 free( msg - sizeof( tp_hdr_t ) );
67         }
68 }