3 * @brief Mpr initialization
5 * @remarks Most routines in this file are not thread-safe. It is the callers
6 * responsibility to perform all thread synchronization.
12 * Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
14 * This software is distributed under commercial and open source licenses.
15 * You may use the GPL open source license described below or you may acquire
16 * a commercial license from Mbedthis Software. You agree to be fully bound
17 * by the terms of either license. Consult the LICENSE.TXT distributed with
18 * this software for full details.
20 * This software is open source; you can redistribute it and/or modify it
21 * under the terms of the GNU General Public License as published by the
22 * Free Software Foundation; either version 2 of the License, or (at your
23 * option) any later version. See the GNU General Public License for more
24 * details at: http://www.mbedthis.com/downloads/gplLicense.html
26 * This program is distributed WITHOUT ANY WARRANTY; without even the
27 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
29 * This GPL license does NOT permit incorporating this software into
30 * proprietary programs. If you are unable to comply with the GPL, you must
31 * acquire a commercial license to use this software. Commercial licenses
32 * for this software and support services are available from Mbedthis
33 * Software at http://www.mbedthis.com
38 /********************************** Includes **********************************/
40 * We need to use the underlying str(cpy) routines to implement our safe
44 #define UNSAFE_FUNCTIONS_OK 1
49 /******************************************************************************/
51 * Initialize the MPR. Create the top level memory context. This routine is
52 * the first call an MPR application must do. If using MprServices, the
53 * creation of an Mpr object will call this routine.
56 MprApp *mprInit(MprAllocCback cback)
58 return mprInitEx(cback, 0);
61 /******************************************************************************/
63 * Add a shell parameter then do the regular init
66 MprApp *mprInitEx(MprAllocCback cback, void *shell)
70 app = (MprApp*) mprAllocInit(cback);
77 app->name = mprStrdup(app, BLD_PRODUCT);
78 app->title = mprStrdup(app, BLD_NAME);
79 app->version = mprStrdup(app, BLD_VERSION);
81 mprSetShell(app, shell);
83 app->table = mprCreateSymbolTable(app, 0);
85 if (mprStartFileServices(app) < 0) {
90 #if BLD_FEATURE_MULTITHREAD
95 * See if any of the preceeding allocations failed
97 if (mprGetAllocErrors(app) > 0) {
103 * Mark all blocks allocated so far as required. They will then be
104 * omitted from leak reports.
106 mprSetRequiredAlloc(app, 1);
111 /******************************************************************************/
113 * Terminate the MPR. If doStats is true, then output a memory allocation
117 void mprTerm(MprApp *app, bool doStats)
119 #if BLD_FEATURE_ALLOC_STATS
121 mprPrintAllocReport(app, 1, "MPR Memory Allocation Report");
125 #if BLD_FEATURE_MULTITHREAD
129 mprStopFileServices(app);
132 mprValidateAllocTree(app);
137 /******************************************************************************/
139 bool mprIsExiting(MprCtx ctx)
143 app = mprGetApp(ctx);
147 return app->flags & MPR_APP_EXITING;
150 /******************************************************************************/
152 int mprHasAllocError(MprCtx ctx)
156 app = mprGetApp(ctx);
161 return app->flags & MPR_APP_ALLOC_ERROR;
164 /******************************************************************************/
166 void mprSignalExit(MprCtx ctx)
170 app = mprGetApp(ctx);
171 app->flags |= MPR_APP_EXITING;
174 /******************************************************************************/
176 void mprSignalAllocError(MprCtx ctx)
180 app = mprGetApp(ctx);
181 app->flags |= MPR_APP_ALLOC_ERROR;
184 /******************************************************************************/
186 int mprSetAppName(MprCtx ctx, const char *name, const char *title,
191 app = mprGetApp(ctx);
195 if ((app->name = mprStrdup(ctx, name)) == 0) {
196 return MPR_ERR_CANT_ALLOCATE;
202 if ((app->title = mprStrdup(ctx, title)) == 0) {
203 return MPR_ERR_CANT_ALLOCATE;
208 mprFree(app->version);
209 if ((app->version = mprStrdup(ctx, version)) == 0) {
210 return MPR_ERR_CANT_ALLOCATE;
216 /******************************************************************************/
218 const char *mprGetAppName(MprCtx ctx)
222 app = mprGetApp(ctx);
226 /******************************************************************************/
228 const char *mprGetAppTitle(MprCtx ctx)
232 app = mprGetApp(ctx);
236 /******************************************************************************/
238 const char *mprGetAppVersion(MprCtx ctx)
242 app = mprGetApp(ctx);
246 /******************************************************************************/
248 int mprSetKeyValue(MprCtx ctx, const char *key, void *ptr)
252 app = mprGetApp(ctx);
253 if (mprInsertSymbol(app->table, key, ptr) == 0) {
254 return MPR_ERR_CANT_WRITE;
259 /******************************************************************************/
261 int mprRemoveKeyValue(MprCtx ctx, const char *key)
265 app = mprGetApp(ctx);
266 return mprRemoveSymbol(app->table, key);
269 /******************************************************************************/
271 void *mprGetKeyValue(MprCtx ctx, const char *key)
275 app = mprGetApp(ctx);
276 return mprLookupSymbol(app->table, key);
279 /******************************************************************************/
281 bool mprGetDebugMode(MprCtx ctx)
283 return mprGetApp(ctx)->debugMode;
286 /******************************************************************************/
288 void mprSetDebugMode(MprCtx ctx, bool on)
290 mprGetApp(ctx)->debugMode = on;
293 /******************************************************************************/
295 void mprSetLogHandler(MprCtx ctx, MprLogHandler handler)
297 mprGetApp(ctx)->logHandler = handler;
300 /******************************************************************************/
302 MprLogHandler mprGetLogHandler(MprCtx ctx)
304 return mprGetApp(ctx)->logHandler;
308 /******************************************************************************/
310 void mprSetMprInstance(MprCtx ctx, void *mprInstance)
312 mprGetApp(ctx)->mprInstance = mprInstance;
315 /******************************************************************************/
317 void *mprGetMprInstance(MprCtx ctx)
319 return mprGetApp(ctx)->mprInstance;
323 /******************************************************************************/
325 const char *mprCopyright()
327 return "Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.";
330 /******************************************************************************/
338 * vim600: sw=4 ts=4 fdm=marker