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