converted to UNIX format.
authorGunter Knauf <gk@gknw.de>
Thu, 10 Jun 2004 21:20:15 +0000 (21:20 +0000)
committerGunter Knauf <gk@gknw.de>
Thu, 10 Jun 2004 21:20:15 +0000 (21:20 +0000)
lib/nwlib.c

index 2dd0f4f..b0eea56 100644 (file)
-/***************************************************************************\r
- *                                  _   _ ____  _\r
- *  Project                     ___| | | |  _ \| |\r
- *                             / __| | | | |_) | |\r
- *                            | (__| |_| |  _ <| |___\r
- *                             \___|\___/|_| \_\_____|\r
- *\r
- * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.\r
- *\r
- * This software is licensed as described in the file COPYING, which\r
- * you should have received as part of this distribution. The terms\r
- * are also available at http://curl.haxx.se/docs/copyright.html.\r
- *\r
- * You may opt to use, copy, modify, merge, publish, distribute and/or sell\r
- * copies of the Software, and permit persons to whom the Software is\r
- * furnished to do so, under the terms of the COPYING file.\r
- *\r
- * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY\r
- * KIND, either express or implied.\r
- *\r
- * $Id$\r
- ***************************************************************************/\r
-\r
-#include <errno.h>\r
-#include <string.h>\r
-#include <stdlib.h>\r
-#include <library.h>\r
-#include <netware.h>\r
-#include <screen.h>\r
-#include <nks/thread.h>\r
-#include <nks/synch.h>\r
-\r
-#include "memory.h"\r
-#include "memdebug.h"\r
-\r
-typedef struct\r
-{\r
-  int     _errno;\r
-  void    *twentybytes;\r
-} libthreaddata_t;\r
-\r
-typedef struct\r
-{\r
-  int         x;\r
-  int         y;\r
-  int         z;\r
-  void        *tenbytes;\r
-  NXKey_t     perthreadkey;   /* if -1, no key obtained... */\r
-  NXMutex_t   *lock;\r
-} libdata_t;\r
-\r
-int         gLibId      = -1;\r
-void        *gLibHandle = (void *) NULL;\r
-rtag_t      gAllocTag   = (rtag_t) NULL;\r
-NXMutex_t   *gLibLock   = (NXMutex_t *) NULL;\r
-\r
-/* internal library function prototypes... */\r
-int     DisposeLibraryData ( void * );\r
-void    DisposeThreadData ( void * );\r
-int     GetOrSetUpData ( int id, libdata_t **data, libthreaddata_t **threaddata );\r
-\r
-\r
-int _NonAppStart( void        *NLMHandle,\r
-                  void        *errorScreen,\r
-                  const char  *cmdLine,\r
-                  const char  *loadDirPath,\r
-                  size_t      uninitializedDataLength,\r
-                  void        *NLMFileHandle,\r
-                  int         (*readRoutineP)( int conn,\r
-                                               void *fileHandle, size_t offset,\r
-                                               size_t nbytes,\r
-                                               size_t *bytesRead,\r
-                                               void *buffer ),\r
-                  size_t      customDataOffset,\r
-                  size_t      customDataSize,\r
-                  int         messageCount,\r
-                  const char  **messages )\r
-{\r
-  NX_LOCK_INFO_ALLOC(liblock, "Per-Application Data Lock", 0);\r
-  \r
-#ifndef __GNUC__\r
-#pragma unused(cmdLine)\r
-#pragma unused(loadDirPath)\r
-#pragma unused(uninitializedDataLength)\r
-#pragma unused(NLMFileHandle)\r
-#pragma unused(readRoutineP)\r
-#pragma unused(customDataOffset)\r
-#pragma unused(customDataSize)\r
-#pragma unused(messageCount)\r
-#pragma unused(messages)\r
-#endif\r
-\r
-/*\r
-** Here we process our command line, post errors (to the error screen),\r
-** perform initializations and anything else we need to do before being able\r
-** to accept calls into us. If we succeed, we return non-zero and the NetWare\r
-** Loader will leave us up, otherwise we fail to load and get dumped.\r
-*/\r
-  gAllocTag = AllocateResourceTag(NLMHandle,\r
-                                  "<library-name> memory allocations",\r
-                                  AllocSignature);\r
-\r
-  if (!gAllocTag) {\r
-    OutputToScreen(errorScreen, "Unable to allocate resource tag for "\r
-                   "library memory allocations.\n");\r
-    return -1;\r
-  }\r
-\r
-  gLibId = register_library(DisposeLibraryData);\r
-\r
-  if (gLibId < -1) {\r
-    OutputToScreen(errorScreen, "Unable to register library with kernel.\n");\r
-    return -1;\r
-  }\r
-\r
-  gLibHandle = NLMHandle;\r
-\r
-  gLibLock = NXMutexAlloc(0, 0, &liblock);\r
-  \r
-  if (!gLibLock) {\r
-    OutputToScreen(errorScreen, "Unable to allocate library data lock.\n");\r
-    return -1;\r
-  }\r
-\r
-  return 0;\r
-}\r
-\r
-/*\r
-** Here we clean up any resources we allocated. Resource tags is a big part\r
-** of what we created, but NetWare doesn't ask us to free those.\r
-*/\r
-void _NonAppStop( void )\r
-{\r
-  (void) unregister_library(gLibId);\r
-  NXMutexFree(gLibLock);\r
-}\r
-\r
-/*\r
-** This function cannot be the first in the file for if the file is linked\r
-** first, then the check-unload function's offset will be nlmname.nlm+0\r
-** which is how to tell that there isn't one. When the check function is\r
-** first in the linked objects, it is ambiguous. For this reason, we will\r
-** put it inside this file after the stop function.\r
-**\r
-** Here we check to see if it's alright to ourselves to be unloaded. If not,\r
-** we return a non-zero value. Right now, there isn't any reason not to allow\r
-** it.\r
-*/\r
-int  _NonAppCheckUnload( void )\r
-{\r
-    return 0;\r
-}\r
-\r
-int GetOrSetUpData(int id, libdata_t **appData,\r
-                   libthreaddata_t **threadData )\r
-{\r
-  int                 err;\r
-  libdata_t           *app_data;\r
-  libthreaddata_t *thread_data;\r
-  NXKey_t             key;\r
-  NX_LOCK_INFO_ALLOC(liblock, "Application Data Lock", 0);\r
-\r
-  err         = 0;\r
-  thread_data = (libthreaddata_t *) NULL;\r
-\r
-/*\r
-** Attempt to get our data for the application calling us. This is where we\r
-** store whatever application-specific information we need to carry in support\r
-** of calling applications.\r
-*/\r
-  app_data = (libdata_t *) get_app_data(id);\r
-\r
-  if (!app_data) {\r
-/*\r
-** This application hasn't called us before; set up application AND per-thread\r
-** data. Of course, just in case a thread from this same application is calling\r
-** us simultaneously, we better lock our application data-creation mutex. We\r
-** also need to recheck for data after we acquire the lock because WE might be\r
-** that other thread that was too late to create the data and the first thread\r
-** in will have created it.\r
-*/\r
-    NXLock(gLibLock);\r
-\r
-    if (!(app_data = (libdata_t *) get_app_data(id))) {\r
-      app_data = (libdata_t *) malloc(sizeof(libdata_t));\r
-\r
-      if (app_data) {\r
-        memset(app_data, 0, sizeof(libdata_t));\r
-        \r
-        app_data->tenbytes = malloc(10);\r
-        app_data->lock     = NXMutexAlloc(0, 0, &liblock);\r
-        \r
-        if (!app_data->tenbytes || !app_data->lock) {\r
-          if (app_data->lock)\r
-            NXMutexFree(app_data->lock);\r
-          \r
-          free(app_data);\r
-          app_data = (libdata_t *) NULL;\r
-          err      = ENOMEM;\r
-        }\r
-        \r
-        if (app_data) {\r
-/*\r
-** Here we burn in the application data that we were trying to get by calling\r
-** get_app_data(). Next time we call the first function, we'll get this data\r
-** we're just now setting. We also go on here to establish the per-thread data\r
-** for the calling thread, something we'll have to do on each application\r
-** thread the first time it calls us.\r
-*/\r
-          err = set_app_data(gLibId, app_data);\r
-          \r
-          if (err) {\r
-            free(app_data);\r
-            app_data = (libdata_t *) NULL;\r
-            err      = ENOMEM;\r
-          }\r
-          else {\r
-            /* create key for thread-specific data... */\r
-            err = NXKeyCreate(DisposeThreadData, (void *) NULL, &key);\r
-            \r
-            if (err)                /* (no more keys left?) */\r
-              key = -1;\r
-            \r
-            app_data->perthreadkey = key;\r
-          }\r
-        }\r
-      }\r
-    }\r
-    \r
-    NXUnlock(gLibLock);\r
-  }\r
-\r
-  if (app_data) {\r
-    key = app_data->perthreadkey;\r
-    \r
-    if (key != -1 /* couldn't create a key? no thread data */\r
-        && !(err = NXKeyGetValue(key, (void **) &thread_data))\r
-        && !thread_data) {\r
-/*\r
-** Allocate the per-thread data for the calling thread. Regardless of whether\r
-** there was already application data or not, this may be the first call by a\r
-** a new thread. The fact that we allocation 20 bytes on a pointer is not very\r
-** important, this just helps to demonstrate that we can have arbitrarily\r
-** complex per-thread data.\r
-*/\r
-      thread_data = (libthreaddata_t *) malloc(sizeof(libthreaddata_t));\r
-      \r
-      if (thread_data) {\r
-        thread_data->_errno      = 0;\r
-        thread_data->twentybytes = malloc(20);\r
-          \r
-        if (!thread_data->twentybytes) {\r
-          free(thread_data);\r
-          thread_data = (libthreaddata_t *) NULL;\r
-          err         = ENOMEM;\r
-        }\r
-        \r
-        if ((err = NXKeySetValue(key, thread_data))) {\r
-          free(thread_data->twentybytes);\r
-          free(thread_data);\r
-          thread_data = (libthreaddata_t *) NULL;\r
-        }\r
-      }\r
-    }\r
-  }\r
-\r
-  if (appData)\r
-    *appData = app_data;\r
-\r
-  if (threadData)\r
-    *threadData = thread_data;\r
-\r
-  return err;\r
-}\r
-\r
-int DisposeLibraryData( void    *data)\r
-{\r
-  if (data) {\r
-    void    *tenbytes = ((libdata_t *) data)->tenbytes;\r
-    \r
-    if (tenbytes)\r
-      free(tenbytes);\r
-    \r
-    free(data);\r
-  }\r
-\r
-  return 0;\r
-}\r
-\r
-void DisposeThreadData(void    *data)\r
-{\r
-  if (data) {\r
-    void    *twentybytes = ((libthreaddata_t *) data)->twentybytes;\r
-    \r
-    if (twentybytes)\r
-      free(twentybytes);\r
-    \r
-    free(data);\r
-  }\r
-}\r
+/***************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ * $Id$
+ ***************************************************************************/
+
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <library.h>
+#include <netware.h>
+#include <screen.h>
+#include <nks/thread.h>
+#include <nks/synch.h>
+
+#include "memory.h"
+#include "memdebug.h"
+
+typedef struct
+{
+  int     _errno;
+  void    *twentybytes;
+} libthreaddata_t;
+
+typedef struct
+{
+  int         x;
+  int         y;
+  int         z;
+  void        *tenbytes;
+  NXKey_t     perthreadkey;   /* if -1, no key obtained... */
+  NXMutex_t   *lock;
+} libdata_t;
+
+int         gLibId      = -1;
+void        *gLibHandle = (void *) NULL;
+rtag_t      gAllocTag   = (rtag_t) NULL;
+NXMutex_t   *gLibLock   = (NXMutex_t *) NULL;
+
+/* internal library function prototypes... */
+int     DisposeLibraryData ( void * );
+void    DisposeThreadData ( void * );
+int     GetOrSetUpData ( int id, libdata_t **data, libthreaddata_t **threaddata );
+
+
+int _NonAppStart( void        *NLMHandle,
+                  void        *errorScreen,
+                  const char  *cmdLine,
+                  const char  *loadDirPath,
+                  size_t      uninitializedDataLength,
+                  void        *NLMFileHandle,
+                  int         (*readRoutineP)( int conn,
+                                               void *fileHandle, size_t offset,
+                                               size_t nbytes,
+                                               size_t *bytesRead,
+                                               void *buffer ),
+                  size_t      customDataOffset,
+                  size_t      customDataSize,
+                  int         messageCount,
+                  const char  **messages )
+{
+  NX_LOCK_INFO_ALLOC(liblock, "Per-Application Data Lock", 0);
+  
+#ifndef __GNUC__
+#pragma unused(cmdLine)
+#pragma unused(loadDirPath)
+#pragma unused(uninitializedDataLength)
+#pragma unused(NLMFileHandle)
+#pragma unused(readRoutineP)
+#pragma unused(customDataOffset)
+#pragma unused(customDataSize)
+#pragma unused(messageCount)
+#pragma unused(messages)
+#endif
+
+/*
+** Here we process our command line, post errors (to the error screen),
+** perform initializations and anything else we need to do before being able
+** to accept calls into us. If we succeed, we return non-zero and the NetWare
+** Loader will leave us up, otherwise we fail to load and get dumped.
+*/
+  gAllocTag = AllocateResourceTag(NLMHandle,
+                                  "<library-name> memory allocations",
+                                  AllocSignature);
+
+  if (!gAllocTag) {
+    OutputToScreen(errorScreen, "Unable to allocate resource tag for "
+                   "library memory allocations.\n");
+    return -1;
+  }
+
+  gLibId = register_library(DisposeLibraryData);
+
+  if (gLibId < -1) {
+    OutputToScreen(errorScreen, "Unable to register library with kernel.\n");
+    return -1;
+  }
+
+  gLibHandle = NLMHandle;
+
+  gLibLock = NXMutexAlloc(0, 0, &liblock);
+  
+  if (!gLibLock) {
+    OutputToScreen(errorScreen, "Unable to allocate library data lock.\n");
+    return -1;
+  }
+
+  return 0;
+}
+
+/*
+** Here we clean up any resources we allocated. Resource tags is a big part
+** of what we created, but NetWare doesn't ask us to free those.
+*/
+void _NonAppStop( void )
+{
+  (void) unregister_library(gLibId);
+  NXMutexFree(gLibLock);
+}
+
+/*
+** This function cannot be the first in the file for if the file is linked
+** first, then the check-unload function's offset will be nlmname.nlm+0
+** which is how to tell that there isn't one. When the check function is
+** first in the linked objects, it is ambiguous. For this reason, we will
+** put it inside this file after the stop function.
+**
+** Here we check to see if it's alright to ourselves to be unloaded. If not,
+** we return a non-zero value. Right now, there isn't any reason not to allow
+** it.
+*/
+int  _NonAppCheckUnload( void )
+{
+    return 0;
+}
+
+int GetOrSetUpData(int id, libdata_t **appData,
+                   libthreaddata_t **threadData )
+{
+  int                 err;
+  libdata_t           *app_data;
+  libthreaddata_t *thread_data;
+  NXKey_t             key;
+  NX_LOCK_INFO_ALLOC(liblock, "Application Data Lock", 0);
+
+  err         = 0;
+  thread_data = (libthreaddata_t *) NULL;
+
+/*
+** Attempt to get our data for the application calling us. This is where we
+** store whatever application-specific information we need to carry in support
+** of calling applications.
+*/
+  app_data = (libdata_t *) get_app_data(id);
+
+  if (!app_data) {
+/*
+** This application hasn't called us before; set up application AND per-thread
+** data. Of course, just in case a thread from this same application is calling
+** us simultaneously, we better lock our application data-creation mutex. We
+** also need to recheck for data after we acquire the lock because WE might be
+** that other thread that was too late to create the data and the first thread
+** in will have created it.
+*/
+    NXLock(gLibLock);
+
+    if (!(app_data = (libdata_t *) get_app_data(id))) {
+      app_data = (libdata_t *) malloc(sizeof(libdata_t));
+
+      if (app_data) {
+        memset(app_data, 0, sizeof(libdata_t));
+        
+        app_data->tenbytes = malloc(10);
+        app_data->lock     = NXMutexAlloc(0, 0, &liblock);
+        
+        if (!app_data->tenbytes || !app_data->lock) {
+          if (app_data->lock)
+            NXMutexFree(app_data->lock);
+          
+          free(app_data);
+          app_data = (libdata_t *) NULL;
+          err      = ENOMEM;
+        }
+        
+        if (app_data) {
+/*
+** Here we burn in the application data that we were trying to get by calling
+** get_app_data(). Next time we call the first function, we'll get this data
+** we're just now setting. We also go on here to establish the per-thread data
+** for the calling thread, something we'll have to do on each application
+** thread the first time it calls us.
+*/
+          err = set_app_data(gLibId, app_data);
+          
+          if (err) {
+            free(app_data);
+            app_data = (libdata_t *) NULL;
+            err      = ENOMEM;
+          }
+          else {
+            /* create key for thread-specific data... */
+            err = NXKeyCreate(DisposeThreadData, (void *) NULL, &key);
+            
+            if (err)                /* (no more keys left?) */
+              key = -1;
+            
+            app_data->perthreadkey = key;
+          }
+        }
+      }
+    }
+    
+    NXUnlock(gLibLock);
+  }
+
+  if (app_data) {
+    key = app_data->perthreadkey;
+    
+    if (key != -1 /* couldn't create a key? no thread data */
+        && !(err = NXKeyGetValue(key, (void **) &thread_data))
+        && !thread_data) {
+/*
+** Allocate the per-thread data for the calling thread. Regardless of whether
+** there was already application data or not, this may be the first call by a
+** a new thread. The fact that we allocation 20 bytes on a pointer is not very
+** important, this just helps to demonstrate that we can have arbitrarily
+** complex per-thread data.
+*/
+      thread_data = (libthreaddata_t *) malloc(sizeof(libthreaddata_t));
+      
+      if (thread_data) {
+        thread_data->_errno      = 0;
+        thread_data->twentybytes = malloc(20);
+          
+        if (!thread_data->twentybytes) {
+          free(thread_data);
+          thread_data = (libthreaddata_t *) NULL;
+          err         = ENOMEM;
+        }
+        
+        if ((err = NXKeySetValue(key, thread_data))) {
+          free(thread_data->twentybytes);
+          free(thread_data);
+          thread_data = (libthreaddata_t *) NULL;
+        }
+      }
+    }
+  }
+
+  if (appData)
+    *appData = app_data;
+
+  if (threadData)
+    *threadData = thread_data;
+
+  return err;
+}
+
+int DisposeLibraryData( void    *data)
+{
+  if (data) {
+    void    *tenbytes = ((libdata_t *) data)->tenbytes;
+    
+    if (tenbytes)
+      free(tenbytes);
+    
+    free(data);
+  }
+
+  return 0;
+}
+
+void DisposeThreadData(void    *data)
+{
+  if (data) {
+    void    *twentybytes = ((libthreaddata_t *) data)->twentybytes;
+    
+    if (twentybytes)
+      free(twentybytes);
+    
+    free(data);
+  }
+}