Minor fixes to comments.
[AROS.git] / rom / exec / makelibrary.c
blobd7fe4c68e5d3e203425cba293e5479c1eb3d54db
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Make a library ready for use.
6 Lang: english
7 */
9 #include <exec/execbase.h>
10 #include <exec/memory.h>
11 #include <dos/dos.h>
12 #include <aros/libcall.h>
13 #include <aros/asmcall.h>
14 #include <proto/exec.h>
16 #include "exec_debug.h"
18 /*****************************************************************************
20 NAME */
22 AROS_LH5(struct Library *, MakeLibrary,
24 /* SYNOPSIS */
25 AROS_LHA(CONST_APTR, funcInit, A0),
26 AROS_LHA(CONST_APTR, structInit, A1),
27 AROS_LHA(ULONG_FUNC, libInit, A2),
28 AROS_LHA(ULONG, dataSize, D0),
29 AROS_LHA(BPTR, segList, D1),
31 /* LOCATION */
32 struct ExecBase *, SysBase, 14, Exec)
34 /* FUNCTION
35 Allocates memory for the library, builds it and calls the library's
36 init vector. Generally this function is for internal use and for
37 use by library programmers that don't want to use the automatic
38 initialization procedure.
40 INPUTS
41 funcInit - Either a pointer to an array of function offsets
42 (starts with -1, relative to funcInit) or to an array
43 of absolute function pointers.
44 structInit - Pointer to a InitStruct() data region or NULL.
45 libInit - The library's init vector or NULL.
46 The init vector is called with the library address (D0),
47 the segList (A0) and ExecBase (A6) as arguments.
48 If the init fails the init code has to free the base memory
49 and return NULL (the library address for success).
50 dataSize - Size of the library structure including system structures.
51 Must be at least sizeof(struct Library).
52 segList - BCPL pointer to the library segments. Used to free the
53 library later.
55 RESULT
56 The library base address or NULL.
58 NOTES
59 The library base is always aligned to the maximum of sizeof(LONG)
60 and double alignment restrictions.
62 EXAMPLE
64 BUGS
66 SEE ALSO
67 AddLibrary(), RemLibrary(), MakeFunctions(), InitStruct(), SumLibrary()
69 INTERNALS
71 ******************************************************************************/
73 AROS_LIBFUNC_INIT
75 struct Library *library;
76 ULONG negsize;
77 ULONG count = 0;
79 DCREATELIBRARY("MakeLibrary: functions table at 0x%p, data size is %lu", funcInit, dataSize);
81 /* Can't do the following check on machines like x86, because 0x????ffff is
82 a valid address for code there! */
84 #if defined(__mc68000) || defined(__ppc__) || defined(__powerpc__)
85 /* Calculate the jumptable's size */
86 if (*(WORD *)funcInit==-1)
88 /* Count offsets */
89 WORD *fp=(WORD *)funcInit+1;
91 while(*fp++!=-1)
92 count++;
93 DCREATELIBRARY("Table contains %lu relative offsets", count);
95 else
96 #endif
98 /* Count function pointers */
99 void **fp=(void **)funcInit;
101 while(*fp++!=(void *)-1)
102 count++;
104 DCREATELIBRARY("Table contains %lu absolute pointers", count);
107 /* Align library base */
108 negsize=AROS_ALIGN(count * LIB_VECTSIZE);
110 /* Allocate memory */
111 library=(struct Library *)AllocMem(dataSize+negsize,MEMF_PUBLIC|MEMF_CLEAR);
112 DCREATELIBRARY("Allocated vector table at 0x%p, size is %lu", library, negsize);
114 /* And initilize the library */
115 if (library!=NULL)
117 /* Get real library base */
118 library=(struct Library *)((char *)library+negsize);
120 /* Build jumptable */
121 #if defined(__mc68000) || defined(__ppc__) || defined(__powerpc__)
122 if(*(WORD *)funcInit==-1)
123 /* offsets */
124 MakeFunctions(library,(WORD *)funcInit+1,(WORD *)funcInit);
125 else
126 #endif
127 /* function pointers */
128 MakeFunctions(library,funcInit,NULL);
130 /* Write sizes */
131 library->lib_NegSize=negsize;
132 library->lib_PosSize=dataSize;
134 /* Create structure, do not clear struct Library */
135 if(structInit!=NULL)
136 InitStruct(structInit,library,0);
138 /* Call init vector */
139 if (libInit!=NULL)
141 DCREATELIBRARY("Calling init function 0x%p", libInit);
143 library=AROS_UFC3(struct Library *, libInit,
144 AROS_UFCA(struct Library *, library, D0),
145 AROS_UFCA(BPTR, segList, A0),
146 AROS_UFCA(struct ExecBase *, SysBase, A6)
151 /* All done */
152 DCREATELIBRARY("Created library 0x%p", library);
153 return library;
155 AROS_LIBFUNC_EXIT
156 } /* MakeLibrary */