shutdown-cleanup-20071213
[openafs.git] / src / afs / afs_osi_alloc.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13 RCSID
14     ("$Header$");
15
16
17
18 #include "afs/sysincludes.h"    /* Standard vendor system headers */
19 #include "afsincludes.h"        /* Afs-based standard headers */
20 #include "afs/afs_stats.h"      /* afs statistics */
21
22 #ifndef AFS_FBSD_ENV
23
24 #ifdef AFS_AIX41_ENV
25 #include "sys/lockl.h"
26 #include "sys/sleep.h"
27 #include "sys/syspest.h"
28 #include "sys/lock_def.h"
29 /*lock_t osi_fsplock = LOCK_AVAIL;*/
30 #endif
31
32 afs_lock_t osi_fsplock;
33
34
35
36 static struct osi_packet {
37     struct osi_packet *next;
38 } *freePacketList = NULL, *freeSmallList;
39 afs_lock_t osi_flplock;
40
41 static char memZero;            /* address of 0 bytes for kmem_alloc */
42
43 struct osimem {
44     struct osimem *next;
45 };
46
47
48 void *
49 afs_osi_Alloc(size_t x)
50 {
51 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_FBSD_ENV)
52     register struct osimem *tm = NULL;
53     register int size;
54 #endif
55
56     AFS_STATCNT(osi_Alloc);
57     /* 0-length allocs may return NULL ptr from AFS_KALLOC, so we special-case
58      * things so that NULL returned iff an error occurred */
59     if (x == 0)
60         return &memZero;
61
62     AFS_STATS(afs_stats_cmperf.OutStandingAllocs++);
63     AFS_STATS(afs_stats_cmperf.OutStandingMemUsage += x);
64 #ifdef AFS_LINUX20_ENV
65     return osi_linux_alloc(x, 1);
66 #elif defined(AFS_FBSD_ENV)
67     return osi_fbsd_alloc(x, 1);
68 #else
69     size = x;
70     tm = (struct osimem *)AFS_KALLOC(size);
71 #ifdef  AFS_SUN5_ENV
72     if (!tm)
73         osi_Panic("osi_Alloc: Couldn't allocate %d bytes; out of memory!\n",
74                   size);
75 #endif
76     return (void *)tm;
77 #endif
78 }
79
80 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
81
82 void *
83 afs_osi_Alloc_NoSleep(size_t x)
84 {
85     register struct osimem *tm;
86     register int size;
87
88     AFS_STATCNT(osi_Alloc);
89     /* 0-length allocs may return NULL ptr from AFS_KALLOC, so we special-case
90      * things so that NULL returned iff an error occurred */
91     if (x == 0)
92         return &memZero;
93
94     size = x;
95     AFS_STATS(afs_stats_cmperf.OutStandingAllocs++);
96     AFS_STATS(afs_stats_cmperf.OutStandingMemUsage += x);
97     tm = (struct osimem *)AFS_KALLOC_NOSLEEP(size);
98     return (void *)tm;
99 }
100
101 #endif /* SUN || SGI */
102
103 void
104 afs_osi_Free(void *x, size_t asize)
105 {
106     AFS_STATCNT(osi_Free);
107     if (x == &memZero)
108         return;                 /* check for putting memZero back */
109
110     AFS_STATS(afs_stats_cmperf.OutStandingAllocs--);
111     AFS_STATS(afs_stats_cmperf.OutStandingMemUsage -= asize);
112 #if defined(AFS_LINUX20_ENV)
113     osi_linux_free(x);
114 #elif defined(AFS_FBSD_ENV)
115     osi_fbsd_free(x);
116 #else
117     AFS_KFREE((struct osimem *)x, asize);
118 #endif
119 }
120
121 void
122 afs_osi_FreeStr(char *x)
123 {
124     afs_osi_Free(x, strlen(x) + 1);
125 }
126
127
128
129 /* free space allocated by AllocLargeSpace.  Also called by mclput when freeing
130  * a packet allocated by osi_NetReceive. */
131
132 void
133 osi_FreeLargeSpace(void *adata)
134 {
135
136     AFS_ASSERT_GLOCK();
137
138     AFS_STATCNT(osi_FreeLargeSpace);
139     afs_stats_cmperf.LargeBlocksActive--;
140     MObtainWriteLock(&osi_flplock, 322);
141     ((struct osi_packet *)adata)->next = freePacketList;
142     freePacketList = adata;
143     MReleaseWriteLock(&osi_flplock);
144 }
145
146 void
147 osi_FreeSmallSpace(void *adata)
148 {
149
150     AFS_ASSERT_GLOCK();
151
152     AFS_STATCNT(osi_FreeSmallSpace);
153     afs_stats_cmperf.SmallBlocksActive--;
154     MObtainWriteLock(&osi_fsplock, 323);
155     ((struct osi_packet *)adata)->next = freeSmallList;
156     freeSmallList = adata;
157     MReleaseWriteLock(&osi_fsplock);
158 }
159
160
161 /* allocate space for sender */
162 void *
163 osi_AllocLargeSpace(size_t size)
164 {
165     register struct osi_packet *tp;
166
167     AFS_ASSERT_GLOCK();
168
169     AFS_STATCNT(osi_AllocLargeSpace);
170     if (size > AFS_LRALLOCSIZ)
171         osi_Panic("osi_AllocLargeSpace: size=%d\n", size);
172     afs_stats_cmperf.LargeBlocksActive++;
173     if (!freePacketList) {
174         char *p;
175
176         afs_stats_cmperf.LargeBlocksAlloced++;
177         p = (char *)afs_osi_Alloc(AFS_LRALLOCSIZ);
178 #ifdef  KERNEL_HAVE_PIN
179         /*
180          * Need to pin this memory since under heavy conditions this memory
181          * could be swapped out; the problem is that we could inside rx where
182          * interrupts are disabled and thus we would panic if we don't pin it.
183          */
184         pin(p, AFS_LRALLOCSIZ);
185 #endif
186         return p;
187     }
188     MObtainWriteLock(&osi_flplock, 324);
189     tp = freePacketList;
190     if (tp)
191         freePacketList = tp->next;
192     MReleaseWriteLock(&osi_flplock);
193     return (char *)tp;
194 }
195
196
197 /* allocate space for sender */
198 void *
199 osi_AllocSmallSpace(size_t size)
200 {
201     register struct osi_packet *tp;
202
203     AFS_STATCNT(osi_AllocSmallSpace);
204     if (size > AFS_SMALLOCSIZ)
205         osi_Panic("osi_AllocSmallS: size=%d\n", size);
206
207     if (!freeSmallList) {
208         afs_stats_cmperf.SmallBlocksAlloced++;
209         afs_stats_cmperf.SmallBlocksActive++;
210         tp = afs_osi_Alloc(AFS_SMALLOCSIZ);
211 #ifdef KERNEL_HAVE_PIN
212         pin((char *)tp, AFS_SMALLOCSIZ);
213 #endif
214         return (char *)tp;
215     }
216     afs_stats_cmperf.SmallBlocksActive++;
217     MObtainWriteLock(&osi_fsplock, 327);
218     tp = freeSmallList;
219     if (tp)
220         freeSmallList = tp->next;
221     MReleaseWriteLock(&osi_fsplock);
222     return (char *)tp;
223 }
224
225
226
227 void
228 shutdown_osinet(void)
229 {
230     AFS_STATCNT(shutdown_osinet);
231     if (afs_cold_shutdown) {
232         struct osi_packet *tp;
233
234         while ((tp = freePacketList)) {
235             freePacketList = tp->next;
236 #ifdef  KERNEL_HAVE_PIN
237             unpin(tp, AFS_LRALLOCSIZ);
238 #endif
239             afs_osi_Free(tp, AFS_LRALLOCSIZ);
240         }
241
242         while ((tp = freeSmallList)) {
243             freeSmallList = tp->next;
244 #ifdef  KERNEL_HAVE_PIN
245             unpin(tp, AFS_SMALLOCSIZ);
246 #endif
247             afs_osi_Free(tp, AFS_SMALLOCSIZ);
248         }
249         LOCK_INIT(&osi_fsplock, "osi_fsplock");
250         LOCK_INIT(&osi_flplock, "osi_flplock");
251     }
252     if (afs_stats_cmperf.LargeBlocksActive || 
253         afs_stats_cmperf.SmallBlocksActive) 
254     {
255         afs_warn("WARNING: not all blocks freed: large %d small %d\n", 
256                  afs_stats_cmperf.LargeBlocksActive, 
257                  afs_stats_cmperf.SmallBlocksActive);
258     }
259 }
260 #endif