Ticket #26: ctors.diff

File ctors.diff, 8.2 KB (added by becoulet, 15 years ago)

patch to use gcc constructor/destructor attributes

  • mutek/include/mutek/ctors.h

     
     1/*
     2    This file is part of MutekH.
     3
     4    MutekH is free software; you can redistribute it and/or modify it
     5    under the terms of the GNU General Public License as published by
     6    the Free Software Foundation; either version 2 of the License, or
     7    (at your option) any later version.
     8
     9    MutekH is distributed in the hope that it will be useful, but
     10    WITHOUT ANY WARRANTY; without even the implied warranty of
     11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12    General Public License for more details.
     13
     14    You should have received a copy of the GNU General Public License
     15    along with MutekH; if not, write to the Free Software Foundation,
     16    Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
     17
     18    Copyright Alexandre Becoulet <alexandre.becoulet@lip6.fr> (c) 2010
     19*/
     20
     21#ifndef _MUTEK_CTORS_H_
     22#define _MUTEK_CTORS_H_
     23
     24#include <hexo/types.h>
     25#include <hexo/error.h>
     26
     27#define __MUTEK_AT_START(n) error_t (n)(uint_fast8_t level)
     28typedef __MUTEK_AT_START(mutek_at_start_t);
     29
     30#define __MUTEK_AT_END(n) void (n)()
     31typedef __MUTEK_AT_END(mutek_at_end_t);
     32
     33#define MUTEK_AT_START(n) __attribute__((constructor,cold)) __MUTEK_AT_START(n)
     34#define MUTEK_AT_END(n) __attribute__((destructor,cold)) __MUTEK_AT_END(n)
     35
     36enum mutek_start_level_e
     37  {
     38    MUTEK_AT_DRVINIT_LEVEL,
     39    MUTEK_AT_MUTEK_START_LEVEL,
     40  };
     41
     42void execute_at_start(uint_fast8_t level);
     43void execute_at_end();
     44
     45#endif
     46
  • mutek/ctors.c

     
     1/*
     2    This file is part of MutekH.
     3
     4    MutekH is free software; you can redistribute it and/or modify it
     5    under the terms of the GNU General Public License as published by
     6    the Free Software Foundation; either version 2 of the License, or
     7    (at your option) any later version.
     8
     9    MutekH is distributed in the hope that it will be useful, but
     10    WITHOUT ANY WARRANTY; without even the implied warranty of
     11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12    General Public License for more details.
     13
     14    You should have received a copy of the GNU General Public License
     15    along with MutekH; if not, write to the Free Software Foundation,
     16    Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
     17
     18    Copyright Alexandre Becoulet <alexandre.becoulet@lip6.fr> (c) 2010
     19*/
     20
     21#include <mutek/ctors.h>
     22
     23extern __ldscript_symbol_t ctors_begin, ctors_end;
     24extern __ldscript_symbol_t dtors_begin, dtors_end;
     25
     26void execute_at_start(uint_fast8_t level)
     27{
     28  mutek_at_start_t **ctors = (mutek_at_start_t**)&ctors_begin;
     29  size_t i, count = (mutek_at_start_t**)&ctors_end - ctors;
     30
     31  for (i = 0; i < count; i++)
     32    {
     33      if (ctors[i] == NULL)
     34        continue;
     35
     36      if (ctors[i](level))
     37        continue;
     38
     39      ctors[i] = NULL;
     40    }
     41}
     42
     43void execute_at_end()
     44{
     45  mutek_at_end_t **dtors = (mutek_at_end_t**)&dtors_begin;
     46  size_t i, count = (mutek_at_end_t**)&dtors_end - dtors;
     47
     48  for (i = 0; i < count; i++)
     49    dtors[i]();
     50}
     51
  • mutek/Makefile

     
    33        mutek/printk.h mutek/rwlock.h mutek/scheduler.h mutek/timer.h   \
    44        mutek/vmem_kalloc.h mutek/semaphore.h mutek/fdt.h mutek/console.h
    55
    6 objs = timer.o printf_arg.o console.o
     6objs = timer.o printf_arg.o console.o ctors.o
    77
    88ifeq ($(CONFIG_MUTEK_MEM_REGION), defined)
    99objs += mem_region.o                                   
  • arch/emu/arch_init.c

     
    116116    sched_cpu_init();
    117117#endif
    118118
     119    execute_at_start(MUTEK_AT_DRVINIT_LEVEL);
     120
    119121    arch_hw_init();
    120122    mem_region_init();
    121123
     
    123125    cpu_init_flag = 1;
    124126#endif
    125127
     128    execute_at_start(MUTEK_AT_MUTEK_START_LEVEL);
     129
    126130    /* run mutek_start() */
    127131    mutek_start(0, 0);
    128132
  • arch/emu/ldscript.cpp

     
    1717                        *(.drivers)
    1818                        global_driver_registry_end = .;
    1919
    20                         /* data depending on cpu architecture (fonction pointer variables, ...) */
    21                         *(.cpuarchdata*)
     20                        // constructors and destructors
     21                        . = ALIGN(4);
     22                        ctors_begin = .;
     23                        KEEP(*(.ctors))
     24                          ctors_end = .;
     25                        dtors_begin = .;
     26                        KEEP(*(.dtors))
     27                          dtors_end = .;
    2228                }
    2329        __data_start = LOADADDR(.data);
    2430    __data_end = LOADADDR(.data) + SIZEOF(.data);
  • arch/ibmpc/arch_init.c

     
    3535#include <mutek/mem_alloc.h>
    3636#include <mutek/scheduler.h>
    3737#include <mutek/printk.h>
     38#include <mutek/ctors.h>
    3839
    3940#ifdef CONFIG_DRIVER_ICU_APIC
    4041# include <drivers/icu/apic/icu-apic.h>
     
    159160      sched_cpu_init();
    160161#endif
    161162
     163      execute_at_start(MUTEK_AT_DRVINIT_LEVEL);
     164
    162165#if defined(CONFIG_ARCH_HW_INIT_USER)
    163166          user_hw_init();
    164167#elif defined(CONFIG_ARCH_HW_INIT)
     
    167170# error No supported hardware initialization
    168171#endif
    169172
     173          execute_at_start(MUTEK_AT_MUTEK_START_LEVEL);
    170174
    171175      /* run mutek_start() */
    172176      mutek_start(0, 0);
  • arch/ibmpc/ldscript.cpp

     
    2525        .sdata : { *(.sdata*) } > mem_ram
    2626        .sbss :  { *(.sbss*) } > mem_ram
    2727        .bss :   { *(.bss*) } > mem_ram
    28         .data :  { *(.data*) } > mem_ram
     28        .data :  { *(.data*)
     29                   // constructors and destructors
     30                   . = ALIGN(4);
     31                   ctors_begin = .;
     32                   KEEP(*(.ctors))
     33                   ctors_end = .;
     34                   dtors_begin = .;
     35                   KEEP(*(.dtors))
     36                   dtors_end = .;
     37                  } > mem_ram
    2938        .cpuarchdata : { *(.cpuarchdata*) } > mem_ram
    3039
    3140        __system_heap_start = ADDR(.cpuarchdata) + SIZEOF(.cpuarchdata);
  • arch/soclib/arch_init.c

     
    4343#include <hexo/cpu.h>
    4444#include <mutek/printk.h>
    4545#include <mutek/scheduler.h>
     46#include <mutek/ctors.h>
    4647
    4748#include <string.h>
    4849
     
    141142        mmu_cpu_init();
    142143#endif
    143144
     145        execute_at_start(MUTEK_AT_DRVINIT_LEVEL);
     146
    144147#if defined(CONFIG_ARCH_DEVICE_TREE)
    145148        device_init(&fdt_enum_dev);
    146149        enum_fdt_init(&fdt_enum_dev, device_tree);
     
    174177        sched_cpu_init();
    175178#endif
    176179
     180        execute_at_start(MUTEK_AT_MUTEK_START_LEVEL);
     181
    177182        /* run mutek_start() */
    178183        mutek_start(0, 0);
    179184#ifdef CONFIG_ARCH_SMP
  • arch/soclib/ldscript.cpp

     
    120120                __data_start = ABSOLUTE(.);
    121121                *(.sdata*)
    122122                *(.data*)
    123                 *(.cpuarchdata*)
     123
     124                // constructors and destructors
     125                . = ALIGN(4);
     126                ctors_begin = .;
     127                KEEP(*(.ctors))
     128                ctors_end = .;
     129                dtors_begin = .;
     130                KEEP(*(.dtors))
     131                dtors_end = .;
    124132        } > mem_ram __AT_MEM_ROM
    125133
    126134        __data_load_start = LOADADDR(.data);
  • examples/hello/hello.c

     
    11
    22#include <pthread.h>
    33#include <mutek/printk.h>
     4#include <mutek/ctors.h>
    45
    56pthread_mutex_t m;
    67pthread_t a, b;
     
    1819
    1920void app_start()
    2021{
     22  return;
    2123  pthread_mutex_init(&m, NULL);
    2224  pthread_create(&a, NULL, f, "Hello world\n");
    2325  pthread_create(&b, NULL, f, "Hello world\n");
    2426}
    2527
     28static MUTEK_AT_START(myinit)
     29{
     30  printk("init level %i\n", level);
     31  return -EAGAIN;
     32}
     33