BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file NCDBuf.c |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
30 | #include "NCDBuf.h" |
||
31 | |||
32 | #include <misc/balloc.h> |
||
33 | #include <misc/offset.h> |
||
34 | #include <misc/debug.h> |
||
35 | |||
36 | static void ref_target_func_release (BRefTarget *ref_target) |
||
37 | { |
||
38 | NCDBuf *o = UPPER_OBJECT(ref_target, NCDBuf, ref_target); |
||
39 | NCDBufStore *store = o->store; |
||
40 | |||
41 | if (store) { |
||
42 | LinkedList0_Remove(&store->used_bufs_list, &o->list_node); |
||
43 | LinkedList0_Prepend(&store->free_bufs_list, &o->list_node); |
||
44 | } else { |
||
45 | BFree(o); |
||
46 | } |
||
47 | } |
||
48 | |||
49 | void NCDBufStore_Init (NCDBufStore *o, size_t buf_size) |
||
50 | { |
||
51 | o->buf_size = buf_size; |
||
52 | LinkedList0_Init(&o->used_bufs_list); |
||
53 | LinkedList0_Init(&o->free_bufs_list); |
||
54 | |||
55 | DebugObject_Init(&o->d_obj); |
||
56 | } |
||
57 | |||
58 | void NCDBufStore_Free (NCDBufStore *o) |
||
59 | { |
||
60 | DebugObject_Free(&o->d_obj); |
||
61 | |||
62 | LinkedList0Node *ln; |
||
63 | |||
64 | ln = LinkedList0_GetFirst(&o->used_bufs_list); |
||
65 | while (ln) { |
||
66 | NCDBuf *buf = UPPER_OBJECT(ln, NCDBuf, list_node); |
||
67 | ASSERT(buf->store == o) |
||
68 | buf->store = NULL; |
||
69 | ln = LinkedList0Node_Next(ln); |
||
70 | } |
||
71 | |||
72 | ln = LinkedList0_GetFirst(&o->free_bufs_list); |
||
73 | while (ln) { |
||
74 | LinkedList0Node *next_ln = LinkedList0Node_Next(ln); |
||
75 | NCDBuf *buf = UPPER_OBJECT(ln, NCDBuf, list_node); |
||
76 | ASSERT(buf->store == o) |
||
77 | BFree(buf); |
||
78 | ln = next_ln; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | size_t NCDBufStore_BufSize (NCDBufStore *o) |
||
83 | { |
||
84 | DebugObject_Access(&o->d_obj); |
||
85 | |||
86 | return o->buf_size; |
||
87 | } |
||
88 | |||
89 | NCDBuf * NCDBufStore_GetBuf (NCDBufStore *o) |
||
90 | { |
||
91 | DebugObject_Access(&o->d_obj); |
||
92 | |||
93 | NCDBuf *buf; |
||
94 | |||
95 | LinkedList0Node *ln = LinkedList0_GetFirst(&o->free_bufs_list); |
||
96 | if (ln) { |
||
97 | buf = UPPER_OBJECT(ln, NCDBuf, list_node); |
||
98 | ASSERT(buf->store == o) |
||
99 | LinkedList0_Remove(&o->free_bufs_list, &buf->list_node); |
||
100 | } else { |
||
101 | bsize_t size = bsize_add(bsize_fromsize(sizeof(NCDBuf)), bsize_fromsize(o->buf_size)); |
||
102 | buf = BAllocSize(size); |
||
103 | if (!buf) { |
||
104 | return NULL; |
||
105 | } |
||
106 | buf->store = o; |
||
107 | } |
||
108 | |||
109 | LinkedList0_Prepend(&o->used_bufs_list, &buf->list_node); |
||
110 | BRefTarget_Init(&buf->ref_target, ref_target_func_release); |
||
111 | |||
112 | return buf; |
||
113 | } |
||
114 | |||
115 | BRefTarget * NCDBuf_RefTarget (NCDBuf *o) |
||
116 | { |
||
117 | return &o->ref_target; |
||
118 | } |
||
119 | |||
120 | char * NCDBuf_Data (NCDBuf *o) |
||
121 | { |
||
122 | return o->data; |
||
123 | } |