Last active
July 18, 2019 07:38
-
-
Save aoleg94/1d94dbc66b00dca41bbc17c98ef76e5a to your computer and use it in GitHub Desktop.
gcc linker list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
linker_list | |
*.o |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "linker_list.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
struct kek { | |
int i; | |
float f; | |
const char* s; | |
}; | |
LINKER_LIST_ITEM(struct kek, lalka) = {42, 3.14f, "ololo"}; | |
LINKER_LIST_ITEM(struct kek, lalka) = {1337, 2.71f, "azaza"}; | |
LINKER_LIST_DECLARE(struct kek, lalka) | |
static int kek_cmp(const void* a, const void* b) | |
{ | |
struct kek* ka = (struct kek*)a; | |
struct kek* kb = (struct kek*)b; | |
return ka->i - kb->i; | |
} | |
int main() | |
{ | |
LINKER_LIST_FOREACH(struct kek, lalka, k) | |
{ | |
printf("before %d %f %s\n", k->i, k->f, k->s); | |
} | |
LINKER_LIST_QSORT(struct kek, lalka, kek_cmp); | |
LINKER_LIST_FOREACH(struct kek, lalka, k) | |
{ | |
printf("after %d %f %s\n", k->i, k->f, k->s); | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef LINKER_LIST_H | |
#define LINKER_LIST_H | |
#define LINKER_LIST_CONCAT2(x, y) x##y | |
#define LINKER_LIST_CONCAT1(x, y) LINKER_LIST_CONCAT2(x,y) | |
#define LINKER_LIST_ITEM(T, N) static T LINKER_LIST_CONCAT1(__LINKER_LIST_ITEM_ ## N, __LINE__) __attribute((__section__(#N))) __attribute((__used__)) | |
#define LINKER_LIST_DECLARE(T, N) \ | |
extern T LINKER_LIST_CONCAT1(__start_, N); \ | |
extern T LINKER_LIST_CONCAT1(__stop_, N); | |
#define LINKER_LIST_FOREACH(T, N, V) for(T* V = &LINKER_LIST_CONCAT1(__start_, N); V != &LINKER_LIST_CONCAT1(__stop_, N); V++) | |
#define LINKER_LIST_QSORT(T, N, F) qsort((void*)&LINKER_LIST_CONCAT1(__start_, N), &LINKER_LIST_CONCAT1(__stop_, N) - &LINKER_LIST_CONCAT1(__start_, N), sizeof(T), F) | |
#define LINKER_LIST_QSORT_R(T, N, F, A) qsort_r((void*)&LINKER_LIST_CONCAT1(__start_, N), &LINKER_LIST_CONCAT1(__start_, N) - &LINKER_LIST_CONCAT1(__stop_, N), sizeof(T), F, A) | |
#endif//LINKER_LIST_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: linker_list | |
linker_list: linker_list.c linker_list.h | |
gcc -o $@ $< -Wall -O2 | |
test: linker_list | |
./linker_list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment