Permalink
Browse files

Initializer functions

This patch adds a new `CORK_INITIALIZER` macro, which you can use to
declare a function that should be automatically executed at program
startup time.  Right now we only implement this macro on GCC, using the
`constructor` function attribute.
  • Loading branch information...
1 parent 6f2298f commit 54b2395c10688eacfd33aab551d9efaa9a5df08e Douglas Creager committed Dec 27, 2012
View
19 docs/attributes.rst
@@ -98,3 +98,22 @@ common compiler attributes.
{
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);
+ }
View
21 include/libcork/core/attributes.h
@@ -133,4 +133,25 @@
#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 */
View
8 src/CMakeLists.txt
@@ -82,6 +82,14 @@ set(CORK_HASH_SRC cork-hash/cork-hash.c)
add_executable(cork-hash ${CORK_HASH_SRC})
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)
add_executable(cork-test ${CORK_TEST_SRC})
target_link_libraries(cork-test embedded_libcork)
View
18 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");
+}
View
18 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");
+}
View
15 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;
+}
View
6 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 54b2395

Please sign in to comment.