DEVEL15-linux-warning-reduction-20090318
[openafs.git] / src / afs / afs_osi_alloc.c
index 539bc8b..2c8a5d7 100644 (file)
@@ -19,7 +19,7 @@ RCSID
 #include "afsincludes.h"       /* Afs-based standard headers */
 #include "afs/afs_stats.h"     /* afs statistics */
 
-#ifndef AFS_FBSD_ENV
+
 
 #ifdef AFS_AIX41_ENV
 #include "sys/lockl.h"
@@ -38,6 +38,93 @@ static struct osi_packet {
 } *freePacketList = NULL, *freeSmallList;
 afs_lock_t osi_flplock;
 
+static char memZero;           /* address of 0 bytes for kmem_alloc */
+
+struct osimem {
+    struct osimem *next;
+};
+
+#if !defined(AFS_OBSD44_ENV)
+void *
+afs_osi_Alloc(size_t x)
+{
+#if !defined(AFS_LINUX20_ENV) && !defined(AFS_FBSD_ENV)
+    register struct osimem *tm = NULL;
+    register int size;
+#endif
+
+    AFS_STATCNT(osi_Alloc);
+    /* 0-length allocs may return NULL ptr from AFS_KALLOC, so we special-case
+     * things so that NULL returned iff an error occurred */
+    if (x == 0)
+       return &memZero;
+
+    AFS_STATS(afs_stats_cmperf.OutStandingAllocs++);
+    AFS_STATS(afs_stats_cmperf.OutStandingMemUsage += x);
+#ifdef AFS_LINUX20_ENV
+    return osi_linux_alloc(x, 1);
+#elif defined(AFS_FBSD_ENV)
+    return osi_fbsd_alloc(x, 1);
+#else
+    size = x;
+    tm = (struct osimem *)AFS_KALLOC(size);
+#ifdef AFS_SUN5_ENV
+    if (!tm)
+       osi_Panic("osi_Alloc: Couldn't allocate %d bytes; out of memory!\n",
+                 size);
+#endif
+    return (void *)tm;
+#endif
+}
+
+#if    defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
+
+void *
+afs_osi_Alloc_NoSleep(size_t x)
+{
+    register struct osimem *tm;
+    register int size;
+
+    AFS_STATCNT(osi_Alloc);
+    /* 0-length allocs may return NULL ptr from AFS_KALLOC, so we special-case
+     * things so that NULL returned iff an error occurred */
+    if (x == 0)
+       return &memZero;
+
+    size = x;
+    AFS_STATS(afs_stats_cmperf.OutStandingAllocs++);
+    AFS_STATS(afs_stats_cmperf.OutStandingMemUsage += x);
+    tm = (struct osimem *)AFS_KALLOC_NOSLEEP(size);
+    return (void *)tm;
+}
+
+#endif /* SUN || SGI */
+
+void
+afs_osi_Free(void *x, size_t asize)
+{
+    AFS_STATCNT(osi_Free);
+    if (x == &memZero)
+       return;                 /* check for putting memZero back */
+
+    AFS_STATS(afs_stats_cmperf.OutStandingAllocs--);
+    AFS_STATS(afs_stats_cmperf.OutStandingMemUsage -= asize);
+#if defined(AFS_LINUX20_ENV)
+    osi_linux_free(x);
+#elif defined(AFS_FBSD_ENV)
+    osi_fbsd_free(x);
+#else
+    AFS_KFREE((struct osimem *)x, asize);
+#endif
+}
+
+void
+afs_osi_FreeStr(char *x)
+{
+    afs_osi_Free(x, strlen(x) + 1);
+}
+
+#endif
 
 /* free space allocated by AllocLargeSpace.  Also called by mclput when freeing
  * a packet allocated by osi_NetReceive. */
@@ -81,7 +168,7 @@ osi_AllocLargeSpace(size_t size)
 
     AFS_STATCNT(osi_AllocLargeSpace);
     if (size > AFS_LRALLOCSIZ)
-       osi_Panic("osi_AllocLargeSpace: size=%d\n", size);
+       osi_Panic("osi_AllocLargeSpace: size=%d\n", (int)size);
     afs_stats_cmperf.LargeBlocksActive++;
     if (!freePacketList) {
        char *p;
@@ -115,12 +202,16 @@ osi_AllocSmallSpace(size_t size)
 
     AFS_STATCNT(osi_AllocSmallSpace);
     if (size > AFS_SMALLOCSIZ)
-       osi_Panic("osi_AllocSmallS: size=%d\n", size);
+       osi_Panic("osi_AllocSmallS: size=%d\n", (int)size);
 
     if (!freeSmallList) {
        afs_stats_cmperf.SmallBlocksAlloced++;
        afs_stats_cmperf.SmallBlocksActive++;
-       return afs_osi_Alloc(AFS_SMALLOCSIZ);
+       tp = afs_osi_Alloc(AFS_SMALLOCSIZ);
+#ifdef KERNEL_HAVE_PIN
+        pin((char *)tp, AFS_SMALLOCSIZ);
+#endif
+        return (char *)tp;
     }
     afs_stats_cmperf.SmallBlocksActive++;
     MObtainWriteLock(&osi_fsplock, 327);
@@ -136,8 +227,6 @@ osi_AllocSmallSpace(size_t size)
 void
 shutdown_osinet(void)
 {
-    extern int afs_cold_shutdown;
-
     AFS_STATCNT(shutdown_osinet);
     if (afs_cold_shutdown) {
        struct osi_packet *tp;
@@ -160,5 +249,12 @@ shutdown_osinet(void)
        LOCK_INIT(&osi_fsplock, "osi_fsplock");
        LOCK_INIT(&osi_flplock, "osi_flplock");
     }
+    if (afs_stats_cmperf.LargeBlocksActive || 
+       afs_stats_cmperf.SmallBlocksActive) 
+    {
+       afs_warn("WARNING: not all blocks freed: large %d small %d\n", 
+                afs_stats_cmperf.LargeBlocksActive, 
+                afs_stats_cmperf.SmallBlocksActive);
+    }
 }
-#endif
+