1*418b791dSBob Badour #ifndef PLATFORM_LIBS_H 2*418b791dSBob Badour #define PLATFORM_LIBS_H 3*418b791dSBob Badour 4*418b791dSBob Badour /** 5*418b791dSBob Badour * Copyright (c) 2019, The Linux Foundation. All rights reserved. 6*418b791dSBob Badour * 7*418b791dSBob Badour * Redistribution and use in source and binary forms, with or without 8*418b791dSBob Badour * modification, are permitted provided that the following conditions are 9*418b791dSBob Badour * met: 10*418b791dSBob Badour * * Redistributions of source code must retain the above copyright 11*418b791dSBob Badour * notice, this list of conditions and the following disclaimer. 12*418b791dSBob Badour * * Redistributions in binary form must reproduce the above 13*418b791dSBob Badour * copyright notice, this list of conditions and the following 14*418b791dSBob Badour * disclaimer in the documentation and/or other materials provided 15*418b791dSBob Badour * with the distribution. 16*418b791dSBob Badour * * Neither the name of The Linux Foundation nor the names of its 17*418b791dSBob Badour * contributors may be used to endorse or promote products derived 18*418b791dSBob Badour * from this software without specific prior written permission. 19*418b791dSBob Badour * 20*418b791dSBob Badour * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 21*418b791dSBob Badour * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 22*418b791dSBob Badour * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 23*418b791dSBob Badour * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 24*418b791dSBob Badour * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25*418b791dSBob Badour * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26*418b791dSBob Badour * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 27*418b791dSBob Badour * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28*418b791dSBob Badour * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 29*418b791dSBob Badour * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 30*418b791dSBob Badour * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31*418b791dSBob Badour */ 32*418b791dSBob Badour 33*418b791dSBob Badour /* 34*418b791dSBob Badour Platfrom Library Loader 35*418b791dSBob Badour ----------------------- 36*418b791dSBob Badour 37*418b791dSBob Badour proj/platform_libs is a library to help manage interdependencies between 38*418b791dSBob Badour other libraries. 39*418b791dSBob Badour 40*418b791dSBob Badour 41*418b791dSBob Badour to use you will need to add 42*418b791dSBob Badour 43*418b791dSBob Badour incs = { 'proj/platform_libs' } 44*418b791dSBob Badour 45*418b791dSBob Badour in source of the library implementation define the entry point 46*418b791dSBob Badour 47*418b791dSBob Badour #include "platform_libs.h" 48*418b791dSBob Badour PL_DEFINE(svfs, svfs_init, svfs_deinit); 49*418b791dSBob Badour 50*418b791dSBob Badour Platfrom Library List 51*418b791dSBob Badour --------------------- 52*418b791dSBob Badour 53*418b791dSBob Badour This list contains top level libraries to be initialized at boot. To make 54*418b791dSBob Badour sure that this library is loaded you would need to call PLRegister or 55*418b791dSBob Badour PLRegisterDefault in your scons build command. 56*418b791dSBob Badour 57*418b791dSBob Badour 58*418b791dSBob Badour Handling Interdependencies and Order 59*418b791dSBob Badour ------------------------------------ 60*418b791dSBob Badour 61*418b791dSBob Badour To make sure that library A is loaded before library B, library B's 62*418b791dSBob Badour constructor and destructor should initialize and cleanup libraryB. 63*418b791dSBob Badour 64*418b791dSBob Badour #include "platform_libs.h" 65*418b791dSBob Badour 66*418b791dSBob Badour PL_DEP(libraryB) 67*418b791dSBob Badour void libraryA_deinit(void) { 68*418b791dSBob Badour PL_DEINIT(libraryB); 69*418b791dSBob Badour } 70*418b791dSBob Badour 71*418b791dSBob Badour int libraryA_init(void) { 72*418b791dSBob Badour int nErr = 0; 73*418b791dSBob Badour // my init code 74*418b791dSBob Badour VERIFY(0 == PL_INIT(libraryB)); 75*418b791dSBob Badour bail: 76*418b791dSBob Badour if(nErr) { libraryA_deinit() } 77*418b791dSBob Badour return nErr; 78*418b791dSBob Badour } 79*418b791dSBob Badour 80*418b791dSBob Badour 81*418b791dSBob Badour libraryB does not need to appear in the platform library list. 82*418b791dSBob Badour */ 83*418b791dSBob Badour 84*418b791dSBob Badour #include <stdint.h> 85*418b791dSBob Badour 86*418b791dSBob Badour struct platform_lib { 87*418b791dSBob Badour const char* name; 88*418b791dSBob Badour uint32_t uRefs; 89*418b791dSBob Badour int nErr; 90*418b791dSBob Badour int (*init)(void); 91*418b791dSBob Badour void (*deinit)(void); 92*418b791dSBob Badour }; 93*418b791dSBob Badour 94*418b791dSBob Badour /** 95*418b791dSBob Badour * use this macro to pull in external dependencies 96*418b791dSBob Badour */ 97*418b791dSBob Badour #ifdef __cplusplus 98*418b791dSBob Badour #define PL_DEP(name)\ 99*418b791dSBob Badour extern "C" {\ 100*418b791dSBob Badour extern struct platform_lib* _pl_##name(void); \ 101*418b791dSBob Badour } 102*418b791dSBob Badour #else 103*418b791dSBob Badour #define PL_DEP(name)\ 104*418b791dSBob Badour extern struct platform_lib* _pl_##name(void); 105*418b791dSBob Badour #endif /* __cplusplus */ 106*418b791dSBob Badour 107*418b791dSBob Badour /** 108*418b791dSBob Badour * should be declared in source in the library 109*418b791dSBob Badour * if constructor fails, destructor is not called 110*418b791dSBob Badour */ 111*418b791dSBob Badour #ifdef __cplusplus 112*418b791dSBob Badour #define PL_DEFINE(name, init, deinit) \ 113*418b791dSBob Badour extern "C" {\ 114*418b791dSBob Badour struct platform_lib* _pl_##name(void) {\ 115*418b791dSBob Badour static struct platform_lib _gpl_##name = { #name, 0, -1, init, deinit };\ 116*418b791dSBob Badour return &_gpl_##name;\ 117*418b791dSBob Badour }\ 118*418b791dSBob Badour } 119*418b791dSBob Badour #else 120*418b791dSBob Badour #define PL_DEFINE(name, init, deinit) \ 121*418b791dSBob Badour struct platform_lib* _pl_##name(void) {\ 122*418b791dSBob Badour static struct platform_lib _gpl_##name = { #name, 0, -1, init, deinit };\ 123*418b791dSBob Badour return &_gpl_##name;\ 124*418b791dSBob Badour } 125*418b791dSBob Badour #endif /* __cplusplus */ 126*418b791dSBob Badour 127*418b791dSBob Badour /** 128*418b791dSBob Badour * should be added to platform_libs_list.c pl_list table 129*418b791dSBob Badour * for all top level modules 130*418b791dSBob Badour */ 131*418b791dSBob Badour #define PL_ENTRY(name) _pl_##name 132*418b791dSBob Badour 133*418b791dSBob Badour /** 134*418b791dSBob Badour * should be called within a constructor to ensure that a 135*418b791dSBob Badour * dependency has been initialized. so if foo depends on bar 136*418b791dSBob Badour * 137*418b791dSBob Badour * #include "bar.h" 138*418b791dSBob Badour * int foo_init() { 139*418b791dSBob Badour * return PL_INIT(bar); 140*418b791dSBob Badour * } 141*418b791dSBob Badour */ 142*418b791dSBob Badour #define PL_INIT(name) pl_lib_init(PL_ENTRY(name)) 143*418b791dSBob Badour 144*418b791dSBob Badour /** 145*418b791dSBob Badour * should be called within a destructor to ensure that a 146*418b791dSBob Badour * dependency has been cleaned up 147*418b791dSBob Badour */ 148*418b791dSBob Badour #define PL_DEINIT(name) pl_lib_deinit(PL_ENTRY(name)) 149*418b791dSBob Badour 150*418b791dSBob Badour #ifdef __cplusplus 151*418b791dSBob Badour extern "C" { 152*418b791dSBob Badour #endif /* __cplusplus */ 153*418b791dSBob Badour 154*418b791dSBob Badour /** 155*418b791dSBob Badour * initalize the static list of library constructors and destructors 156*418b791dSBob Badour * should be called from main() 157*418b791dSBob Badour * 158*418b791dSBob Badour * see platform_libs_list.h to add high level libraries 159*418b791dSBob Badour * 160*418b791dSBob Badour */ 161*418b791dSBob Badour int pl_init(void); 162*418b791dSBob Badour 163*418b791dSBob Badour /** 164*418b791dSBob Badour * calls all the destructors. 165*418b791dSBob Badour */ 166*418b791dSBob Badour void pl_deinit(void); 167*418b791dSBob Badour 168*418b791dSBob Badour /** 169*418b791dSBob Badour * initialize a single library. called via PL_INIT 170*418b791dSBob Badour */ 171*418b791dSBob Badour int pl_lib_init(struct platform_lib* (*pl)(void)); 172*418b791dSBob Badour 173*418b791dSBob Badour /** 174*418b791dSBob Badour * deinitialize a single library called via PL_DEINIT 175*418b791dSBob Badour */ 176*418b791dSBob Badour void pl_lib_deinit(struct platform_lib* (*pl)(void)); 177*418b791dSBob Badour 178*418b791dSBob Badour #ifdef __cplusplus 179*418b791dSBob Badour } 180*418b791dSBob Badour #endif 181*418b791dSBob Badour 182*418b791dSBob Badour #endif //PLATFORM_LIBS 183