Branch data Line data Source code
1 : : /*
2 : : Copyright (C) 2012-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 Pinto nor the names of its contributors may be used to endorse
15 : : or promote products derived from this software without specific prior
16 : : written 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, Realloc, 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/realloc failures.
43 : : */
44 : :
45 : : /******************************************************************************/
46 : : #include <stdlib.h> /* malloc, calloc, realloc, free */
47 : : #include <errno.h> /* errno */
48 : : #include <stdio.h> /* fprintf, fflush */
49 : : #include <string.h> /* strerror */
50 : :
51 : : #include "pinto.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 *(*pintoHookMalloc)( 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 *(*pintoHookCalloc)( size_t nmemb, size_t size ) = calloc;
62 : : /*! This is the function that the library uses for 'realloc'. Used for unit
63 : : testing failed reallocs and in case the user of the library has their own
64 : : memory management routines. */
65 : : void *(*pintoHookRealloc)( void *ptr, size_t size ) = realloc;
66 : : /*! This is the function that the library uses for 'free'. Used in case the
67 : : user of the library has their own memory management routines. */
68 : : void (*pintoHookFree)( void *ptr ) = free;
69 : :
70 : : /*! The default log function. Sends log messages to stderr. */
71 : 2935 : void pintoHookLogDefault( s32 library, s32 file, s32 line, s32 rc, s32 a, s32 b, s32 c )
72 : : {
73 [ + + ]: 2935 : if ( errno == 0 )
74 : : {
75 [ + + ]: 2933 : fprintf( stderr, "%d %d %5d - %2d %s - - %d %d %d\n",
76 : : library, file, line,
77 : : rc, ( rc == 0 ? "" : pintoRCToString( rc ) ),
78 : : a, b, c
79 : : );
80 : : }
81 : : else
82 : : {
83 [ + + ]: 4 : fprintf( stderr, "%d %d %5d - %2d %s - %d %s - %d %d %d\n",
84 : : library, file, line,
85 : : rc, ( rc == 0 ? "" : pintoRCToString( rc ) ),
86 : 4 : errno, strerror( errno ),
87 : : a, b, c
88 : : );
89 : : }
90 : 2935 : fflush( stderr );
91 : 2935 : }
92 : :
93 : : /*! This function pointer is used by Pinto to log errors. Not really useful for
94 : : end-users of the library, but useful for developers. The default function
95 : : prints to stderr, but you can change this to plug Pinto into your own
96 : : logging system. */
97 : : void (*pintoHookLog)( s32 library, s32 file, s32 line, s32 rc, s32 a, s32 b, s32 c ) = pintoHookLogDefault;
98 : :
|