use 'if test' in recipes.
[AROS.git] / rom / oop / obtainattrbase.c
blobb6ca152a7a6622236745be5bf77eb2b918f02b78
1 /*
2 Copyright © 1995-2019, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: OOP function OOP_ObtainAttrBase
6 Lang: english
7 */
9 #include "intern.h"
10 #include "hash.h"
11 /*****************************************************************************
13 NAME */
14 #include <string.h>
16 #include <proto/exec.h>
17 #include <exec/memory.h>
18 #include <aros/libcall.h>
20 #include <aros/debug.h>
22 AROS_LH1(OOP_AttrBase, OOP_ObtainAttrBase,
24 /* SYNOPSIS */
25 AROS_LHA(CONST_STRPTR , interfaceID, A0),
27 /* LOCATION */
28 struct Library *, OOPBase, 6, OOP)
30 /* FUNCTION
31 Maps a globally unique string interface ID into
32 a numeric AttrBase ID that is unique on a
33 per machine basis. The AttrBase can be combined
34 with attribute offsets to generate attribute IDs.
36 INPUTS
37 interfaceID - globally unique interface identifier.
38 for which to obtain an attrbase.
40 RESULT
41 Numeric AttrBase that is unique for this machine.
42 A return value of 0 means that the call failed.
44 NOTES
45 Obtained attrbases should be released with ReleaseAttrBase().
47 EXAMPLE
48 #define aTimer_CurrentTime (__AB_Timer + aoTime_CurrentTime)
51 __AB_Timer = OOP_ObtainAttrBase(IID_Timer);
53 SetAttrs(timer, aTimer_CurrentTime, "10:37:00");
56 BUGS
58 SEE ALSO
60 INTERNALS
62 ******************************************************************************/
64 AROS_LIBFUNC_INIT
66 /* Look up ID */
67 struct iid_bucket *idb;
68 struct HashTable *iidtable = GetOBase(OOPBase)->ob_IIDTable;
69 ULONG base = (ULONG)-1;
71 EnterFunc(bug("OOP_ObtainAttrBase(interfaceID=%s)\n", interfaceID));
73 ObtainSemaphore(&GetOBase(OOPBase)->ob_IIDTableLock);
75 /* Has ID already been mapped to a numeric ID? */
76 idb = (struct iid_bucket *)iidtable->Lookup(iidtable, (IPTR)interfaceID, GetOBase(OOPBase));
77 if (idb)
80 /* If so, it has been stored in the hashtable, and we have
81 ** to return the same numeric ID now.
83 if (idb->attrbase == (ULONG)-1)
85 idb->attrbase = ++GetOBase(OOPBase)->ob_CurrentAttrBase;
88 base = idb->attrbase;
89 base <<= NUM_METHOD_BITS;
91 D(bug("Bucket found: id=%ld\n", base));
93 else
96 D(bug("No existing bucket\n"));
99 /* If not, then map it and create a new bucket in the
100 ** hashtable to store it
102 idb = AllocMem(sizeof (struct iid_bucket), MEMF_ANY|MEMF_CLEAR);
103 if (idb)
105 idb->interface_id = AllocVec(strlen(interfaceID) + 1, MEMF_ANY);
106 if (idb->interface_id)
108 D(bug("Allocated bucket\n"));
109 strcpy(idb->interface_id, interfaceID);
111 /* Get next free ID, and increase the free ID count to mark it as used */
112 base = idb->attrbase = ++GetOBase(OOPBase)->ob_CurrentAttrBase;
114 base <<= NUM_METHOD_BITS;
116 /* Methodbase not inited yet */
117 idb->methodbase = (ULONG)-1;
119 /* Insert bucket into hash table */
120 InsertBucket(iidtable, (struct Bucket *)idb, GetOBase(OOPBase));
122 else
124 FreeMem(idb, sizeof (struct iid_bucket));
126 /* Throw exception here ? */
127 base = 0UL;
132 if (base)
134 /* Increase refcount of bucket */
135 idb->refcount ++;
138 ReleaseSemaphore(&GetOBase(OOPBase)->ob_IIDTableLock);
140 ReturnInt ("OOP_ObtainAttrBase", OOP_AttrBase, base);
142 AROS_LIBFUNC_EXIT
144 } /* OOP_ObtainAttrBase */