Skip to content

Commit

Permalink
Abstract __attribute__((constructor))
Browse files Browse the repository at this point in the history
Some platforms (e.g. macOS) do not support constructor priorities, so
at least make them compile. Ideally, we'd register a function to be
called at initialization the same way Lua modules are registered, and
manage the order inside lwan_init(); but this is good for now.
  • Loading branch information
lpereira committed Aug 27, 2024
1 parent 953d765 commit af47365
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/bin/tools/bin2hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
#include <sys/types.h>
#include <unistd.h>

#include "lwan.h"
#include "lwan-private.h"

static int constructor_attr_supported = 0;

__attribute__((constructor))
LWAN_CONSTRUCTOR()
static void initialize_constructor_attr_supported(void)
{
constructor_attr_supported = 1;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ static inline unsigned int hash_int64_crc32(const void *keyptr)

#endif

__attribute__((constructor(65535))) static void initialize_fnv1a_seed(void)
LWAN_CONSTRUCTOR(65535)
static void initialize_fnv1a_seed(void)
{
uint8_t entropy[128];

Expand Down
2 changes: 1 addition & 1 deletion src/lib/lwan-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ static int luaopen_log(lua_State *L)
DEFINE_ARRAY_TYPE(lwan_lua_method_array, luaL_reg)
static struct lwan_lua_method_array lua_methods;

__attribute__((constructor))
LWAN_CONSTRUCTOR()
__attribute__((no_sanitize_address))
static void register_lua_methods(void)
{
Expand Down
6 changes: 6 additions & 0 deletions src/lib/lwan-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
#define DEFAULT_BUFFER_SIZE 4096
#define DEFAULT_HEADERS_SIZE 2048

#if defined(__APPLE__)
# define LWAN_CONSTRUCTOR(prio_) __attribute__((constructor))
#else
# define LWAN_CONSTRUCTOR(prio_) __attribute__((constructor(prio_)))
#endif

struct lwan_request_parser_helper {
struct lwan_value *buffer; /* The whole request buffer */
char *next_request; /* For pipelined requests */
Expand Down
3 changes: 2 additions & 1 deletion src/lib/lwan-readahead.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ static pthread_t readahead_self;
static long page_size = PAGE_SIZE;

#ifdef _SC_PAGESIZE
__attribute__((constructor)) static void get_page_size(void)
LWAN_CONSTRUCTOR()
static void get_page_size(void)
{
long ps = sysconf(_SC_PAGESIZE);

Expand Down
3 changes: 2 additions & 1 deletion src/lib/lwan-request.c
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,8 @@ get_temp_dir(void)
return NULL;
}

__attribute__((constructor)) static void initialize_temp_dir(void)
LWAN_CONSTRUCTOR()
static void initialize_temp_dir(void)
{
temp_dir = get_temp_dir();
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/lwan-status.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ void lwan_syslog_status_out(
lwan_strbuf_free(&buf);
}

__attribute__((constructor)) static void register_lwan_to_syslog(void)
LWAN_CONSTRUCTOR()
static void register_lwan_to_syslog(void)
{
openlog("lwan", LOG_NDELAY | LOG_PID | LOG_CONS, LOG_USER);
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/lwan.c
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,8 @@ void lwan_main_loop(struct lwan *l)
}

#ifdef CLOCK_MONOTONIC_COARSE
__attribute__((constructor)) static void detect_fastest_monotonic_clock(void)
LWAN_CONSTRUCTOR()
static void detect_fastest_monotonic_clock(void)
{
struct timespec ts;

Expand Down
3 changes: 2 additions & 1 deletion src/samples/clock/blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <stdio.h>
#include <string.h>

#include "lwan-private.h"
#include "blocks.h"

enum shape {
Expand Down Expand Up @@ -175,7 +176,7 @@ static const struct fall *fall[] = {

static int block_sizes[10];

__attribute__((constructor)) void calculate_block_sizes(void)
LWAN_CONSTRUCTOR() void calculate_block_sizes(void)
{
for (int i = 0; i < 10; i++) {
const struct fall *instr = fall[i];
Expand Down
4 changes: 2 additions & 2 deletions src/samples/clock/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <stdlib.h>
#include <limits.h>

#include "lwan.h"
#include "lwan-private.h"
#include "lwan-template.h"
#include "lwan-mod-redirect.h"
#include "gifenc.h"
Expand Down Expand Up @@ -245,7 +245,7 @@ static const struct lwan_var_descriptor index_desc[] = {

static struct lwan_tpl *index_tpl;

__attribute__((constructor)) static void initialize_template(void)
LWAN_CONSTRUCTOR() static void initialize_template(void)
{
static const char index[] =
"<html>\n"
Expand Down
2 changes: 1 addition & 1 deletion src/samples/clock/xdaliclock.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ frame_from_pixmap(const unsigned char *bits, int width, int height)
return frame;
}

__attribute__((constructor)) static void initialize_numbers(void)
LWAN_CONSTRUCTOR() static void initialize_numbers(void)
{
const struct raw_number *raw = get_raw_numbers();

Expand Down

0 comments on commit af47365

Please sign in to comment.