Skip to content

Commit

Permalink
Merge branch 'feature/initializer' into develop
Browse files Browse the repository at this point in the history
* feature/initializer:
  Initializer functions
  • Loading branch information
Douglas Creager committed Dec 27, 2012
2 parents c138f23 + 54b2395 commit aec3b0e
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docs/attributes.rst
Expand Up @@ -98,3 +98,22 @@ common compiler attributes.
{ {
CORK_ATTR_UNUSED int unused_value; CORK_ATTR_UNUSED int unused_value;
} }


.. macro:: CORK_INITIALIZER(func_name)

Declare a ``static`` function that will be automatically called at program
startup. If there are multiple initializer functions linked into a program,
there is no guarantee about the order in which the functions will be called.

::

#include <libcork/core.h>
#include <libcork/ds.h>

static cork_array(int) array;

CORK_INITIALIZER(init_array)
{
cork_array_init(&array);
}
21 changes: 21 additions & 0 deletions include/libcork/core/attributes.h
Expand Up @@ -148,4 +148,25 @@
#endif #endif




/*
* Declare a static function that should automatically be called at program
* startup.
*/

/* TODO: When we implement a full Windows port, [1] describes how best to
* implement an initialization function under Visual Studio.
*
* [1] http://stackoverflow.com/questions/1113409/attribute-constructor-equivalent-in-vc
*/

#if CORK_CONFIG_HAVE_GCC_ATTRIBUTES
#define CORK_INITIALIZER(name) \
__attribute__((constructor)) \
static void \
name(void)
#else
#error "Don't know how to implement initialization functions of this platform"
#endif


#endif /* LIBCORK_CORE_ATTRIBUTES_H */ #endif /* LIBCORK_CORE_ATTRIBUTES_H */
8 changes: 8 additions & 0 deletions src/CMakeLists.txt
Expand Up @@ -85,6 +85,14 @@ set(CORK_HASH_SRC cork-hash/cork-hash.c)
add_executable(cork-hash ${CORK_HASH_SRC}) add_executable(cork-hash ${CORK_HASH_SRC})
target_link_libraries(cork-hash embedded_libcork) target_link_libraries(cork-hash embedded_libcork)


set(CORK_INITIALIZER_SRC
cork-initializer/init1.c
cork-initializer/init2.c
cork-initializer/main.c
)
add_executable(cork-initializer ${CORK_INITIALIZER_SRC})
target_link_libraries(cork-initializer embedded_libcork)

set(CORK_TEST_SRC cork-test/cork-test.c) set(CORK_TEST_SRC cork-test/cork-test.c)
add_executable(cork-test ${CORK_TEST_SRC}) add_executable(cork-test ${CORK_TEST_SRC})
target_link_libraries(cork-test embedded_libcork) target_link_libraries(cork-test embedded_libcork)
Expand Down
18 changes: 18 additions & 0 deletions src/cork-initializer/init1.c
@@ -0,0 +1,18 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2012, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/

#include <stdio.h>

#include <libcork/core.h>

CORK_INITIALIZER(init)
{
printf("Initializer 1\n");
}
18 changes: 18 additions & 0 deletions src/cork-initializer/init2.c
@@ -0,0 +1,18 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2012, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/

#include <stdio.h>

#include <libcork/core.h>

CORK_INITIALIZER(init)
{
printf("Initializer 2\n");
}
15 changes: 15 additions & 0 deletions src/cork-initializer/main.c
@@ -0,0 +1,15 @@
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2012, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license
* details.
* ----------------------------------------------------------------------
*/

int
main(int argc, char **argv)
{
return 0;
}
6 changes: 6 additions & 0 deletions tests/cork-initializer.t
@@ -0,0 +1,6 @@
We need to sort the output, since there's no guarantee about which order our
initializer functions will run in.
$ cork-initializer | sort
Initializer 1
Initializer 2

0 comments on commit aec3b0e

Please sign in to comment.