📄 gname.c
字号:
/* *---------------------------------------------------------------------- * T-Kernel / Standard Extension * * Copyright (C) 2006 by Ken Sakamura. All rights reserved. * T-Kernel / Standard Extension is distributed * under the T-License for T-Kernel / Standard Extension. *---------------------------------------------------------------------- * * Version: 1.00.00 * Released by T-Engine Forum(http://www.t-engine.org) at 2006/8/11. * *---------------------------------------------------------------------- *//* * gname.c (gname) * * Global name management * */#include <basic.h>#include <tk/tkernel.h>#include <tk/util.h>#include <extension/gname.h>#include <extension/tkcall.h>#include <extension/errno.h>#include <sys/util.h>#include <libstr.h>#include <sys/queue.h>#include <extension/sys/tkse_ssid.h>#include <extension/sys/svc/ifgname.h>#include "gname_def.h"LOCAL UW name2Key(TC *nam);LOCAL void initGlobalName(GlobalName *gname);LOCAL void initGNameEntry(GNameEntry *entry);LOCAL void initListOfEntry(ListOfEntry *list);LOCAL void initPoolList(GNameEntry *first, CONST W numOfEntries);LOCAL void initGNameList(VOID);LOCAL W setName(GNameEntry *entry, TC *name);LOCAL GNameEntry *getEntry(ListOfEntry *list);LOCAL GNameEntry *searchEntryWithName(ListOfEntry *src, TC *name);LOCAL void getListOfPID(ListOfEntry *list, CONST ID pid);LOCAL GNameEntry *searchEntry(TC *name);LOCAL ER _tkse_cre_nam(TC *name, W data, W opt);LOCAL ER _tkse_del_nam(TC *name);LOCAL ER _tkse_get_nam(TC *name, W *data);LOCAL ER initialize(void);LOCAL ER shutdown(void);/* for Debug */#ifdef DEBUG #include "../test/dbgPrGName.h" #define DBN(exp) /* */ #define DBF(exp) /* */ #define DBS(exp) exp #define DBE(exp) exp extern printf();#else #define DBN(exp) /* */ #define DBF(exp) /* */ #define DBS(exp) /* */ #define DBE(exp) /* */#endif#define TSD_TCN_VAL_0X00000000 0x00000000U#define TSD_N2K_VAL_65536 65536U#define TSD_GNE_SZ_16 16/*--------------------------------------------------------------*//* Global Resources *//*--------------------------------------------------------------*/FastLock GNameMgrLock; /* semaphore */#ifdef DEBUGGNameEntry GNameEntryPool[NumOfPoolEntries]; /* pool entry */#elseGNameEntry *GNameEntryPool; /* pool entry */#endifListOfEntry PoolList; /* pool entry list */ListOfEntry GNameList[NumOfHashEntries]; /* enrolled entry list */B MaxNumOfGNameEntries[] = "GlobalNameLimit"; /* tmp *//*--------------------------------------------------------------*//* Translate GlobalName to SearchKey (Hash Function) *//*--------------------------------------------------------------*/LOCAL UW name2Key(TC *nam){ W count; UW hashKey = 0; UH *tc; DBF(printf("+ Name2Key((TC *):%08x)\n", (W)nam)); /* Compute the search key from the global name (Hash Function). */ /* The global name has an area of 16 bytes. */ /* It is assumed that the shortfall is filled with TNULL. */ DBN(printf("Hash : [")); for(count = 0, tc = (UH *)nam;count < GlobalNameLengthLimit; count++){ DBN(printf("%c", *(char *)tc)); DBN(printf("%c", *((char *)tc + 1))); hashKey = ((hashKey * TSD_N2K_VAL_65536) + *tc) % (UW)HashRate; tc++; } DBN(printf("] => %d\n", hashKey)); DBF(printf("- Name2Key():%d\n",hashKey)); return hashKey;}/*--------------------------------------------------------------*//* Initialize GlobalName *//*--------------------------------------------------------------*/LOCAL void initGlobalName(GlobalName *gname){ W count; gname->numOfNames = 0; for(count = 0; count < GlobalNameLengthLimit; count++){ gname->name[count] = TNULL; }}/*--------------------------------------------------------------*//* Initialize GNameEntry *//*--------------------------------------------------------------*/LOCAL void initGNameEntry(GNameEntry *entry){ entry->queue.prev = entry->queue.next = (QUEUE *)entry; entry->searchKey = InvalidKey; entry->pid = InvalidPID; initGlobalName(&(entry->gname)); entry->data = 0;}/*--------------------------------------------------------------*//* Initialize ListOfEntry *//*--------------------------------------------------------------*/LOCAL void initListOfEntry(ListOfEntry *list){ list->prev = list->next = (QUEUE *)list;}/*--------------------------------------------------------------*//* Initialize GNameEntryPool *//*--------------------------------------------------------------*/LOCAL void initPoolList(GNameEntry *first, CONST W numOfEntries){ W count; GNameEntry *entry; DBF(printf("+ initPoolList((GNameEntry *)%08x, (W)%d)\n", (W)first, numOfEntries)); initListOfEntry(&PoolList); for(count = 0, entry = first; count < numOfEntries; count++, entry++){ initGNameEntry(entry); QueInsert((QUEUE *)entry, (QUEUE *)&(PoolList)); } DBF(printf("- initPoolList()\n"));}/*--------------------------------------------------------------*//* Initialize GNameList *//*--------------------------------------------------------------*/LOCAL void initGNameList(VOID){ W count; DBF(printf("+ initGNameList()\n")); for(count = 0; count < NumOfHashEntries; count++){ initListOfEntry(&(GNameList[count])); } DBF(printf("- initGNameList()\n"));}/*--------------------------------------------------------------*//* Global Name Set to GNameEntry *//*--------------------------------------------------------------*/LOCAL W setName(GNameEntry *entry, TC *name){ W count; TC *tc; DBF(printf("+ setName((GNameEntry *):%08x, (TC *):%08x)\n", (W)entry, (W)name)); for(count = 0, tc = name; (count < GlobalNameLengthLimit) && (*tc != TNULL); count++){ entry->gname.name[count] = *tc; tc++; } entry->gname.numOfNames = count; DBF(printf("- setName():%d\n", count)); return count;}/*--------------------------------------------------------------*//* Fetch the entry from the list. (Fetch the first entry unconditionally)*//*--------------------------------------------------------------*/LOCAL GNameEntry *getEntry(ListOfEntry *list){ GNameEntry *retEntry; DBF(printf("+ getEntry((ListOfEntry *):%08x)\n", (W)list)); if(!list) { return (GNameEntry *)NULL; } retEntry = (GNameEntry *)QueRemoveNext((QUEUE *)list); if(retEntry != 0){ retEntry->queue.prev = retEntry->queue.next = (QUEUE *)retEntry; } DBF(printf("- getEntry():%08x\n", (W)retEntry)); return retEntry;}/*--------------------------------------------------------------*//* Retrieve the entry with the global name. *//*--------------------------------------------------------------*/LOCAL GNameEntry *searchEntryWithName(ListOfEntry *src, TC *name){ GNameEntry *aEntry; DBF(printf("+ searchEntryWithName((ListOfEntry *):%08x, (TC *):%08x)\n", (W)src, (W)name)); /* The global name has an area of 16 bytes. */ /* It is assumed that the shortfall is filled with TNULL. */ for(aEntry = (GNameEntry *)src->next; aEntry != (GNameEntry *)src; aEntry = (GNameEntry *)aEntry->queue.next){ if(memcmp(aEntry->gname.name, name, (size_t)TSD_GNE_SZ_16) == 0) { break; } } if(aEntry == (GNameEntry *)src) { aEntry = (GNameEntry *)NULL; } DBF(printf("- searchEntryWithName():%08x\n", (W)aEntry)); return aEntry;}/*--------------------------------------------------------------*//* Retrieve/Fetch/List the entry that has a specific process ID.*//*--------------------------------------------------------------*/LOCAL void getListOfPID(ListOfEntry *list, CONST ID pid){ QUEUE *aEntry; QUEUE *start; QUEUE *src; W count; DBF(printf("+ getListOfPID((ListOfEntry *):%08x, (W):%d\n", (W)list, pid)); if(!list) { return; } for(count = 0; count < NumOfHashEntries; count++){ src = (QUEUE *)&(GNameList[count]); if(isQueEmpty(src) == TRUE) { continue; } start = src; for(;;){ aEntry = QueSearch(start, src, pid, OffsetOfPID); if(aEntry == src) { break; } start = aEntry->prev; QueRemove(aEntry); DBS(printf("Global Name Entry Delete with 3B Process:\n")); DBS(prGNameEntry((GNameEntry *)aEntry)); QueInit(aEntry); /* link detouch */ /* initGNameEntry((GNameEntry *)aEntry)); */ QueInsert(aEntry, (QUEUE *)list); } } DBF(printf("- getListOfPID()\n"));}/*--------------------------------------------------------------*//* Search Entry *//*--------------------------------------------------------------*/LOCAL GNameEntry *searchEntry(TC *name){ UW skey; GNameEntry src; GNameEntry *retEntry = (GNameEntry *)NULL; ListOfEntry *list; DBF(printf("+ searchEntry((TC *):%08x)\n", (W)name)); initGNameEntry(&src); if(setName(&src, name) == 0) { return (GNameEntry *)NULL; } skey = name2Key(src.gname.name); list = &(GNameList[skey]); retEntry = searchEntryWithName(list, src.gname.name); DBF(printf("- searchEntry():%08x\n", (W)retEntry)); return retEntry;}/*--------------------------------------------------------------*//* get Process ID *//*--------------------------------------------------------------*/#ifdef DEBUGEXPORT ID getPID(void){ ID myTskID = 0; get_tid(&myTskID); return myTskID;}#else#define getPID() GetMyPid()#endif/*--------------------------------------------------------------*//* System Call : cre_nam() *//*--------------------------------------------------------------*/LOCAL ER _tkse_cre_nam(TC *name, W data, W opt){ ER aERR = E_OK; GNameEntry *entry = (GNameEntry *)NULL; ID pid = InvalidPID; W count; TC *nm; UW atr = (UW)opt; DBS(printf("+ cre_nam((TC *): %08x, (W): %d, (W):%08x)\n", (W)name, data, opt)); /* Parameter check */ if(!name) { return E_MACV; } for(count = 0, nm = name; count < GlobalNameLengthLimit; count++){ if(CheckSpaceR(nm, sizeof(TC)) < 0) { return E_MACV; } if(*nm == TNULL){ if(count != 0){ break; }else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -