Minor fixes to comments.
[AROS.git] / rom / intuition / makeclass.c
blobbdad8e083df569c40a5359e69d49803279cbece7
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 Copyright © 2001-2003, The MorphOS Development Team. All Rights Reserved.
4 $Id$
6 Initialize a BOOPSI class.
7 */
9 #include <exec/lists.h>
10 #include <exec/memory.h>
11 #include <proto/exec.h>
12 #include "intuition_intern.h"
14 #define MAX_PUDDLE_SIZE (16 * 1024) /* Maximimum puddle size */
16 /*****************************************************************************
18 NAME */
19 #include <intuition/classes.h>
20 #include <proto/intuition.h>
22 AROS_LH5(struct IClass *, MakeClass,
24 /* SYNOPSIS */
25 AROS_LHA(ClassID, classID, A0),
26 AROS_LHA(ClassID, superClassID, A1),
27 AROS_LHA(struct IClass *, superClassPtr, A2),
28 AROS_LHA(ULONG, instanceSize, D0),
29 AROS_LHA(ULONG, flags, D1),
31 /* LOCATION */
32 struct IntuitionBase *, IntuitionBase, 113, Intuition)
34 /* FUNCTION
35 Only for class implementators.
37 This function creates a new public BOOPSI class. The SuperClass
38 should be another BOOPSI class; all BOOPSI classes are subclasses
39 of the ROOTCLASS.
41 SuperClasses can by private or public. You can specify a name/ID
42 for the class if you want it to become a public class. For public
43 classes, you must call AddClass() afterwards to make it public
44 accessible.
46 The return value contains a pointer to the IClass structure of your
47 class. You must specify your dispatcher in cl_Dispatcher. You can
48 also store shared data in cl_UserData.
50 To get rid of the class, you must call FreeClass().
52 INPUTS
53 classID - NULL for private classes otherwise the name/ID of the
54 public class.
55 superClassID - Name/ID of a public SuperClass. NULL is you don't
56 want to use a public SuperClass or if you have the pointer
57 your SuperClass.
58 superClassPtr - Pointer to the SuperClass. If this is non-NULL,
59 then superClassID is ignored.
60 instanceSize - The amount of memory which your objects need (in
61 addition to the memory which is needed by the SuperClass(es))
62 flags - For future extensions. To maintain comaptibility, use 0
63 for now.
65 RESULT
66 Pointer to the new class or NULL if
67 - There wasn't enough memory
68 - The superclass couldn't be found
69 - There already is a class with the same name/ID.
71 NOTES
72 No copy is made of classID. So make sure the lifetime of the contents
73 of classID is at least the same as the lifetime of the class itself.
75 EXAMPLE
77 BUGS
79 SEE ALSO
81 INTERNALS
83 *****************************************************************************/
85 AROS_LIBFUNC_INIT
87 Class *iclass = NULL;
89 EXTENDUWORD(instanceSize);
91 DEBUG_MAKECLASS(dprintf("MakeClass: ID <%s> SuperID <%s> Super 0x%lx Size 0x%lx Flags 0x%lx\n",
92 classID ? classID : (UBYTE*)"NULL",
93 superClassID ? superClassID : (UBYTE*)"NULL",
94 superClassPtr,
95 instanceSize,
96 flags));
98 /* trust the user ;-) */
99 if (!superClassID && !superClassPtr)
101 #ifdef __MORPHOS__
102 /* Workaround for buggy callers: set z flag - Piru */
103 REG_SR |= 4;
104 #endif
105 return NULL;
108 ObtainSemaphoreShared(&GetPrivIBase(IntuitionBase)->ClassListLock);
110 /* Does this class already exist? */
111 if (!FindClass(classID))
113 /* Has the user specified a classPtr? */
114 if (!superClassPtr)
116 /* Search for the class... */
117 superClassPtr = FindClass(superClassID);
120 if (superClassPtr)
122 /* Allocate memory */
123 iclass = (Class *) AllocMem
125 sizeof(Class), MEMF_PUBLIC | MEMF_CLEAR
128 if (iclass != NULL)
130 int perpuddle;
132 /* Initialize fields */
133 iclass->cl_Super = superClassPtr;
134 iclass->cl_ID = classID;
135 iclass->cl_InstOffset = superClassPtr->cl_InstOffset +
136 superClassPtr->cl_InstSize;
137 iclass->cl_InstSize = instanceSize;
138 iclass->cl_Flags = flags;
139 iclass->cl_ObjectSize = iclass->cl_InstOffset
140 + iclass->cl_InstSize
141 + sizeof(struct _Object);
143 /* Try to limit the puddle to MAX_PUDDLE_SIZE.
144 * This comes in to play, for example, with
145 * picture.library, where 32 instances of the
146 * picture class is a whopping 280K.
148 perpuddle = MAX_PUDDLE_SIZE / iclass->cl_ObjectSize;
149 if (perpuddle == 0)
150 perpuddle = 1;
151 if (perpuddle > 32)
152 perpuddle = 32;
154 /* Initialize memory subsystem */
155 iclass->cl_MemoryPool = CreatePool
157 MEMF_ANY | MEMF_CLEAR | MEMF_SEM_PROTECTED,
158 perpuddle * iclass->cl_ObjectSize, iclass->cl_ObjectSize
161 if (iclass->cl_MemoryPool != NULL)
163 /* SuperClass is used one more time now */
164 AROS_ATOMIC_INC(superClassPtr->cl_SubclassCount);
166 else
168 FreeMem(iclass, sizeof(Class));
169 iclass = NULL;
173 else
175 DEBUG_MAKECLASS(dprintf("MakeClass: superclass not found\n"));
178 else
180 DEBUG_MAKECLASS(dprintf("MakeClass: already there\n"));
183 ReleaseSemaphore(&GetPrivIBase(IntuitionBase)->ClassListLock);
185 DEBUG_MAKECLASS(dprintf("MakeClass: return 0x%lx\n", iclass));
187 #ifdef __MORPHOS__
188 /* Workaround for buggy callers: clear/set z flag - Piru */
189 if (iclass) REG_SR &= (ULONG) ~4;
190 else REG_SR |= 4;
191 #endif
193 return iclass;
195 AROS_LIBFUNC_EXIT
196 } /* MakeClass() */