]> git.sesse.net Git - vlc/blobdiff - bin/override.c
Avoid calling dlsym at every code
[vlc] / bin / override.c
index a8b0754887f4758ea774d4fde466c01b3e3a4343..5996e43c499b44067a2b67b8d4cdbf1840062dc1 100644 (file)
@@ -26,7 +26,7 @@
 
 void vlc_enable_override (void);
 
-#if defined (__GNUC__) /* typeof and statement-expression */ \
+#if defined (__GNUC__) \
  && (defined (__ELF__) && !defined (__sun__))
 /* Solaris crashes on printf("%s", NULL); which is legal, but annoying. */
 
@@ -97,8 +97,23 @@ static void *getsym (const char *name)
 }
 
 #define LOG(level, ...) logbug(level, __func__, __VA_ARGS__)
+/* Evil non-standard GNU C macro ;)
+ *  typeof keyword,
+ *  statement-expression,
+ *  nested function...
+ */
 #define CALL(func, ...) \
-    ({ typeof (func) *sym = getsym ( # func); sym (__VA_ARGS__); })
+({ \
+    static typeof (func) *sym = NULL; \
+    static pthread_once_t once = PTHREAD_ONCE_INIT; \
+    auto void getsym_once (void); \
+    void getsym_once (void) \
+    { \
+        sym = getsym ( # func); \
+    } \
+    pthread_once (&once, getsym_once); \
+    sym (__VA_ARGS__); \
+})
 
 
 /*** Environment ***
@@ -148,24 +163,28 @@ int unsetenv (const char *name)
  * preserve reproducibility of the number sequence (which usually does not
  * matter).
  **/
-static pthread_mutex_t prng_lock = PTHREAD_MUTEX_INITIALIZER;
+static struct
+{
+    pthread_mutex_t lock;
+    unsigned int seed;
+} prng = { PTHREAD_MUTEX_INITIALIZER, 0, };
 
 void srand (unsigned int seed)
 {
-    pthread_mutex_lock (&prng_lock);
+    pthread_mutex_lock (&prng.lock);
     LOG("Warning", "%d", seed);
-    CALL(srand, seed);
-    pthread_mutex_unlock (&prng_lock);
+    prng.seed = seed;
+    pthread_mutex_unlock (&prng.lock);
 }
 
 int rand (void)
 {
     int ret;
 
-    pthread_mutex_lock (&prng_lock);
+    pthread_mutex_lock (&prng.lock);
     LOG("Warning", "");
-    ret = CALL(rand);
-    pthread_mutex_unlock (&prng_lock);
+    ret = rand_r (&prng.seed);
+    pthread_mutex_unlock (&prng.lock);
     return ret;
 }