Mastodon Feed: Post

Mastodon Feed

Boosted by cstanhope@social.coop ("Your friendly 'net denizen"):
mei@donotsta.re ("mei | fully hingeless architecture now in production") wrote:

@wren6991 @lofty one trick i've seen for dealing with fpga toolchains that i really like is to LD_PRELOAD this bad boy. makes them much less flakey.

screenshot of enterprise_malloc.c, a 30-line file with the following contents: #define _GNU_SOURCE #include <dlfcn.h> #include <pthread.h> #include <stdlib.h> #define FREE_DELAY 100000 static void *free_delay_queue[FREE_DELAY]; static int free_delay_pos; static void (*orig_free)(void *); static void *(*orig_malloc)(size_t); static pthread_mutex_t free_delay_mutex = PTHREAD_MUTEX_INITIALIZER; void free(void *ptr) { 	if (!ptr) 		return; 	pthread_mutex_lock(&free_delay_mutex); 	if (!orig_free) 		orig_free = dlsym(RTLD_NEXT, "free"); 	orig_free(free_delay_queue[free_delay_pos]); 	free_delay_queue[free_delay_pos] = ptr; 	free_delay_pos++; 	free_delay_pos %= FREE_DELAY; 	pthread_mutex_unlock(&free_delay_mutex); } void *malloc(size_t sz) { 	pthread_mutex_lock(&free_delay_mutex); 	if (!orig_malloc) 		orig_malloc = dlsym(RTLD_NEXT, "malloc"); 	pthread_mutex_unlock(&free_delay_mutex); 	return orig_malloc (sz * 2 + 0x100); }