Branch data Line data Source code
1 : : /*
2 : : Copyright (c) 2010-2014 Jeremiah Martell
3 : : All rights reserved.
4 : :
5 : : Redistribution and use in source and binary forms, with or without modification,
6 : : are permitted provided that the following conditions are met:
7 : :
8 : : - Redistributions of source code must retain the above copyright notice,
9 : : this list of conditions and the following disclaimer.
10 : : - Redistributions in binary form must reproduce the above copyright notice,
11 : : this list of conditions and the following disclaimer in the documentation
12 : : and/or other materials provided with the distribution.
13 : : - Neither the name of Jeremiah Martell nor the name of GeekHorse nor the
14 : : name of Trot nor the names of its contributors may be used to endorse or
15 : : promote products derived from this software without specific prior written
16 : : permission.
17 : :
18 : : THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 : : ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 : : WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 : : DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22 : : ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 : : (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 : : LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 : : ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 : : (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 : : SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 : : */
29 : :
30 : : /******************************************************************************/
31 : : /*!
32 : : \file
33 : : Contains hook function pointers for:
34 : : - Malloc, Calloc, Free
35 : : - Logging
36 : :
37 : : Used if we're embedded in an app that uses it's own
38 : : memory functions. We can easily "plug into" their memory management
39 : : system.
40 : : We can also "plug into" their logging system.
41 : :
42 : : Used in our unit tests for testing malloc/calloc failures.
43 : : */
44 : :
45 : : /******************************************************************************/
46 : : #include <stdlib.h> /* for malloc, calloc, free */
47 : : #include <errno.h> /* errno */
48 : : #include <stdio.h> /* fprintf, fflush */
49 : : #include <string.h> /* strerror */
50 : :
51 : : #include "trot.h"
52 : :
53 : : /******************************************************************************/
54 : : /*! This is the function that the library uses for 'malloc'. Used for unit
55 : : testing failed mallocs and in case the user of the library has their own
56 : : memory management routines. */
57 : : void *(*trotHookMalloc)( size_t size ) = malloc;
58 : : /*! This is the function that the library uses for 'calloc'. Used for unit
59 : : testing failed callocs and in case the user of the library has their own
60 : : memory management routines. */
61 : : void *(*trotHookCalloc)( size_t nmemb, size_t size ) = calloc;
62 : : /*! This is the function that the library uses for 'free'. Used in case the
63 : : user of the library has their own memory management routines. */
64 : : void (*trotHookFree)( void *ptr ) = free;
65 : :
66 : : /*! The default log function. Sends log messages to stderr. */
67 : 4 : void trotHookLogDefault( s32 library, s32 file, s32 line, TROT_INT rc, TROT_INT a, TROT_INT b, TROT_INT c )
68 : : {
69 [ + + ]: 4 : if ( errno == 0 )
70 : : {
71 [ + + ]: 2 : fprintf( stderr, "%d %d %5d - %2d %s - - %d %d %d\n",
72 : : library, file, line,
73 : : rc, ( rc == 0 ? "" : trotRCToString( rc ) ),
74 : : a, b, c
75 : : );
76 : : }
77 : : else
78 : : {
79 [ + + ]: 4 : fprintf( stderr, "%d %d %5d - %2d %s - %d %s - %d %d %d\n",
80 : : library, file, line,
81 : : rc, ( rc == 0 ? "" : trotRCToString( rc ) ),
82 : 4 : errno, strerror( errno ),
83 : : a, b, c
84 : : );
85 : : }
86 : 4 : fflush( stderr );
87 : 4 : }
88 : :
89 : : /*! This function pointer is used by Trot to log errors. Not really useful for
90 : : end-users of the library, but useful for developers. The default function
91 : : prints to stderr, but you can change this to plug Trot into your own
92 : : logging system. */
93 : : void (*trotHookLog)( s32 library, s32 file, s32 line, TROT_INT rc, TROT_INT a, TROT_INT b, TROT_INT c ) = trotHookLogDefault;
94 : :
|