aboutsummaryrefslogtreecommitdiffstats
path: root/stacks/lwip_stack/src/sbr/sbr_res_mgr.h
blob: 54729d4261dd258de0bc254179974fe8f9d9dbfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
*
* Copyright (c) 2018 Huawei Technologies Co.,Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef SBR_RES_MGR_H
#define SBR_RES_MGR_H
#include "sbr_protocol_api.h"
#include "sbr_index_ring.h"

#ifdef __cplusplus
/* *INDENT-OFF* */
extern "C" {
/* *INDENT-ON* */
#endif

typedef struct
{
  sbr_index_ring *sk_ring;
  sbr_socket_t sk[SBR_MAX_FD_NUM + 1];  /* unuse index 0 */
} sbr_res_group;

extern sbr_res_group g_res_group;

/*****************************************************************************
*   Prototype    : sbr_malloc_sk
*   Description  : malloc sock
*   Input        : None
*   Output       : None
*   Return Value : static inline sbr_socket_t *
*   Calls        :
*   Called By    :
*
*****************************************************************************/
static inline sbr_socket_t *
sbr_malloc_sk ()
{
  int fd;

  if (sbr_index_ring_dequeue (g_res_group.sk_ring, &fd) != 1)
    {
      NSSBR_LOGERR ("malloc sk failed]");
      sbr_set_errno (EMFILE);
      return NULL;
    }

  NSSBR_LOGDBG ("malloc sk ok]fd=%d", fd);
  return &g_res_group.sk[fd];
}

/*****************************************************************************
*   Prototype    : sbr_free_sk
*   Description  : free sock
*   Input        : sbr_socket_t * sk
*   Output       : None
*   Return Value : static inline void
*   Calls        :
*   Called By    :
*
*****************************************************************************/
static inline void
sbr_free_sk (sbr_socket_t * sk)
{
  sk->fdopt = NULL;
  sk->sk_obj = NULL;
  sk->stack_obj = NULL;

  if (sbr_index_ring_enqueue (g_res_group.sk_ring, sk->fd) != 1)
    {
      NSSBR_LOGERR ("sbr_index_ring_enqueue failed, this can not happen");
    }

  NSSBR_LOGDBG ("free sk ok]fd=%d", sk->fd);
}

/*****************************************************************************
*   Prototype    : sbr_lookup_sk
*   Description  : lookup socket
*   Input        : int fd
*   Output       : None
*   Return Value : static inline sbr_socket_t *
*   Calls        :
*   Called By    :
*
*****************************************************************************/
static inline sbr_socket_t *
sbr_lookup_sk (int fd)
{
  if ((fd < 1) || (fd > SBR_MAX_FD_NUM))
    {
      NSSBR_LOGERR ("fd is not ok]fd=%d", fd);
      sbr_set_errno (EBADF);
      return NULL;
    }

  sbr_socket_t *sk = &g_res_group.sk[fd];
  if (!sk->sk_obj || !sk->stack_obj)
    {
      NSSBR_LOGERR
        ("data in sk is error, this can not happen]fd=%d,sk_obj=%p,stack_obj=%p",
         fd, sk->sk_obj, sk->stack_obj);
      sbr_set_errno (EBADF);
      return NULL;
    }

  return sk;
}

int sbr_init_res ();

#ifdef __cplusplus
/* *INDENT-OFF* */
}
/* *INDENT-ON* */
#endif

#endif