00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef NETWARE
00025
00026 #include <stdlib.h>
00027
00028 #ifdef __NOVELL_LIBC__
00029
00030 #include <errno.h>
00031 #include <string.h>
00032 #include <library.h>
00033 #include <netware.h>
00034 #include <screen.h>
00035 #include <nks/thread.h>
00036 #include <nks/synch.h>
00037
00038 #include "memory.h"
00039 #include "memdebug.h"
00040
00041 typedef struct
00042 {
00043 int _errno;
00044 void *twentybytes;
00045 } libthreaddata_t;
00046
00047 typedef struct
00048 {
00049 int x;
00050 int y;
00051 int z;
00052 void *tenbytes;
00053 NXKey_t perthreadkey;
00054 NXMutex_t *lock;
00055 } libdata_t;
00056
00057 int gLibId = -1;
00058 void *gLibHandle = (void *) NULL;
00059 rtag_t gAllocTag = (rtag_t) NULL;
00060 NXMutex_t *gLibLock = (NXMutex_t *) NULL;
00061
00062
00063 int DisposeLibraryData ( void * );
00064 void DisposeThreadData ( void * );
00065 int GetOrSetUpData ( int id, libdata_t **data, libthreaddata_t **threaddata );
00066
00067
00068 int _NonAppStart( void *NLMHandle,
00069 void *errorScreen,
00070 const char *cmdLine,
00071 const char *loadDirPath,
00072 size_t uninitializedDataLength,
00073 void *NLMFileHandle,
00074 int (*readRoutineP)( int conn,
00075 void *fileHandle, size_t offset,
00076 size_t nbytes,
00077 size_t *bytesRead,
00078 void *buffer ),
00079 size_t customDataOffset,
00080 size_t customDataSize,
00081 int messageCount,
00082 const char **messages )
00083 {
00084 NX_LOCK_INFO_ALLOC(liblock, "Per-Application Data Lock", 0);
00085
00086 #ifndef __GNUC__
00087 #pragma unused(cmdLine)
00088 #pragma unused(loadDirPath)
00089 #pragma unused(uninitializedDataLength)
00090 #pragma unused(NLMFileHandle)
00091 #pragma unused(readRoutineP)
00092 #pragma unused(customDataOffset)
00093 #pragma unused(customDataSize)
00094 #pragma unused(messageCount)
00095 #pragma unused(messages)
00096 #endif
00097
00098
00099
00100
00101
00102
00103
00104 gAllocTag = AllocateResourceTag(NLMHandle,
00105 "<library-name> memory allocations",
00106 AllocSignature);
00107
00108 if (!gAllocTag) {
00109 OutputToScreen(errorScreen, "Unable to allocate resource tag for "
00110 "library memory allocations.\n");
00111 return -1;
00112 }
00113
00114 gLibId = register_library(DisposeLibraryData);
00115
00116 if (gLibId < -1) {
00117 OutputToScreen(errorScreen, "Unable to register library with kernel.\n");
00118 return -1;
00119 }
00120
00121 gLibHandle = NLMHandle;
00122
00123 gLibLock = NXMutexAlloc(0, 0, &liblock);
00124
00125 if (!gLibLock) {
00126 OutputToScreen(errorScreen, "Unable to allocate library data lock.\n");
00127 return -1;
00128 }
00129
00130 return 0;
00131 }
00132
00133
00134
00135
00136
00137 void _NonAppStop( void )
00138 {
00139 (void) unregister_library(gLibId);
00140 NXMutexFree(gLibLock);
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 int _NonAppCheckUnload( void )
00155 {
00156 return 0;
00157 }
00158
00159 int GetOrSetUpData(int id, libdata_t **appData,
00160 libthreaddata_t **threadData )
00161 {
00162 int err;
00163 libdata_t *app_data;
00164 libthreaddata_t *thread_data;
00165 NXKey_t key;
00166 NX_LOCK_INFO_ALLOC(liblock, "Application Data Lock", 0);
00167
00168 err = 0;
00169 thread_data = (libthreaddata_t *) NULL;
00170
00171
00172
00173
00174
00175
00176 app_data = (libdata_t *) get_app_data(id);
00177
00178 if (!app_data) {
00179
00180
00181
00182
00183
00184
00185
00186
00187 NXLock(gLibLock);
00188
00189 if (!(app_data = (libdata_t *) get_app_data(id))) {
00190 app_data = (libdata_t *) malloc(sizeof(libdata_t));
00191
00192 if (app_data) {
00193 memset(app_data, 0, sizeof(libdata_t));
00194
00195 app_data->tenbytes = malloc(10);
00196 app_data->lock = NXMutexAlloc(0, 0, &liblock);
00197
00198 if (!app_data->tenbytes || !app_data->lock) {
00199 if (app_data->lock)
00200 NXMutexFree(app_data->lock);
00201
00202 free(app_data);
00203 app_data = (libdata_t *) NULL;
00204 err = ENOMEM;
00205 }
00206
00207 if (app_data) {
00208
00209
00210
00211
00212
00213
00214
00215 err = set_app_data(gLibId, app_data);
00216
00217 if (err) {
00218 free(app_data);
00219 app_data = (libdata_t *) NULL;
00220 err = ENOMEM;
00221 }
00222 else {
00223
00224 err = NXKeyCreate(DisposeThreadData, (void *) NULL, &key);
00225
00226 if (err)
00227 key = -1;
00228
00229 app_data->perthreadkey = key;
00230 }
00231 }
00232 }
00233 }
00234
00235 NXUnlock(gLibLock);
00236 }
00237
00238 if (app_data) {
00239 key = app_data->perthreadkey;
00240
00241 if (key != -1
00242 && !(err = NXKeyGetValue(key, (void **) &thread_data))
00243 && !thread_data) {
00244
00245
00246
00247
00248
00249
00250
00251 thread_data = (libthreaddata_t *) malloc(sizeof(libthreaddata_t));
00252
00253 if (thread_data) {
00254 thread_data->_errno = 0;
00255 thread_data->twentybytes = malloc(20);
00256
00257 if (!thread_data->twentybytes) {
00258 free(thread_data);
00259 thread_data = (libthreaddata_t *) NULL;
00260 err = ENOMEM;
00261 }
00262
00263 if ((err = NXKeySetValue(key, thread_data))) {
00264 free(thread_data->twentybytes);
00265 free(thread_data);
00266 thread_data = (libthreaddata_t *) NULL;
00267 }
00268 }
00269 }
00270 }
00271
00272 if (appData)
00273 *appData = app_data;
00274
00275 if (threadData)
00276 *threadData = thread_data;
00277
00278 return err;
00279 }
00280
00281 int DisposeLibraryData( void *data )
00282 {
00283 if (data) {
00284 void *tenbytes = ((libdata_t *) data)->tenbytes;
00285
00286 if (tenbytes)
00287 free(tenbytes);
00288
00289 free(data);
00290 }
00291
00292 return 0;
00293 }
00294
00295 void DisposeThreadData( void *data )
00296 {
00297 if (data) {
00298 void *twentybytes = ((libthreaddata_t *) data)->twentybytes;
00299
00300 if (twentybytes)
00301 free(twentybytes);
00302
00303 free(data);
00304 }
00305 }
00306
00307 #else
00308
00309 #include <nwthread.h>
00310
00311
00312
00313
00314
00315
00316
00317
00318 int main ( void )
00319 {
00320
00321
00322
00323
00324
00325 ExitThread (TSR_THREAD, 0);
00326 return 0;
00327 }
00328
00329 #endif
00330
00331 #endif