PerformanceLib.cpp
Go to the documentation of this file.
00001 // ****************************************************************************
00002 // This file is part of the Integrating Vision Toolkit (IVT).
00003 //
00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
00006 //
00007 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
00008 // All rights reserved.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are met:
00012 //
00013 // 1. Redistributions of source code must retain the above copyright
00014 //    notice, this list of conditions and the following disclaimer.
00015 //
00016 // 2. Redistributions in binary form must reproduce the above copyright
00017 //    notice, this list of conditions and the following disclaimer in the
00018 //    documentation and/or other materials provided with the distribution.
00019 //
00020 // 3. Neither the name of the KIT nor the names of its contributors may be
00021 //    used to endorse or promote products derived from this software
00022 //    without specific prior written permission.
00023 //
00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 // ****************************************************************************
00035 // ****************************************************************************
00036 // Filename:  PerformanceLib.cpp
00037 // Author:    Florian Hecht
00038 // Date:      2008
00039 // ****************************************************************************
00040 
00041 #include <new> // for explicitly using correct new/delete operators on VC DSPs
00042 
00043 #include "PerformanceLib.h"
00044 
00045 
00046 // this is needed so that the function pointers are not declared extern
00047 // and are defined here
00048 #define OPT_DEFINE
00049 
00050 // include all the function prototypes and function pointers
00051 #include "OptimizedFunctions.h"
00052 
00053 // include the necessary headers to load a dynamic library
00054 #ifdef WIN32
00055 #include <windows.h>
00056 #elif defined(LOAD_KPP)
00057 #include <dlfcn.h>
00058 #endif
00059 
00060 #include <stdio.h>
00061 
00062 // this is the only instance that should be created ever
00063 // it's global so it executes before the main function
00064 #ifdef WIN32
00065 //CPerformanceLibInitializer performanceLibInitializer; // activate this line under Windows to automatically load the KPP
00066 #else
00067 CPerformanceLibInitializer performanceLibInitializer;
00068 #endif
00069 
00070 
00071 #if defined(WIN32)
00072 #define LOAD_OPTIMIZED_FUNCTION(name) Optimized##name = (DefOptimized##name) GetProcAddress(hMod, #name);
00073 #elif defined(LOAD_KPP)
00074 #define LOAD_OPTIMIZED_FUNCTION(name) Optimized##name = (DefOptimized##name) dlsym(m_pLibHandle, #name);
00075 #else
00076 // dummy
00077 #define LOAD_OPTIMIZED_FUNCTION(name) Optimized##name = 0;
00078 #endif
00079 
00080 // these macros load a function from the shared library
00081 // different macros for different number of parameters
00082 #undef DECLARE_OPTIMIZED_FUNCTION_0
00083 #undef DECLARE_OPTIMIZED_FUNCTION_1
00084 #undef DECLARE_OPTIMIZED_FUNCTION_2
00085 #undef DECLARE_OPTIMIZED_FUNCTION_3
00086 #undef DECLARE_OPTIMIZED_FUNCTION_4
00087 #undef DECLARE_OPTIMIZED_FUNCTION_5
00088 #undef DECLARE_OPTIMIZED_FUNCTION_5_RET
00089 #undef DECLARE_OPTIMIZED_FUNCTION_8
00090 #undef DECLARE_OPTIMIZED_FUNCTION_9
00091 
00092 #define DECLARE_OPTIMIZED_FUNCTION_0(name) LOAD_OPTIMIZED_FUNCTION(name)
00093 #define DECLARE_OPTIMIZED_FUNCTION_1(name, p1) LOAD_OPTIMIZED_FUNCTION(name)
00094 #define DECLARE_OPTIMIZED_FUNCTION_2(name, p1, p2) LOAD_OPTIMIZED_FUNCTION(name)
00095 #define DECLARE_OPTIMIZED_FUNCTION_3(name, p1, p2, p3) LOAD_OPTIMIZED_FUNCTION(name)
00096 #define DECLARE_OPTIMIZED_FUNCTION_4(name, p1, p2, p3, p4) LOAD_OPTIMIZED_FUNCTION(name)
00097 #define DECLARE_OPTIMIZED_FUNCTION_5(name, p1, p2, p3, p4, p5) LOAD_OPTIMIZED_FUNCTION(name)
00098 #define DECLARE_OPTIMIZED_FUNCTION_5_RET(name, p1, p2, p3, p4, p5) LOAD_OPTIMIZED_FUNCTION(name)
00099 #define DECLARE_OPTIMIZED_FUNCTION_8(name, p1, p2, p3, p4, p5, p6, p7, p8) LOAD_OPTIMIZED_FUNCTION(name)
00100 #define DECLARE_OPTIMIZED_FUNCTION_9(name, p1, p2, p3, p4, p5, p6, p7, p8, p9) LOAD_OPTIMIZED_FUNCTION(name)
00101 
00102 
00103 CPerformanceLibInitializer::CPerformanceLibInitializer() : m_pLibHandle(0)
00104 {
00105         LoadPerformanceLib();
00106 }
00107 
00108 CPerformanceLibInitializer::~CPerformanceLibInitializer()
00109 {
00110         FreePerformanceLib();
00111 }
00112 
00113 void CPerformanceLibInitializer::LoadPerformanceLib()
00114 {
00115         // only load once
00116         if (m_pLibHandle != 0)
00117                 return;
00118                 
00119         #if defined(WIN32)
00120         HMODULE hMod = LoadLibrary("KPP.dll");
00121         if (hMod == NULL)
00122                 return;
00123         m_pLibHandle = hMod;
00124         #elif defined(LOAD_KPP)
00125         #ifdef __APPLE__
00126         m_pLibHandle = dlopen("libkpp.dylib", RTLD_NOW);
00127         #else
00128         m_pLibHandle = dlopen("libkpp.so", RTLD_NOW);
00129         #endif
00130         if (m_pLibHandle == 0)
00131                 return;
00132         #else
00133         return;
00134         #endif
00135         
00136         // this include will generate the necessary loading functions
00137         // due to the DECLARE_OPTIMIZED_FUNCTION_x macros
00138         #include "OptimizedFunctionsList.h"
00139         
00140         // activate with license key
00141         typedef bool (*ActivateFunction) (const char *);
00142         ActivateFunction f = 0;
00143         
00144         #ifdef WIN32
00145         f = (ActivateFunction) GetProcAddress(hMod, "Activate");
00146         #elif defined(LOAD_KPP)
00147         f = (ActivateFunction) dlsym(m_pLibHandle, "Activate");
00148         #endif
00149         
00150         if (f)
00151         {
00152                 printf("info: loaded Keyetech Performance Primitives (KPP)\n");
00153                 
00154                 if (f("license_kpp.txt"))
00155                         printf("info: activated KPP with license key\n");
00156                 else
00157                 {
00158                         printf("info: KPP activation with licensey key failed\n");
00159                         printf("info: The KPP thus run as a demo version.\n");
00160                 }
00161         }
00162 }
00163 
00164 
00165 // these macros clear the function pointers
00166 #undef DECLARE_OPTIMIZED_FUNCTION_0
00167 #undef DECLARE_OPTIMIZED_FUNCTION_1
00168 #undef DECLARE_OPTIMIZED_FUNCTION_2
00169 #undef DECLARE_OPTIMIZED_FUNCTION_3
00170 #undef DECLARE_OPTIMIZED_FUNCTION_4
00171 #undef DECLARE_OPTIMIZED_FUNCTION_5
00172 #undef DECLARE_OPTIMIZED_FUNCTION_5_RET
00173 #undef DECLARE_OPTIMIZED_FUNCTION_8
00174 #undef DECLARE_OPTIMIZED_FUNCTION_9
00175 
00176 #define DECLARE_OPTIMIZED_FUNCTION_0(name) Optimized##name = 0;
00177 #define DECLARE_OPTIMIZED_FUNCTION_1(name, p1) Optimized##name = 0;
00178 #define DECLARE_OPTIMIZED_FUNCTION_2(name, p1, p2) Optimized##name = 0;
00179 #define DECLARE_OPTIMIZED_FUNCTION_3(name, p1, p2, p3) Optimized##name = 0;
00180 #define DECLARE_OPTIMIZED_FUNCTION_4(name, p1, p2, p3, p4) Optimized##name = 0;
00181 #define DECLARE_OPTIMIZED_FUNCTION_5(name, p1, p2, p3, p4, p5) Optimized##name = 0;
00182 #define DECLARE_OPTIMIZED_FUNCTION_5_RET(name, p1, p2, p3, p4, p5) Optimized##name = 0;
00183 #define DECLARE_OPTIMIZED_FUNCTION_8(name, p1, p2, p3, p4, p5, p6, p7, p8) Optimized##name = 0;
00184 #define DECLARE_OPTIMIZED_FUNCTION_9(name, p1, p2, p3, p4, p5, p6, p7, p8, p9) Optimized##name = 0;
00185 
00186 void CPerformanceLibInitializer::FreePerformanceLib()
00187 {
00188         if (m_pLibHandle != 0)
00189         {
00190                 #if defined(WIN32)
00191                 HMODULE hMod = (HMODULE) m_pLibHandle;
00192                 FreeLibrary(hMod);
00193                 #elif defined(LOAD_KPP)
00194                 dlclose(m_pLibHandle);
00195                 #endif
00196                 
00197                 m_pLibHandle = 0;
00198         }
00199         
00200         // this include will generate the necessary clear operations
00201         // due to the DECLARE_OPTIMIZED_FUNCTION_x macros
00202         #include "OptimizedFunctionsList.h"
00203 }


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Thu Jun 6 2019 21:46:57